Add matching stash/news documents to filter results as they're loaded

This commit is contained in:
Christian Rocha 2020-11-19 22:26:31 -05:00
parent 53b9c7ee62
commit 86e581c79c
No known key found for this signature in database
GPG key ID: D6CC7A16E5878018
3 changed files with 43 additions and 17 deletions

View file

@ -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 {

View file

@ -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

View file

@ -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))