go-sh-manymanuals/gshmm.go

357 lines
9.3 KiB
Go
Raw Normal View History

2023-05-10 18:34:09 +02:00
// go-sh-manymanuals TODO
2023-05-10 00:49:15 +02:00
package main
import (
"flag"
"fmt"
2023-05-10 19:48:55 +02:00
"log"
2023-05-10 00:49:15 +02:00
"os"
2023-05-10 09:58:59 +02:00
"path/filepath"
2023-05-10 01:36:11 +02:00
"strings"
2023-05-10 00:49:15 +02:00
2023-05-10 19:24:10 +02:00
"github.com/charmbracelet/bubbles/spinner"
2023-05-10 01:09:56 +02:00
"github.com/charmbracelet/bubbles/textinput"
2023-05-10 13:07:34 +02:00
"github.com/charmbracelet/bubbles/viewport"
2023-05-10 00:49:15 +02:00
tea "github.com/charmbracelet/bubbletea"
2023-05-10 13:07:34 +02:00
"github.com/charmbracelet/lipgloss"
pdf "github.com/johbar/go-poppler"
2023-05-11 00:11:13 +02:00
"github.com/rkoesters/xdg"
2023-05-10 09:53:08 +02:00
"github.com/sahilm/fuzzy"
2023-05-10 19:48:55 +02:00
"golang.org/x/term"
2023-05-10 00:49:15 +02:00
)
2023-05-10 18:51:32 +02:00
// filenameFilterMode searches by PDF file name.
const filenameFilterMode = "filename"
// contentFilterMode searches by PDF contents.
const contentFilterMode = "content"
2023-05-10 18:34:09 +02:00
// help is the command-line interface help output.
2023-05-10 00:49:15 +02:00
const help = `go-sh-manymanuals: TODO
TODO
Options:
-h output help
`
2023-05-10 01:36:11 +02:00
// minCharsUntilFilter is the minimum amount of characters that are
// required before the filtering logic commences actually filtering.
2023-05-10 09:53:08 +02:00
const minCharsUntilFilter = 2
2023-05-10 01:36:11 +02:00
2023-05-10 18:34:09 +02:00
// helpFlag is the help flag for the command-line interface.
2023-05-10 00:49:15 +02:00
var helpFlag bool
2023-05-10 18:34:09 +02:00
// handleCliFlags handles command-line flags.
2023-05-10 00:49:15 +02:00
func handleCliFlags() {
flag.BoolVar(&helpFlag, "h", false, "output help")
flag.Parse()
}
2023-05-10 18:34:09 +02:00
// 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.
2023-05-10 13:07:34 +02:00
func readPDF(name string) (string, error) {
doc, err := pdf.Open(name)
2023-05-10 13:07:34 +02:00
if err != nil {
return "", err
2023-05-10 13:07:34 +02:00
}
defer doc.Close()
2023-05-10 13:07:34 +02:00
var txt string
for i := 0; i < doc.GetNPages(); i++ {
txt += doc.GetPage(i).Text()
2023-05-10 13:07:34 +02:00
}
return txt, nil
2023-05-10 13:07:34 +02:00
}
2023-05-10 18:34:09 +02:00
// model offers the core of the state for the entire UI.
2023-05-10 00:49:15 +02:00
type model struct {
2023-05-10 19:48:55 +02:00
input textinput.Model // Fuzzy search interface
filterMode string // The filtering mode ("filename", "content")
datasheets []datasheet // All datasheets under cwd
datasheetNames []string // All datasheet names (caching)
datasheetsStyle lipgloss.Style // Style to use for listing datasheets
filteredDatasheets []string // Filtered view on all datasheets
loadDatasheetSpinner spinner.Model // Spinner to show while loading datasheets
datasheetsLoaded bool // Whether or not the datasheets are loaded or not
datasheetViewport viewport.Model // Viewport for the PDF content
datasheetViewportStyle lipgloss.Style // Style to show while showing datasheet viewport
2023-05-10 13:07:34 +02:00
}
2023-05-10 18:34:09 +02:00
// datasheetFromName retrieves a datasheet via a name.
2023-05-10 14:35:13 +02:00
func (m model) datasheetFromName(name string) string {
for _, d := range m.datasheets {
if d.filename == name {
return d.contents
}
}
return ""
}
2023-05-10 18:51:32 +02:00
// toggleFilterMode toggles the filter mode.
func (m *model) toggleFilterMode() {
if m.filterMode == filenameFilterMode {
m.filterMode = contentFilterMode
} else {
m.filterMode = filenameFilterMode
}
}
2023-05-10 18:34:09 +02:00
// datasheet represents a datasheet on disk.
2023-05-10 13:07:34 +02:00
type datasheet struct {
2023-05-10 18:34:09 +02:00
filename string // The name of the file
absPath string // the absolute file path
contents string // the contents of the PDF
2023-05-10 00:49:15 +02:00
}
2023-05-10 18:34:09 +02:00
// initialModel constucts an initial state for the UI.
2023-05-10 19:48:55 +02:00
func initialModel(width int, height int) model {
2023-05-10 01:09:56 +02:00
input := textinput.New()
input.Focus()
2023-05-10 19:48:55 +02:00
datasheetViewport := viewport.New(width/2+20, height/2)
datasheetViewportStyle := lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63")).
Padding(2)
2023-05-10 19:24:10 +02:00
2023-05-14 19:40:09 +02:00
// NOTE: disable the default keymap, we don't want to be able to
// scroll on this viewport or control it in any view. it's
// read-only
datasheetViewport.KeyMap = viewport.KeyMap{}
2023-05-10 19:24:10 +02:00
loadDatasheetSpinner := spinner.New()
loadDatasheetSpinner.Spinner = spinner.Dot
2023-05-10 19:48:55 +02:00
loadDatasheetSpinner.Style = lipgloss.NewStyle().
Foreground(lipgloss.Color("205"))
datasheetsStyle := lipgloss.NewStyle().
Width(width/2 - 30).
Height(height/2 - 30).
MarginRight(2).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63")).
Padding(2)
2023-05-10 19:24:10 +02:00
m := model{
2023-05-10 19:48:55 +02:00
input: input,
filterMode: filenameFilterMode,
datasheetsLoaded: false,
loadDatasheetSpinner: loadDatasheetSpinner,
datasheetViewport: datasheetViewport,
datasheetViewportStyle: datasheetViewportStyle,
datasheetsStyle: datasheetsStyle,
2023-05-10 19:24:10 +02:00
}
return m
}
// loadedDatasheetsMsg signals to the UI that the datasheets have been loaded.
type loadedDatasheetsMsg struct {
datasheets []datasheet
datasheetNames []string
}
// loadDatasheets loads the datasheets from disk. The UI shows a spinner while
// this is happening so that nobody gets bored.
func loadDatasheets(m model) loadedDatasheetsMsg {
var datasheets []datasheet
var datasheetNames []string
2023-05-10 19:27:33 +02:00
// TODO: have this gradually update the UI as files are walked/parsed instead
// of blocking until everything is done in one big go. the current loading
// spinner will be there for a while on actually many manuals
2023-05-10 13:07:34 +02:00
// TODO: handle error in interface?
2023-05-10 09:58:59 +02:00
_ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
name := info.Name()
if strings.HasSuffix(name, "pdf") {
2023-05-10 13:07:34 +02:00
// TODO: handle error in interface?
2023-05-10 14:35:13 +02:00
// TODO: don't read them all up front while blocking?
// we could run this in a goroutine somewhere
// this currently slows down startup time
2023-05-10 13:07:34 +02:00
contents, _ := readPDF(path)
datasheet := datasheet{
2023-05-10 13:07:34 +02:00
filename: name,
absPath: path,
contents: contents,
}
datasheets = append(datasheets, datasheet)
datasheetNames = append(datasheetNames, name)
2023-05-10 09:58:59 +02:00
}
return nil
})
2023-05-10 09:53:08 +02:00
2023-05-10 19:24:10 +02:00
return loadedDatasheetsMsg{
datasheets: datasheets,
datasheetNames: datasheetNames,
2023-05-10 01:09:56 +02:00
}
2023-05-10 00:49:15 +02:00
}
2023-05-10 18:34:09 +02:00
// Init initialises the program.
2023-05-10 00:49:15 +02:00
func (m model) Init() tea.Cmd {
2023-05-10 19:24:10 +02:00
return tea.Batch(
func() tea.Msg { return loadDatasheets(m) },
textinput.Blink,
m.loadDatasheetSpinner.Tick,
)
2023-05-10 00:49:15 +02:00
}
// filterDatasheetNames filters datasheet names based on user input.
func filterDatasheetNames(m model) []string {
search := m.input.Value()
if !(len(search) >= minCharsUntilFilter) {
return m.datasheetNames
}
var matchedDatasheets []string
matches := fuzzy.Find(search, m.datasheetNames)
for _, match := range matches {
matchedDatasheets = append(matchedDatasheets, match.Str)
}
if len(matches) > 0 {
return matchedDatasheets
}
return m.datasheetNames
}
2023-05-10 18:34:09 +02:00
// Update updates the program state.
2023-05-10 00:49:15 +02:00
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2023-05-10 13:07:34 +02:00
var (
cmd tea.Cmd
cmds []tea.Cmd
)
2023-05-10 01:09:56 +02:00
2023-05-10 19:24:10 +02:00
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
m.datasheetViewport, cmd = m.datasheetViewport.Update(msg)
cmds = append(cmds, cmd)
m.loadDatasheetSpinner, cmd = m.loadDatasheetSpinner.Update(msg)
cmds = append(cmds, cmd)
if m.input.Focused() && m.datasheetsLoaded {
m.filteredDatasheets = filterDatasheetNames(m)
2023-05-10 14:35:13 +02:00
// TODO: implement cursor for scrolling up/down filtered
// results so we can view the PDF contents as desired
2023-05-10 16:45:23 +02:00
// it's currently just the last one (closest to input)
2023-05-10 19:01:57 +02:00
lastDatasheet := m.filteredDatasheets[len(m.filteredDatasheets)-1]
viewportText := m.datasheetFromName(lastDatasheet)
2023-05-10 18:36:13 +02:00
m.datasheetViewport.SetContent(viewportText)
2023-05-10 01:36:11 +02:00
}
2023-05-10 00:49:15 +02:00
switch msg := msg.(type) {
2023-05-10 19:24:10 +02:00
case loadedDatasheetsMsg:
m.datasheets = msg.datasheets
m.datasheetNames = msg.datasheetNames
m.filteredDatasheets = msg.datasheetNames
2023-05-11 00:11:13 +02:00
selectedDatasheet := msg.datasheets[len(msg.datasheets)-1]
m.datasheetViewport.SetContent(selectedDatasheet.contents)
2023-05-10 19:24:10 +02:00
m.datasheetsLoaded = true
2023-05-10 19:48:55 +02:00
case tea.WindowSizeMsg:
// TODO: handle terminal resizing
// resize listing / viewport
2023-05-10 00:49:15 +02:00
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
2023-05-10 00:49:15 +02:00
return m, tea.Quit
2023-05-10 18:51:32 +02:00
case "tab":
m.toggleFilterMode()
2023-05-11 00:11:13 +02:00
case "enter":
selectedDatasheet := m.datasheets[len(m.datasheets)-1]
// TODO: handle error
_ = xdg.Open(selectedDatasheet.absPath)
2023-05-10 00:49:15 +02:00
}
}
2023-05-10 13:07:34 +02:00
return m, tea.Batch(cmds...)
2023-05-10 00:49:15 +02:00
}
2023-05-10 18:34:09 +02:00
// View outputs the program state for viewing.
2023-05-10 00:49:15 +02:00
func (m model) View() string {
2023-05-10 01:36:11 +02:00
body := strings.Builder{}
2023-05-10 19:24:10 +02:00
var sheets string
if !m.datasheetsLoaded {
sheets = fmt.Sprintf("%s gathering datasheets...", m.loadDatasheetSpinner.View())
} else {
// TODO: paginate / trim view to last 10 or something?
sheets = strings.Join(m.filteredDatasheets, "\n")
}
2023-05-10 16:45:23 +02:00
// TODO: style further with lipgloss, e.g. borders, margins, etc.
2023-05-10 19:48:55 +02:00
panes := lipgloss.JoinHorizontal(
lipgloss.Left,
m.datasheetsStyle.Render(sheets),
m.datasheetViewportStyle.Render(m.datasheetViewport.View()),
)
2023-05-10 16:45:23 +02:00
2023-05-10 13:07:34 +02:00
body.WriteString(panes)
2023-05-10 01:36:11 +02:00
body.WriteString("\n" + m.input.View())
2023-05-10 01:36:11 +02:00
2023-05-10 18:51:32 +02:00
mode := "filter: "
if m.filterMode == filenameFilterMode {
mode += "filename"
} else {
// TODO make this mode actually work once we figure out bleve
mode += "content (FIXME)"
}
body.WriteString("\n" + mode)
2023-05-11 00:11:13 +02:00
help := "[ctrl-c]: quit | [tab]: filter mode | [enter]: open"
2023-05-10 18:51:32 +02:00
body.WriteString("\n" + help)
2023-05-10 01:36:11 +02:00
return body.String()
2023-05-10 00:49:15 +02:00
}
2023-05-10 18:34:09 +02:00
// main is the command-line entrypoint.
2023-05-10 00:49:15 +02:00
func main() {
handleCliFlags()
if helpFlag {
fmt.Printf(help)
os.Exit(0)
}
2023-05-10 13:07:34 +02:00
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
2023-05-10 19:48:55 +02:00
log.Fatal(err)
2023-05-10 13:07:34 +02:00
}
defer f.Close()
2023-05-10 19:48:55 +02:00
width, height, err := term.GetSize(0)
if err != nil {
log.Fatal(err)
}
2023-05-10 13:07:34 +02:00
p := tea.NewProgram(
2023-05-10 19:48:55 +02:00
initialModel(width, height),
2023-05-10 13:07:34 +02:00
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
2023-05-10 00:49:15 +02:00
if err := p.Start(); err != nil {
fmt.Printf("oops, something went wrong: %v", err)
os.Exit(1)
}
}