diff --git a/ui/pager.go b/ui/pager.go index 902c03d..e28d2eb 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -283,7 +283,6 @@ func pagerUpdate(msg tea.Msg, m pagerModel) (pagerModel, tea.Cmd) { // that of a stashed document, but don't re-render since the body is // identical to what's already rendered. m.currentDocument = markdown(msg) - m.currentDocument.localPath = "" // Show a success message to the user. m.state = pagerStateStatusMessage @@ -513,13 +512,16 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd { if len(md.Body) == 0 { data, err := ioutil.ReadFile(md.localPath) if err != nil { + if debug { + log.Println("error loading doucument body for stashing:", err) + } return stashErrMsg{err} } md.Body = string(data) } - // Turn local markdown into a stashed markdown - md.markdownType = stashedMarkdown + // Turn local markdown into a newly stashed (converted) markdown + md.markdownType = convertedMarkdown md.CreatedAt = time.Now() // Set the note as the filename without the extension diff --git a/ui/stash.go b/ui/stash.go index 4424edd..ef7651a 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -51,36 +51,41 @@ const ( stashedMarkdown markdownType = iota newsMarkdown localMarkdown + convertedMarkdown // used to be local, now its stashed ) // markdown wraps charm.Markdown so we can differentiate between stashed items // and news. type markdown struct { markdownType markdownType - localPath string // only relevent to local files + localPath string // only relevent to local files and converted files that are newly stashed charm.Markdown } +func (m markdown) sortAsLocal() bool { + return m.markdownType == localMarkdown || m.markdownType == convertedMarkdown +} + // Sort documents with local files first, then by date type markdownsByLocalFirst []*markdown func (m markdownsByLocalFirst) Len() int { return len(m) } func (m markdownsByLocalFirst) Swap(i, j int) { m[i], m[j] = m[j], m[i] } func (m markdownsByLocalFirst) Less(i, j int) bool { - iType := m[i].markdownType - jType := m[j].markdownType + iIsLocal := m[i].sortAsLocal() + jIsLocal := m[j].sortAsLocal() - // Local files come first - if iType == localMarkdown && jType != localMarkdown { + // Local files (and files that used to be local) come first + if iIsLocal && !jIsLocal { return true } - if iType != localMarkdown && jType == localMarkdown { + if !iIsLocal && jIsLocal { return false } // If both are local files, sort by filename. Note that we should never // hit equality here since two files can't have the same path. - if iType == localMarkdown && jType == localMarkdown { + if iIsLocal && jIsLocal { return strings.Compare(m[i].localPath, m[j].localPath) == -1 } @@ -191,6 +196,34 @@ func (m *stashModel) addMarkdowns(mds ...*markdown) { } } +// find a local markdown by its path and remove it +func (m *stashModel) removeLocalMarkdown(localPath string) error { + i := -1 + + // Look for local markdown + for j, doc := range m.markdowns { + if doc.localPath == localPath { + i = j + break + } + } + + // Did we find it? + if i == -1 { + err := fmt.Errorf("could't find local markdown %s; not removing from stash", localPath) + if debug { + log.Println(err) + } + return err + } + + // Slice out markdown + if i >= 0 { + m.markdowns = append(m.markdowns[:i], m.markdowns[i+1:]...) + } + return nil +} + // return the number of markdown documents of a given type func (m stashModel) countMarkdowns(t markdownType) (found int) { if len(m.markdowns) == 0 { @@ -314,7 +347,8 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { case stashSuccessMsg: md := markdown(msg) delete(m.filesStashing, md.localPath) // remove from the things-we're-stashing list - md.localPath = "" + + _ = m.removeLocalMarkdown(md.localPath) m.addMarkdowns(&md) m.showStatusMessage = true @@ -410,7 +444,10 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { // if we're busy stashing this don't do it again _, exists := m.filesStashing[md.localPath] - if exists || md.markdownType != localMarkdown || md.localPath == "" { + if exists || (md.markdownType != localMarkdown) || md.localPath == "" { + if md.localPath == "" { + log.Printf("refusing to load markdown; local path is empty: %#v", md) + } break } @@ -421,7 +458,8 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { case "x": m.hideStatusMessage() - isUserMarkdown := m.selectedMarkdown().markdownType == stashedMarkdown + t := m.selectedMarkdown().markdownType + isUserMarkdown := t == stashedMarkdown || t == convertedMarkdown isValidState := m.state != stashStateSettingNote if isUserMarkdown && isValidState { @@ -775,7 +813,7 @@ func loadRemoteMarkdown(cc *charm.Client, id int, t markdownType) tea.Cmd { err error ) - if t == stashedMarkdown { + if t == stashedMarkdown || t == convertedMarkdown { md, err = cc.GetStashMarkdown(id) } else { md, err = cc.GetNewsMarkdown(id) diff --git a/ui/stashitem.go b/ui/stashitem.go index a488392..8d32b6b 100644 --- a/ui/stashitem.go +++ b/ui/stashitem.go @@ -46,7 +46,7 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) { } else { title = newsPrefix + truncate(title, truncateTo-rw.StringWidth(newsPrefix)) } - case stashedMarkdown: + case stashedMarkdown, convertedMarkdown: icon = stashIcon icon = stashIcon if title == "" { diff --git a/ui/ui.go b/ui/ui.go index 89b0d74..d5914f9 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -340,6 +340,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { // message and generally don't want any other effects. if m.state == stateShowDocument { md := markdown(msg) + _ = m.stash.removeLocalMarkdown(md.localPath) m.stash.addMarkdowns(&md) } }