|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Sanjays2402/tsk/internal/model" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +type lsFilters struct { |
| 15 | + done, all, today, overdue, upcoming bool |
| 16 | + tag string |
| 17 | + priorityStr string |
| 18 | + asJSON bool |
| 19 | +} |
| 20 | + |
| 21 | +func newLsCmd() *cobra.Command { |
| 22 | + f := lsFilters{} |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "ls", |
| 25 | + Aliases: []string{"list"}, |
| 26 | + Short: "List tasks (undone by default)", |
| 27 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 28 | + s, err := resolveStore(cmd, true) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + tasks, err := applyFilters(s.Tasks, f) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + return printTasks(cmd.OutOrStdout(), tasks, f.asJSON) |
| 37 | + }, |
| 38 | + } |
| 39 | + cmd.Flags().BoolVar(&f.done, "done", false, "only show done tasks") |
| 40 | + cmd.Flags().BoolVar(&f.all, "all", false, "show all tasks (done + undone)") |
| 41 | + cmd.Flags().BoolVar(&f.today, "today", false, "only show tasks due today") |
| 42 | + cmd.Flags().BoolVar(&f.overdue, "overdue", false, "only show overdue tasks") |
| 43 | + cmd.Flags().BoolVar(&f.upcoming, "upcoming", false, "only show tasks due in the future") |
| 44 | + cmd.Flags().StringVar(&f.tag, "tag", "", "only show tasks with this tag") |
| 45 | + cmd.Flags().StringVar(&f.priorityStr, "priority", "", "only show tasks with this priority") |
| 46 | + cmd.Flags().BoolVar(&f.asJSON, "json", false, "emit JSON") |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +func applyFilters(in []model.Task, f lsFilters) ([]model.Task, error) { |
| 51 | + var prio model.Priority |
| 52 | + prioFilter := false |
| 53 | + if f.priorityStr != "" { |
| 54 | + p, err := model.ParsePriority(f.priorityStr) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + prio = p |
| 59 | + prioFilter = true |
| 60 | + } |
| 61 | + now := time.Now() |
| 62 | + out := make([]model.Task, 0, len(in)) |
| 63 | + for _, t := range in { |
| 64 | + switch { |
| 65 | + case f.all: |
| 66 | + // everything |
| 67 | + case f.done: |
| 68 | + if !t.Done { |
| 69 | + continue |
| 70 | + } |
| 71 | + default: |
| 72 | + if t.Done { |
| 73 | + continue |
| 74 | + } |
| 75 | + } |
| 76 | + if f.today && !t.IsDueToday(now) { |
| 77 | + continue |
| 78 | + } |
| 79 | + if f.overdue && !t.IsOverdue(now) { |
| 80 | + continue |
| 81 | + } |
| 82 | + if f.upcoming && !t.IsUpcoming(now) { |
| 83 | + continue |
| 84 | + } |
| 85 | + if f.tag != "" && !t.HasTag(f.tag) { |
| 86 | + continue |
| 87 | + } |
| 88 | + if prioFilter && t.Priority != prio { |
| 89 | + continue |
| 90 | + } |
| 91 | + out = append(out, t) |
| 92 | + } |
| 93 | + return out, nil |
| 94 | +} |
| 95 | + |
| 96 | +func printTasks(w io.Writer, tasks []model.Task, asJSON bool) error { |
| 97 | + if asJSON { |
| 98 | + enc := json.NewEncoder(w) |
| 99 | + enc.SetIndent("", " ") |
| 100 | + return enc.Encode(tasks) |
| 101 | + } |
| 102 | + if len(tasks) == 0 { |
| 103 | + fmt.Fprintln(w, "no tasks") |
| 104 | + return nil |
| 105 | + } |
| 106 | + for _, t := range tasks { |
| 107 | + check := " " |
| 108 | + if t.Done { |
| 109 | + check = "x" |
| 110 | + } |
| 111 | + line := fmt.Sprintf("[%s] #%d %s (%s)", check, t.ID, t.Title, t.Priority) |
| 112 | + if t.Due != nil { |
| 113 | + line += " due:" + t.Due.Format(model.DateLayout) |
| 114 | + } |
| 115 | + if len(t.Tags) > 0 { |
| 116 | + line += " #" + strings.Join(t.Tags, " #") |
| 117 | + } |
| 118 | + fmt.Fprintln(w, line) |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
0 commit comments