From 86e581c79ca7069b3884dd0d72a30f4ae3f90fd6 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 19 Nov 2020 22:26:31 -0500 Subject: [PATCH] Add matching stash/news documents to filter results as they're loaded --- ui/markdown.go | 18 ++++++++++++++++++ ui/stash.go | 41 ++++++++++++++++++++++++----------------- ui/ui.go | 1 + 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/ui/markdown.go b/ui/markdown.go index 1e7e33d..8e249c9 100644 --- a/ui/markdown.go +++ b/ui/markdown.go @@ -1,6 +1,7 @@ package ui import ( + "log" "strings" "github.com/charmbracelet/charm" @@ -33,6 +34,23 @@ type markdown struct { charm.Markdown } +func (m *markdown) buildFilterValue() { + note, err := normalize(m.Note) + if err != nil { + if debug { + log.Printf("error normalizing '%s': %v", m.Note, err) + } + m.filterValue = m.Note + } + + if m.markdownType == newsMarkdown { + m.filterValue = "News: " + note + return + } + + m.filterValue = note +} + // sortAsLocal returns whether or not this markdown should be sorted as though // it's a local markdown document. func (m markdown) sortAsLocal() bool { diff --git a/ui/stash.go b/ui/stash.go index d96844b..dcaf507 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -365,17 +365,17 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { m.err = msg.err m.loaded |= NewsDocuments // still done, albeit unsuccessfully - // We're finished searching for local files case localFileSearchFinished: + // We're finished searching for local files m.loaded |= LocalDocuments - // Stash results have come in from the server case gotStashMsg: + // Stash results have come in from the server. + // // This doesn't mean the whole stash listing is loaded, but some we've // finished checking for the stash, at least, so mark the stash as // loaded here. m.loaded |= StashedDocuments - m.loadingFromNetwork = false if len(msg) == 0 { @@ -383,14 +383,33 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { m.stashFullyLoaded = true } else { docs := wrapMarkdowns(stashedMarkdown, msg) + + // If we're filtering build filter indexes immediately so any + // matching results will show up in the filter. + if m.state == stashStateFilterNotes || m.state == stashStateShowFiltered { + for _, md := range docs { + md.buildFilterValue() + } + } + m.addMarkdowns(docs...) } - // News has come in from the server case gotNewsMsg: + // News has come in from the server m.loaded |= NewsDocuments + if len(msg) > 0 { docs := wrapMarkdowns(newsMarkdown, msg) + + // If we're filtering build filter indexes immediately so any + // matching results will show up in the filter. + if m.state == stashStateFilterNotes || m.state == stashStateShowFiltered { + for _, md := range docs { + md.buildFilterValue() + } + } + m.addMarkdowns(docs...) } @@ -487,19 +506,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { // Build values we'll filter against for _, md := range m.markdowns { - note, err := normalize(md.Note) - if err != nil { - if debug { - log.Printf("error normalizing '%s': %v", md.Note, err) - } - md.filterValue = md.Note - continue - } - if md.markdownType == newsMarkdown { - md.filterValue = "News: " + note - continue - } - md.filterValue = note + md.buildFilterValue() } m.paginator.Page = 0 diff --git a/ui/ui.go b/ui/ui.go index 683dff2..ce432b0 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -373,6 +373,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.general.authStatus = authFailed case fetchedMarkdownMsg: + // We've loaded a markdown file's contents for rendering m.pager.currentDocument = *msg msg.Body = string(utils.RemoveFrontmatter([]byte(msg.Body))) cmds = append(cmds, renderWithGlamour(m.pager, msg.Body))