go-sh-manymanuals/gshmm.go

138 lines
2.5 KiB
Go
Raw Normal View History

2023-05-10 00:49:15 +02:00
package main
import (
"flag"
"fmt"
"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 01:09:56 +02:00
"github.com/charmbracelet/bubbles/textinput"
2023-05-10 00:49:15 +02:00
tea "github.com/charmbracelet/bubbletea"
2023-05-10 09:53:08 +02:00
"github.com/sahilm/fuzzy"
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 00:49:15 +02:00
var helpFlag bool
type Configuration struct {
ManualDir string
}
func handleCliFlags() {
flag.BoolVar(&helpFlag, "h", false, "output help")
flag.Parse()
}
type model struct {
2023-05-10 09:53:08 +02:00
filterInput textinput.Model // fuzzy search interface
datasheets []string // all datasheets under cwd
dataSheetsView []string // filtered view on all datasheets
2023-05-10 00:49:15 +02:00
}
func initialModel() model {
2023-05-10 01:09:56 +02:00
input := textinput.New()
input.Focus()
2023-05-10 09:58:59 +02:00
var ds []string
// TODO(d1): 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") {
ds = append(ds, name)
}
return nil
})
2023-05-10 09:53:08 +02:00
2023-05-10 01:09:56 +02:00
return model{
2023-05-10 09:53:08 +02:00
filterInput: input,
datasheets: ds,
dataSheetsView: ds,
2023-05-10 01:09:56 +02:00
}
2023-05-10 00:49:15 +02:00
}
func (m model) Init() tea.Cmd {
2023-05-10 01:36:11 +02:00
// TODO(d1): implement reading all PDFs in cwd
// https://stackoverflow.com/a/67214584
// requires error reporting also
2023-05-10 01:09:56 +02:00
return textinput.Blink
2023-05-10 00:49:15 +02:00
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2023-05-10 01:09:56 +02:00
var cmd tea.Cmd
2023-05-10 01:36:11 +02:00
if m.filterInput.Focused() {
2023-05-10 09:53:08 +02:00
var matched []string
2023-05-10 01:36:11 +02:00
search := m.filterInput.Value()
if len(search) >= minCharsUntilFilter {
2023-05-10 09:53:08 +02:00
matches := fuzzy.Find(search, m.datasheets)
for _, match := range matches {
matched = append(matched, match.Str)
}
if len(matches) > 0 {
m.dataSheetsView = matched
} else {
m.dataSheetsView = m.datasheets
2023-05-10 01:36:11 +02:00
}
2023-05-10 09:53:08 +02:00
} else {
m.dataSheetsView = m.datasheets
2023-05-10 01:36:11 +02:00
}
2023-05-10 09:53:08 +02:00
2023-05-10 01:36:11 +02:00
}
2023-05-10 00:49:15 +02:00
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
}
2023-05-10 01:09:56 +02:00
m.filterInput, cmd = m.filterInput.Update(msg)
return m, cmd
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 09:53:08 +02:00
body.WriteString(strings.Join(m.dataSheetsView, "\n") + "\n")
2023-05-10 01:36:11 +02:00
body.WriteString(m.filterInput.View() + "\n")
return body.String()
2023-05-10 00:49:15 +02:00
}
func main() {
handleCliFlags()
if helpFlag {
fmt.Printf(help)
os.Exit(0)
}
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
if err := p.Start(); err != nil {
fmt.Printf("oops, something went wrong: %v", err)
os.Exit(1)
}
}