mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-20 07:04:17 +02:00
Sort stash by local files first, then date
This commit is contained in:
parent
e0bd2dd466
commit
58abda8cf7
1 changed files with 20 additions and 7 deletions
27
ui/stash.go
27
ui/stash.go
|
|
@ -56,13 +56,26 @@ type markdown struct {
|
|||
*charm.Markdown
|
||||
}
|
||||
|
||||
// Sort documents by date in descending order
|
||||
type markdownsByCreatedAtDesc []*markdown
|
||||
// Sort documents with local files first, then by date
|
||||
type markdownsByLocalFirst []*markdown
|
||||
|
||||
// Sort implementation for MarkdownByCreatedAt
|
||||
func (m markdownsByCreatedAtDesc) Len() int { return len(m) }
|
||||
func (m markdownsByCreatedAtDesc) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
|
||||
func (m markdownsByCreatedAtDesc) Less(i, j int) bool { return m[i].CreatedAt.After(*m[j].CreatedAt) }
|
||||
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
|
||||
|
||||
// Local files come first
|
||||
if iType == localFile && jType != localFile {
|
||||
return true
|
||||
}
|
||||
if iType != localFile && jType == localFile {
|
||||
return false
|
||||
}
|
||||
|
||||
// Both or neither are local files, so sort by date descending
|
||||
return m[i].CreatedAt.After(*m[j].CreatedAt)
|
||||
}
|
||||
|
||||
type stashState int
|
||||
|
||||
|
|
@ -146,7 +159,7 @@ func (m stashModel) selectedMarkdown() *markdown {
|
|||
// addDocuments adds markdown documents to the model
|
||||
func (m *stashModel) addMarkdowns(mds ...*markdown) {
|
||||
m.markdowns = append(m.markdowns, mds...)
|
||||
sort.Sort(markdownsByCreatedAtDesc(m.markdowns))
|
||||
sort.Sort(markdownsByLocalFirst(m.markdowns))
|
||||
m.paginator.SetTotalPages(len(m.markdowns))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue