// go-sh-manymanuals TODO package main import ( "flag" "fmt" "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" ) // help is the command-line interface help output. 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 // helpFlag is the help flag for the command-line interface. var helpFlag bool // handleCliFlags handles command-line flags. func handleCliFlags() { flag.BoolVar(&helpFlag, "h", false, "output help") flag.Parse() } // readPDF reads the plain text contents of a PDF. This does not include the // formatting. Only the content you see when you view it through a PDF reader. // The library we're using makes use of the C bindings to the Poppler PDF // library https://poppler.freedesktop.org which appears to offer a good mix of // reliability and effectiveness. 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 } // model offers the core of the state for the entire UI. type model struct { input textinput.Model // Fuzzy search interface datasheets []datasheet // All datasheets under cwd datasheetNames []string // All datasheet names (caching) filteredDatasheets []string // Filtered view on all datasheets datasheetViewport viewport.Model // Viewport for the PDF content } // datasheetFromName retrieves a datasheet via a name. func (m model) datasheetFromName(name string) string { for _, d := range m.datasheets { if d.filename == name { return d.contents } } return "" } // datasheet represents a datasheet on disk. type datasheet struct { filename string // The name of the file absPath string // the absolute file path contents string // the contents of the PDF } // initialModel constucts an initial state for the UI. func initialModel() model { input := textinput.New() input.Focus() var datasheets []datasheet var datasheetNames []string // 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? // TODO: don't read them all up front while blocking? // we could run this in a goroutine somewhere // this currently slows down startup time contents, _ := readPDF(path) datasheet := datasheet{ filename: name, absPath: path, contents: contents, } datasheets = append(datasheets, datasheet) datasheetNames = append(datasheetNames, name) } return nil }) // TODO: set width/heigh to match terminal viewp := viewport.New(60, 30) selectedDatasheet := datasheets[len(datasheets)-1].contents viewp.SetContent(selectedDatasheet) m := model{ input: input, datasheets: datasheets, datasheetNames: datasheetNames, filteredDatasheets: datasheetNames, datasheetViewport: viewp, } return m } // Init initialises the program. func (m model) Init() tea.Cmd { return textinput.Blink } // Update updates the program state. func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var ( cmd tea.Cmd cmds []tea.Cmd ) if m.input.Focused() { var matchedDatasheets []string search := m.input.Value() if len(search) >= minCharsUntilFilter { matches := fuzzy.Find(search, m.datasheetNames) for _, match := range matches { matchedDatasheets = append(matchedDatasheets, match.Str) } if len(matches) > 0 { m.filteredDatasheets = matchedDatasheets } else { m.filteredDatasheets = m.datasheetNames } } else { m.filteredDatasheets = m.datasheetNames } // TODO: implement cursor for scrolling up/down filtered // results so we can view the PDF contents as desired // it's currently just the last one (closest to input) lastdatasheet := m.filteredDatasheets[len(m.filteredDatasheets)-1] viewportText := m.datasheetFromName(lastdatasheet) m.datasheetViewport.SetContent(viewportText) } // TODO: handle terminal resizing switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "ctrl+c": return m, tea.Quit } } m.input, cmd = m.input.Update(msg) cmds = append(cmds, cmd) m.datasheetViewport, cmd = m.datasheetViewport.Update(msg) cmds = append(cmds, cmd) return m, tea.Batch(cmds...) } // View outputs the program state for viewing. func (m model) View() string { body := strings.Builder{} // TODO: paginate / trim view to last 10 or something? sheets := strings.Join(m.filteredDatasheets, "\n") // TODO: style further with lipgloss, e.g. borders, margins, etc. panes := lipgloss.JoinHorizontal(lipgloss.Left, sheets, m.datasheetViewport.View()) body.WriteString(panes) body.WriteString("\n" + m.input.View()) return body.String() } // main is the command-line entrypoint. 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) } }