feat: filter mode switcher

This commit is contained in:
decentral1se 2023-05-10 18:51:32 +02:00
parent 4307d4e117
commit 3628337a9a
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410

View File

@ -16,6 +16,12 @@ import (
"github.com/sahilm/fuzzy"
)
// filenameFilterMode searches by PDF file name.
const filenameFilterMode = "filename"
// contentFilterMode searches by PDF contents.
const contentFilterMode = "content"
// help is the command-line interface help output.
const help = `go-sh-manymanuals: TODO
@ -67,6 +73,8 @@ type model struct {
filteredDatasheets []string // Filtered view on all datasheets
datasheetViewport viewport.Model // Viewport for the PDF content
filterMode string // The filtering mode ("filename", "content")
}
// datasheetFromName retrieves a datasheet via a name.
@ -79,6 +87,15 @@ func (m model) datasheetFromName(name string) string {
return ""
}
// toggleFilterMode toggles the filter mode.
func (m *model) toggleFilterMode() {
if m.filterMode == filenameFilterMode {
m.filterMode = contentFilterMode
} else {
m.filterMode = filenameFilterMode
}
}
// datasheet represents a datasheet on disk.
type datasheet struct {
filename string // The name of the file
@ -132,6 +149,7 @@ func initialModel() model {
datasheetNames: datasheetNames,
filteredDatasheets: datasheetNames,
datasheetViewport: viewp,
filterMode: filenameFilterMode,
}
return m
@ -182,6 +200,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "tab":
m.toggleFilterMode()
}
}
@ -208,6 +228,18 @@ func (m model) View() string {
body.WriteString("\n" + m.input.View())
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)
help := "[ctrl-c]: quit | [tab]: filter mode"
body.WriteString("\n" + help)
return body.String()
}