Skip to content

Commit

Permalink
feat(tasks): add multiple sort options
Browse files Browse the repository at this point in the history
* fix: fix issue with different ordering
* feat: add sort by due date and title
* release: add goreleaser
* feat: move version string to build ldflag
  • Loading branch information
BRO3886 committed Jan 21, 2022
1 parent 8356a70 commit 853887c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ google-tasks-cli
gtasks
gtasks.exe
bin/*

dist/
40 changes: 40 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
project_name: gtasks

release:
prerelease: auto
draft: true
name_template: "gtasks {{.Version}}"

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
# - go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
ldflags:
- -X github.com/BRO3886/gtasks/cmd.Version={{.Version}}
archives:
- replacements:
darwin: macOS
linux: Linux
windows: Windows
386: i386
amd64: x86_64

checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ linux:
go build -o ./bin/linux/gtasks
@ cd bin/linux
gtasks
mac:
@echo "Building for mac"
GOOS=darwin GOARCH=amd64 go build -o ./bin/mac/gtasks
@ cd bin/mac
gtasks
all:
@echo "Building for every OS and Platform"
GOOS=windows GOARCH=386 GO386=softfloat go build -o ./bin/windows/gtasks.exe
Expand Down
8 changes: 8 additions & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func GetTasks(srv *tasks.Service, id string, includeCompleted bool) ([]*tasks.Ta
}
}

func MakeMap(taskList []*tasks.Task) map[string]tasks.Task {
m := make(map[string]tasks.Task)
for _, t := range taskList {
m[t.Id] = *t
}
return m
}

// GetTaskInfo to get more info about a task
func GetTaskInfo(srv *tasks.Service, id string, taskID string) (*tasks.Task, error) {
r, err := srv.Tasks.Get(id, taskID).Do()
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (

var cfgFile string

// version is set during build
var Version = "DEV"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gtasks",
Short: "A CLI Tool for Google Tasks",
Version: "0.9.5",
Version: Version,
Long: `
A CLI Tool for managing your Google Tasks:
Expand Down
26 changes: 9 additions & 17 deletions cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,15 @@ var viewTasksCmd = &cobra.Command{
return
}

// sort tasks by date
sort.SliceStable(tasks, func(i, j int) bool {
if tasks[i].Due == "" {
return false
}

if tasks[j].Due == "" {
return true
}

return tasks[i].Due < tasks[j].Due
})
utils.Sort(tasks, viewTasksFlags.sort)

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"No", "Title", "Description", "Status", "Due"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.SetCenterSeparator("|")
// table.SetRowLine(true)
// table.SetRowSeparator("-")
table.SetRowLine(false)
table.SetRowSeparator("-")

for ind, task := range tasks {
if viewTasksFlags.onlyCompleted && task.Status == "needsAction" {
Expand All @@ -91,14 +81,14 @@ var viewTasksCmd = &cobra.Command{

due, err := time.Parse(time.RFC3339, task.Due)
if err != nil {
row = append(row, "No Due Date")
row = append(row, "-")
} else {
row = append(row, due.Local().Format("02 January 2006"))
}

table.Append(row)

}

table.Render()
},
}
Expand Down Expand Up @@ -228,6 +218,7 @@ var (
viewTasksFlags struct {
includeCompleted bool
onlyCompleted bool
sort string
}
taskListFlag string
addTaskFlags struct {
Expand All @@ -243,6 +234,7 @@ func init() {
createTaskCmd.Flags().StringVarP(&addTaskFlags.due, "due", "d", "", "use this flag to set a tasks due date")
viewTasksCmd.Flags().BoolVarP(&viewTasksFlags.includeCompleted, "include-completed", "i", false, "use this flag to include completed tasks")
viewTasksCmd.Flags().BoolVar(&viewTasksFlags.onlyCompleted, "completed", false, "use this flag to only show completed tasks")
viewTasksCmd.Flags().StringVar(&viewTasksFlags.sort, "sort", "position", "use this flag to sort by [due,title,position]")
tasksCmd.PersistentFlags().StringVarP(&taskListFlag, "tasklist", "l", "", "use this flag to specify a tasklist")
tasksCmd.AddCommand(viewTasksCmd, createTaskCmd, markCompletedCmd, deleteTaskCmd)
rootCmd.AddCommand(tasksCmd)
Expand Down
33 changes: 33 additions & 0 deletions internal/utils/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"sort"

"google.golang.org/api/tasks/v1"
)

func Sort(tasks []*tasks.Task, sortBy string) {
switch sortBy {
case "due":
sort.SliceStable(tasks, func(i, j int) bool {
if tasks[i].Due == "" {
return false
}

if tasks[j].Due == "" {
return true
}

return tasks[i].Due < tasks[j].Due
})
case "title":
sort.SliceStable(tasks, func(i, j int) bool {
return tasks[i].Title < tasks[j].Title
})
case "position":
default:
sort.SliceStable(tasks, func(i, j int) bool {
return tasks[i].Position < tasks[j].Position && tasks[i].Parent == ""
})
}
}

0 comments on commit 853887c

Please sign in to comment.