Browse Source

refactor: naming & caching of sheet names

main
decentral1se 1 year ago
parent
commit
d4a2d98189
No known key found for this signature in database GPG Key ID: 3789458B3D0C410
  1. 66
      gshmm.go

66
gshmm.go

@ -60,20 +60,13 @@ func readPDF(name string) (string, error) {
// model offers the core of the state for the entire UI. // model offers the core of the state for the entire UI.
type model struct { type model struct {
filterInput textinput.Model // Fuzzy search interface input textinput.Model // Fuzzy search interface
datasheets []datasheet // All datasheets under cwd
datasheetsView []string // Filtered view on all datasheets
datasheetViewport viewport.Model // Viewport for the PDF content
}
// datasheetNames lists all datasheet names. datasheets []datasheet // All datasheets under cwd
func (m model) datasheetNames() []string { datasheetNames []string // All datasheet names (caching)
// TODO: cache this somewhere, it's called several times... in the model? filteredDatasheets []string // Filtered view on all datasheets
var names []string
for _, datasheet := range m.datasheets { datasheetViewport viewport.Model // Viewport for the PDF content
names = append(names, datasheet.filename)
}
return names
} }
// datasheetFromName retrieves a datasheet via a name. // datasheetFromName retrieves a datasheet via a name.
@ -98,7 +91,9 @@ func initialModel() model {
input := textinput.New() input := textinput.New()
input.Focus() input.Focus()
var ds []datasheet var datasheets []datasheet
var datasheetNames []string
// TODO: handle error in interface? // TODO: handle error in interface?
_ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error { _ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@ -112,12 +107,15 @@ func initialModel() model {
// we could run this in a goroutine somewhere // we could run this in a goroutine somewhere
// this currently slows down startup time // this currently slows down startup time
contents, _ := readPDF(path) contents, _ := readPDF(path)
d := datasheet{
datasheet := datasheet{
filename: name, filename: name,
absPath: path, absPath: path,
contents: contents, contents: contents,
} }
ds = append(ds, d)
datasheets = append(datasheets, datasheet)
datasheetNames = append(datasheetNames, name)
} }
return nil return nil
@ -125,14 +123,16 @@ func initialModel() model {
// TODO: set width/heigh to match terminal // TODO: set width/heigh to match terminal
viewp := viewport.New(60, 30) viewp := viewport.New(60, 30)
viewp.SetContent(ds[len(ds)-1].contents) selectedDatasheet := datasheets[len(datasheets)-1].contents
viewp.SetContent(selectedDatasheet)
m := model{ m := model{
filterInput: input, input: input,
datasheets: ds, datasheets: datasheets,
datasheetViewport: viewp, datasheetNames: datasheetNames,
filteredDatasheets: datasheetNames,
datasheetViewport: viewp,
} }
m.datasheetsView = m.datasheetNames()
return m return m
} }
@ -149,29 +149,29 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds []tea.Cmd cmds []tea.Cmd
) )
if m.filterInput.Focused() { if m.input.Focused() {
var matched []string var matchedDatasheets []string
search := m.filterInput.Value() search := m.input.Value()
if len(search) >= minCharsUntilFilter { if len(search) >= minCharsUntilFilter {
matches := fuzzy.Find(search, m.datasheetNames()) matches := fuzzy.Find(search, m.datasheetNames)
for _, match := range matches { for _, match := range matches {
matched = append(matched, match.Str) matchedDatasheets = append(matchedDatasheets, match.Str)
} }
if len(matches) > 0 { if len(matches) > 0 {
m.datasheetsView = matched m.filteredDatasheets = matchedDatasheets
} else { } else {
m.datasheetsView = m.datasheetNames() m.filteredDatasheets = m.datasheetNames
} }
} else { } else {
m.datasheetsView = m.datasheetNames() m.filteredDatasheets = m.datasheetNames
} }
// TODO: implement cursor for scrolling up/down filtered // TODO: implement cursor for scrolling up/down filtered
// results so we can view the PDF contents as desired // results so we can view the PDF contents as desired
// it's currently just the last one (closest to input) // it's currently just the last one (closest to input)
lastdatasheet := m.datasheetsView[len(m.datasheetsView)-1] lastdatasheet := m.filteredDatasheets[len(m.filteredDatasheets)-1]
viewportText := m.datasheetFromName(lastdatasheet) viewportText := m.datasheetFromName(lastdatasheet)
m.datasheetViewport.SetContent(viewportText) m.datasheetViewport.SetContent(viewportText)
} }
@ -185,7 +185,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
} }
m.filterInput, cmd = m.filterInput.Update(msg) m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
m.datasheetViewport, cmd = m.datasheetViewport.Update(msg) m.datasheetViewport, cmd = m.datasheetViewport.Update(msg)
@ -199,14 +199,14 @@ func (m model) View() string {
body := strings.Builder{} body := strings.Builder{}
// TODO: paginate / trim view to last 10 or something? // TODO: paginate / trim view to last 10 or something?
sheets := strings.Join(m.datasheetsView, "\n") sheets := strings.Join(m.filteredDatasheets, "\n")
// TODO: style further with lipgloss, e.g. borders, margins, etc. // TODO: style further with lipgloss, e.g. borders, margins, etc.
panes := lipgloss.JoinHorizontal(lipgloss.Left, sheets, m.datasheetViewport.View()) panes := lipgloss.JoinHorizontal(lipgloss.Left, sheets, m.datasheetViewport.View())
body.WriteString(panes) body.WriteString(panes)
body.WriteString("\n" + m.filterInput.View()) body.WriteString("\n" + m.input.View())
return body.String() return body.String()
} }

Loading…
Cancel
Save