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 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"
|
2023-05-10 14:08:06 +02:00
|
|
|
pdf "github.com/johbar/go-poppler"
|
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
|
|
|
|
|
|
|
|
func handleCliFlags() {
|
|
|
|
flag.BoolVar(&helpFlag, "h", false, "output help")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2023-05-10 13:07:34 +02:00
|
|
|
func readPDF(name string) (string, error) {
|
2023-05-10 14:08:06 +02:00
|
|
|
doc, err := pdf.Open(name)
|
2023-05-10 13:07:34 +02:00
|
|
|
if err != nil {
|
2023-05-10 14:08:06 +02:00
|
|
|
return "", err
|
2023-05-10 13:07:34 +02:00
|
|
|
}
|
2023-05-10 14:08:06 +02:00
|
|
|
defer doc.Close()
|
2023-05-10 13:07:34 +02:00
|
|
|
|
2023-05-10 14:08:06 +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
|
|
|
}
|
|
|
|
|
2023-05-10 14:08:06 +02:00
|
|
|
return txt, nil
|
2023-05-10 13:07:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 00:49:15 +02:00
|
|
|
type model struct {
|
2023-05-10 13:07:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 13:07:34 +02:00
|
|
|
type datasheet struct {
|
|
|
|
filename string
|
|
|
|
absPath string
|
|
|
|
contents string
|
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 13:07:34 +02:00
|
|
|
var ds []datasheet
|
|
|
|
// 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)
|
|
|
|
d := datasheet{
|
|
|
|
filename: name,
|
|
|
|
absPath: path,
|
|
|
|
contents: contents,
|
|
|
|
}
|
|
|
|
ds = append(ds, d)
|
2023-05-10 09:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2023-05-10 09:53:08 +02:00
|
|
|
|
2023-05-10 15:14:36 +02:00
|
|
|
// TODO: set width/heigh to match terminal
|
2023-05-10 13:07:34 +02:00
|
|
|
viewp := viewport.New(60, 30)
|
|
|
|
viewp.SetContent(ds[len(ds)-1].contents)
|
|
|
|
|
|
|
|
m := model{
|
|
|
|
filterInput: input,
|
|
|
|
datasheets: ds,
|
|
|
|
dataSheetViewport: viewp,
|
2023-05-10 01:09:56 +02:00
|
|
|
}
|
2023-05-10 13:07:34 +02:00
|
|
|
m.dataSheetsView = m.dataSheetNames()
|
|
|
|
|
|
|
|
return m
|
2023-05-10 00:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
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 13:07:34 +02:00
|
|
|
var (
|
|
|
|
cmd tea.Cmd
|
|
|
|
cmds []tea.Cmd
|
|
|
|
)
|
2023-05-10 01:09:56 +02:00
|
|
|
|
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 13:07:34 +02:00
|
|
|
matches := fuzzy.Find(search, m.dataSheetNames())
|
2023-05-10 09:53:08 +02:00
|
|
|
for _, match := range matches {
|
|
|
|
matched = append(matched, match.Str)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
m.dataSheetsView = matched
|
|
|
|
} else {
|
2023-05-10 13:07:34 +02:00
|
|
|
m.dataSheetsView = m.dataSheetNames()
|
2023-05-10 01:36:11 +02:00
|
|
|
}
|
2023-05-10 09:53:08 +02:00
|
|
|
} else {
|
2023-05-10 13:07:34 +02:00
|
|
|
m.dataSheetsView = m.dataSheetNames()
|
2023-05-10 01:36:11 +02:00
|
|
|
}
|
2023-05-10 14:35:13 +02:00
|
|
|
|
2023-05-10 15:14:36 +02:00
|
|
|
// 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 (closed to input)
|
2023-05-10 14:35:13 +02:00
|
|
|
lastDatasheet := m.dataSheetsView[len(m.dataSheetsView)-1]
|
|
|
|
viewportText := m.datasheetFromName(lastDatasheet)
|
|
|
|
m.dataSheetViewport.SetContent(viewportText)
|
2023-05-10 01:36:11 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:14:36 +02:00
|
|
|
// TODO: handle terminal resizing
|
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)
|
2023-05-10 13:07:34 +02:00
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
|
|
m.dataSheetViewport, cmd = m.dataSheetViewport.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
2023-05-10 01:09:56 +02:00
|
|
|
|
2023-05-10 13:07:34 +02:00
|
|
|
return m, tea.Batch(cmds...)
|
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 13:07:34 +02:00
|
|
|
// 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)
|
2023-05-10 01:36:11 +02:00
|
|
|
|
2023-05-10 13:07:34 +02:00
|
|
|
body.WriteString("\n" + m.filterInput.View())
|
2023-05-10 01:36:11 +02:00
|
|
|
|
|
|
|
return body.String()
|
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 {
|
|
|
|
fmt.Println("fatal:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
p := tea.NewProgram(
|
|
|
|
initialModel(),
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|