Moving Task listing from ListCmd to Task and Adding Shell Completion for Task

This commit is contained in:
Martin (Schretzi) Fuchsluger
2025-02-14 22:44:33 +01:00
parent d5c41a921a
commit fa86b0f64f
6 changed files with 112 additions and 52 deletions
+8
View File
@@ -72,4 +72,12 @@ func init() {
entryCmd.Flags().StringVarP(&notes, "notes", "n", "", "Update activity notes")
entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task")
entryCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
flagName := "task"
entryCmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
user := GetCurrentUser()
entries, _ := database.ListEntries(user)
_, tasks := listProjectsAndTasks(entries)
return tasks, cobra.ShellCompDirectiveDefault
})
}
+8
View File
@@ -83,4 +83,12 @@ func init() {
exportCmd.Flags().StringVar(&listRange, "range", "", "shortcut to set since/until for a given range (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)")
exportCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be exported")
exportCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be exported")
flagName := "task"
exportCmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
user := GetCurrentUser()
entries, _ := database.ListEntries(user)
_, tasks := listProjectsAndTasks(entries)
return tasks, cobra.ShellCompDirectiveDefault
})
}
+8
View File
@@ -105,4 +105,12 @@ func init() {
finishCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be assigned")
finishCmd.Flags().StringVarP(&notes, "notes", "n", "", "Activity notes")
finishCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned")
flagName := "task"
finishCmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
user := GetCurrentUser()
entries, _ := database.ListEntries(user)
_, tasks := listProjectsAndTasks(entries)
return tasks, cobra.ShellCompDirectiveDefault
})
}
+9 -52
View File
@@ -2,7 +2,6 @@ package z
import (
"fmt"
"os"
"github.com/shopspring/decimal"
"github.com/spf13/cobra"
@@ -19,58 +18,8 @@ var listCmd = &cobra.Command{
Short: "List activities",
Long: "List all tracked activities.",
Run: func(cmd *cobra.Command, args []string) {
user := GetCurrentUser()
entries, err := database.ListEntries(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
sinceTime, untilTime := ParseSinceUntil(since, until, listRange)
var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
if listOnlyProjectsAndTasks == true || listOnlyTasks == true {
var projectsAndTasks = make(map[string]map[string]bool)
for _, filteredEntry := range filteredEntries {
taskMap, ok := projectsAndTasks[filteredEntry.Project]
if !ok {
taskMap = make(map[string]bool)
projectsAndTasks[filteredEntry.Project] = taskMap
}
taskMap[filteredEntry.Task] = true
projectsAndTasks[filteredEntry.Project] = taskMap
}
for project, _ := range projectsAndTasks {
if listOnlyProjectsAndTasks == true && listOnlyTasks == false {
fmt.Printf("%s %s\n", CharMore, project)
}
for task, _ := range projectsAndTasks[project] {
if listOnlyProjectsAndTasks == true && listOnlyTasks == false {
fmt.Printf("%*s└── ", 1, " ")
}
if appendProjectIDToTask == true {
fmt.Printf("%s [%s]\n", task, project)
} else {
fmt.Printf("%s\n", task)
}
}
}
return
}
filteredEntries := listEntries()
totalHours := decimal.NewFromInt(0)
for _, entry := range filteredEntries {
@@ -97,4 +46,12 @@ func init() {
listCmd.Flags().BoolVar(&listOnlyProjectsAndTasks, "only-projects-and-tasks", false, "Only list projects and their tasks, no entries")
listCmd.Flags().BoolVar(&listOnlyTasks, "only-tasks", false, "Only list tasks, no projects nor entries")
listCmd.Flags().BoolVar(&appendProjectIDToTask, "append-project-id-to-task", false, "Append project ID to tasks in the list")
flagName := "task"
listCmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
user := GetCurrentUser()
entries, _ := database.ListEntries(user)
_, tasks := listProjectsAndTasks(entries)
return tasks, cobra.ShellCompDirectiveDefault
})
}
+71
View File
@@ -1,9 +1,80 @@
package z
import (
"fmt"
"os"
)
type Task struct {
Name string `json:"name,omitempty"`
GitRepository string `json:"gitRepository,omitempty"`
}
func listEntries() []Entry {
user := GetCurrentUser()
entries, err := database.ListEntries(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
sinceTime, untilTime := ParseSinceUntil(since, until, listRange)
var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
if listOnlyProjectsAndTasks || listOnlyTasks {
printProjects(filteredEntries)
return nil
}
return filteredEntries
}
func printProjects(entries []Entry) {
projectsAndTasks, _ := listProjectsAndTasks(entries)
for project := range projectsAndTasks {
if listOnlyProjectsAndTasks && !listOnlyTasks {
fmt.Printf("%s %s\n", CharMore, project)
}
for task := range projectsAndTasks[project] {
if listOnlyProjectsAndTasks && !listOnlyTasks {
fmt.Printf("%*s└── ", 1, " ")
}
if appendProjectIDToTask {
fmt.Printf("%s [%s]\n", task, project)
} else {
fmt.Printf("%s\n", task)
}
}
}
}
func listProjectsAndTasks(entries []Entry) (map[string]map[string]bool, []string) {
var projectsAndTasks = make(map[string]map[string]bool)
var allTasks []string
for _, filteredEntry := range entries {
taskMap, ok := projectsAndTasks[filteredEntry.Project]
if !ok {
taskMap = make(map[string]bool)
projectsAndTasks[filteredEntry.Project] = taskMap
}
taskMap[filteredEntry.Task] = true
projectsAndTasks[filteredEntry.Project] = taskMap
allTasks = append(allTasks, filteredEntry.Task)
}
return projectsAndTasks, allTasks
}
+8
View File
@@ -70,4 +70,12 @@ func init() {
trackCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned")
trackCmd.Flags().StringVarP(&notes, "notes", "n", "", "Activity notes")
trackCmd.Flags().BoolVarP(&force, "force", "f", false, "Force begin tracking of a new task \neven though another one is still running \n(ONLY IF YOU KNOW WHAT YOU'RE DOING!)")
flagName := "task"
trackCmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
user := GetCurrentUser()
entries, _ := database.ListEntries(user)
_, tasks := listProjectsAndTasks(entries)
return tasks, cobra.ShellCompDirectiveDefault
})
}