diff --git a/ui/markdown.go b/ui/markdown.go new file mode 100644 index 0000000..f8ca91c --- /dev/null +++ b/ui/markdown.go @@ -0,0 +1,64 @@ +package ui + +import ( + "strings" + + "github.com/charmbracelet/charm" +) + +// markdownType allows us to differentiate between the types of markdown +// documents we're dealing with. +type markdownType int + +const ( + stashedMarkdown markdownType = iota + newsMarkdown + localMarkdown + convertedMarkdown // used to be local, now its stashed +) + +// markdown wraps charm.Markdown. +type markdown struct { + markdownType markdownType + localPath string // only relevant to local files and converted files that are newly stashed + displayPath string // what we show in the note field + charm.Markdown +} + +// sortAsLocal returns whether or not this markdown should be sorted as though +// it's a local markdown document. +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 { + iIsLocal := m[i].sortAsLocal() + jIsLocal := m[j].sortAsLocal() + + // Local files (and files that used to be local) come first + if iIsLocal && !jIsLocal { + return true + } + 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 iIsLocal && jIsLocal { + return strings.Compare(m[i].localPath, m[j].localPath) == -1 + } + + // Neither are local files so sort by date descending + if !m[i].CreatedAt.Equal(m[j].CreatedAt) { + return m[i].CreatedAt.After(m[j].CreatedAt) + } + + // If the timestamps also match, sort by ID. + return m[i].ID > m[j].ID +} diff --git a/ui/stash.go b/ui/stash.go index a69ea69..6e379bd 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -43,62 +43,6 @@ type deletedStashedItemMsg int // MODEL -// markdownType allows us to differentiate between the types of markdown -// documents we're dealing with, namely stuff the user stashed versus news. -type markdownType int - -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 relevant to local files and converted files that are newly stashed - displayPath string // what we show in the note field - 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 { - iIsLocal := m[i].sortAsLocal() - jIsLocal := m[j].sortAsLocal() - - // Local files (and files that used to be local) come first - if iIsLocal && !jIsLocal { - return true - } - 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 iIsLocal && jIsLocal { - return strings.Compare(m[i].localPath, m[j].localPath) == -1 - } - - // Neither are local files so sort by date descending - if !m[i].CreatedAt.Equal(m[j].CreatedAt) { - return m[i].CreatedAt.After(m[j].CreatedAt) - } - - // If the timestamps also match, sort by ID. - return m[i].ID > m[j].ID -} - type loadedState byte const (