Skip to content

Conversation

@cameron-michie
Copy link

@cameron-michie cameron-michie commented May 13, 2025

Describe your changes

Adds the ability to press 'y' to toggle the sort by date. Also adds the ability to press 't' to switch back to sorting by title, and can sort reverse alphabetically by pressing repeatedly. These options now appear in the fullView after pressing '?'.

Related issue/discussion: #228

@cameron-michie cameron-michie requested a review from a team as a code owner May 13, 2025 12:25
@cameron-michie cameron-michie requested review from raphamorim and removed request for a team May 13, 2025 12:25
Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cameron-michie,

I made a small suggestion.

Comment on lines +16 to +38
func sortMarkdowns(mds []*markdown, sortByDate bool, ascending sortOrder) {
if sortByDate {
if ascending {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return compareTime(a.Modtime, b.Modtime)
})
} else {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return -compareTime(a.Modtime, b.Modtime)
})
}
} else {
if ascending {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return cmp.Compare(a.Note, b.Note)
})
} else {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return -cmp.Compare(a.Note, b.Note)
})
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions:

  1. Instead of repeating slices.SortStableFunc 4 times, assigning functions and calling in the end (make it a one-liner).
  2. Change if/else into a switch case to reduce block inside block.
Suggested change
func sortMarkdowns(mds []*markdown, sortByDate bool, ascending sortOrder) {
if sortByDate {
if ascending {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return compareTime(a.Modtime, b.Modtime)
})
} else {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return -compareTime(a.Modtime, b.Modtime)
})
}
} else {
if ascending {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return cmp.Compare(a.Note, b.Note)
})
} else {
slices.SortStableFunc(mds, func(a, b *markdown) int {
return -cmp.Compare(a.Note, b.Note)
})
}
}
}
func sortMarkdowns(mds []*markdown, sortByDate bool, order sortOrder) {
var fn func(a, b *markdown) int
switch {
case sortByDate && order == ascending:
fn = func(a, b *markdown) int { return compareTime(a.Modtime, b.Modtime) }
case sortByDate && order == descending:
fn = func(a, b *markdown) int { return -compareTime(a.Modtime, b.Modtime) }
case !sortByDate && order == ascending:
fn = func(a, b *markdown) int { return cmp.Compare(a.Note, b.Note) }
case !sortByDate && order == descending:
fn = func(a, b *markdown) int { return -cmp.Compare(a.Note, b.Note) }
}
slices.SortStableFunc(mds, fn)
}

gustavosbarreto pushed a commit to drera-labs/glow that referenced this pull request Jun 9, 2025
…charmbracelet#765)

Bumps [github.com/charmbracelet/x/ansi](https://github.com/charmbracelet/x) from 0.5.2 to 0.6.0.
- [Release notes](https://github.com/charmbracelet/x/releases)
- [Commits](charmbracelet/x@ansi/v0.5.2...ansi/v0.6.0)

---
updated-dependencies:
- dependency-name: github.com/charmbracelet/x/ansi
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Different sorting options

2 participants