forked from varia/go-sh-manymanuals
113 lines
1.9 KiB
Go
113 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
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 = 3
|
|
|
|
var helpFlag bool
|
|
|
|
type Configuration struct {
|
|
ManualDir string
|
|
}
|
|
|
|
func handleCliFlags() {
|
|
flag.BoolVar(&helpFlag, "h", false, "output help")
|
|
flag.Parse()
|
|
}
|
|
|
|
type model struct {
|
|
filterInput textinput.Model
|
|
datasheets []string
|
|
}
|
|
|
|
func initialModel() model {
|
|
input := textinput.New()
|
|
input.Focus()
|
|
|
|
return model{
|
|
filterInput: input,
|
|
datasheets: []string{},
|
|
}
|
|
}
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
// TODO(d1): implement reading all PDFs in cwd
|
|
// https://stackoverflow.com/a/67214584
|
|
// requires error reporting also
|
|
return textinput.Blink
|
|
}
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
var cmd tea.Cmd
|
|
|
|
if m.filterInput.Focused() {
|
|
var filtered []string
|
|
|
|
search := m.filterInput.Value()
|
|
if len(search) >= minCharsUntilFilter {
|
|
// NOTE(d1): naive prototype implementation
|
|
for _, ds := range m.datasheets {
|
|
if strings.HasPrefix(ds, search) {
|
|
filtered = append(filtered, ds)
|
|
}
|
|
}
|
|
m.datasheets = filtered
|
|
}
|
|
}
|
|
|
|
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)
|
|
|
|
return m, cmd
|
|
}
|
|
|
|
func (m model) View() string {
|
|
body := strings.Builder{}
|
|
|
|
body.WriteString(strings.Join(m.datasheets, "\n") + "\n")
|
|
|
|
body.WriteString(m.filterInput.View() + "\n")
|
|
|
|
return body.String()
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|