Skip to content

Commit

Permalink
Enable sorting of snippets via settings and allow sorting case-insens…
Browse files Browse the repository at this point in the history
…itively
  • Loading branch information
pbek committed Nov 25, 2021
1 parent a9fed09 commit b83b85e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# QOwnNotes command-line snippet manager changelog

## v0.3.0
- Enable sorting of snippets via settings and allow sorting case-insensitively

## v0.2.0

- Cache snippets in case QOwnNotes is not running
Expand Down
22 changes: 14 additions & 8 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package snippet
import (
"bytes"
"sort"
"strings"

"github.com/qownnotes/qc/config"
"github.com/qownnotes/qc/entity"
"github.com/qownnotes/qc/websocket"
)
Expand Down Expand Up @@ -44,8 +46,8 @@ func (snippets *Snippets) ToString() (string, error) {
// Order snippets regarding SortBy option defined in config toml
// Prefix "-" reverses the order, default is "recency", "+<expressions>" is the same as "<expression>"
func (snippets *Snippets) Order() {
//sortBy := config.Conf.General.SortBy
sortBy := "command"
sortBy := config.Conf.General.SortBy

switch {
case sortBy == "command" || sortBy == "+command":
sort.Sort(ByCommand(snippets.Snippets))
Expand Down Expand Up @@ -75,15 +77,19 @@ func (snippets *Snippets) reverse() {

type ByCommand []entity.SnippetInfo

func (a ByCommand) Len() int { return len(a) }
func (a ByCommand) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCommand) Less(i, j int) bool { return a[i].Command > a[j].Command }
func (a ByCommand) Len() int { return len(a) }
func (a ByCommand) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCommand) Less(i, j int) bool {
return strings.ToLower(a[i].Command) > strings.ToLower(a[j].Command)
}

type ByDescription []entity.SnippetInfo

func (a ByDescription) Len() int { return len(a) }
func (a ByDescription) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDescription) Less(i, j int) bool { return a[i].Description > a[j].Description }
func (a ByDescription) Len() int { return len(a) }
func (a ByDescription) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDescription) Less(i, j int) bool {
return strings.ToLower(a[i].Description) > strings.ToLower(a[j].Description)
}

type ByOutput []entity.SnippetInfo

Expand Down

0 comments on commit b83b85e

Please sign in to comment.