go-sh-manymanuals/gshmm.go
decentral1se 73bd0d74f1
wip: use poppler because it's the core of pdf2text
This way we can actually read the PDF contents.
2023-05-10 14:08:06 +02:00

207 lines
4.0 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
pdf "github.com/johbar/go-poppler"
"github.com/sahilm/fuzzy"
)
const help = `go-sh-manymanuals: TODO
TODO
Options:
-h output help
`
// minCharsUntilFilter is the minimum amount of characters that are
// required before the filtering logic commences actually filtering.
const minCharsUntilFilter = 2
var helpFlag bool
type Configuration struct {
ManualDir string
}
func handleCliFlags() {
flag.BoolVar(&helpFlag, "h", false, "output help")
flag.Parse()
}
func readPDF(name string) (string, error) {
doc, err := pdf.Open(name)
if err != nil {
return "", err
}
defer doc.Close()
var txt string
for i := 0; i < doc.GetNPages(); i++ {
txt += doc.GetPage(i).Text()
}
return txt, nil
}
type model struct {
filterInput textinput.Model // fuzzy search interface
datasheets []datasheet // all datasheets under cwd
dataSheetsView []string // filtered view on all datasheets
dataSheetViewport viewport.Model
}
func (m model) dataSheetNames() []string {
var names []string
for _, datasheet := range m.datasheets {
names = append(names, datasheet.filename)
}
return names
}
type datasheet struct {
filename string
absPath string
contents string
}
func initialModel() model {
input := textinput.New()
input.Focus()
var ds []datasheet
// TODO: handle error in interface?
_ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
name := info.Name()
if strings.HasSuffix(name, "pdf") {
// TODO: handle error in interface?
contents, _ := readPDF(path)
d := datasheet{
filename: name,
absPath: path,
contents: contents,
}
ds = append(ds, d)
log.Print(d)
}
return nil
})
viewp := viewport.New(60, 30)
viewp.SetContent(ds[len(ds)-1].contents)
m := model{
filterInput: input,
datasheets: ds,
dataSheetViewport: viewp,
}
// TODO: which index is the datasheet closest to the filter input?
m.dataSheetsView = m.dataSheetNames()
return m
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
if m.filterInput.Focused() {
var matched []string
search := m.filterInput.Value()
if len(search) >= minCharsUntilFilter {
matches := fuzzy.Find(search, m.dataSheetNames())
for _, match := range matches {
matched = append(matched, match.Str)
}
if len(matches) > 0 {
m.dataSheetsView = matched
} else {
m.dataSheetsView = m.dataSheetNames()
}
} else {
m.dataSheetsView = m.dataSheetNames()
}
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
}
m.filterInput, cmd = m.filterInput.Update(msg)
cmds = append(cmds, cmd)
// TODO figure out how update viewport when filtering
// the last item in m.dataSheetsView should be shown
m.dataSheetViewport, cmd = m.dataSheetViewport.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
body := strings.Builder{}
// TODO: paginate / trim view to last 10 or something?
sheets := strings.Join(m.dataSheetsView, "\n")
panes := lipgloss.JoinHorizontal(lipgloss.Left, sheets, m.dataSheetViewport.View())
body.WriteString(panes)
body.WriteString("\n" + m.filterInput.View())
return body.String()
}
func main() {
handleCliFlags()
if helpFlag {
fmt.Printf(help)
os.Exit(0)
}
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
defer f.Close()
p := tea.NewProgram(
initialModel(),
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
if err := p.Start(); err != nil {
fmt.Printf("oops, something went wrong: %v", err)
os.Exit(1)
}
}