Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sorting by dates #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func traverseItems(item *todoist.Item, f func(item *todoist.Item, depth int), de
}
}

var byPriority int = 1

Choose a reason for hiding this comment

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

these could be iota instead of manually numbered

var byDate int = 2

func sortItems(itemListPtr *[][]string, byIndex int) {
itemList := *itemListPtr
length := len(itemList)
Expand Down Expand Up @@ -74,10 +77,20 @@ func List(c *cli.Context) error {
})
}, 0)

if c.Bool("priority") == true {
// sort output by priority
// and no need to use "else block" as items returned by API are already sorted by task id
sortItems(&itemList, 1)
switch c.String("sort") {
case "priority":
sortItems(&itemList, byPriority)
break
case "date":
sortItems(&itemList, byDate)
break
default:
// For backwards compatibility, can be removed when users have had time
// to migrate.
if c.Bool("priority") == true {
sortItems(&itemList, byPriority)
}

}

defer writer.Flush()
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func main() {
}
sortPriorityFlag := cli.BoolFlag{
Name: "priority, p",
Usage: "sort the output by priority",
Usage: "(Deprecated, prefer --sort)",
}
sortFlag := cli.StringFlag{
Name: "sort",
Usage: "sort the output by the desired parameter `(date|priority)`",
}
reminderFlg := cli.BoolFlag{
Name: "reminder, r",
Expand Down Expand Up @@ -207,6 +211,7 @@ func main() {
Flags: []cli.Flag{
filterFlag,
sortPriorityFlag,
sortFlag,
},
ArgsUsage: " ",
},
Expand Down