Goutil.go -rw-r--r-- 2.1 KiB
1package main
2
3import (
4 "fmt"
5 "os"
6 "os/user"
7 "path"
8 "time"
9
10 "github.com/fatih/color"
11)
12
13func maybe(err error) {
14 if err != nil {
15 fmt.Printf("Error:\n%s\n", err)
16 os.Exit(1)
17 }
18}
19
20func defaultConfigPath() string {
21 u, err := user.Current()
22 maybe(err)
23 return path.Join(u.HomeDir, "/.pomo/config.json")
24}
25
26func summerizeTasks(config *Config, tasks []*Task) {
27 for _, task := range tasks {
28 var start string
29 if len(task.Pomodoros) > 0 {
30 start = task.Pomodoros[0].Start.Format(config.DateTimeFmt)
31 }
32 fmt.Printf("%d: [%s] [%s] ", task.ID, start, task.Duration.Truncate(time.Second))
33 // a list of green/yellow/red pomodoros
34 // green indicates the pomodoro was finished normally
35 // yellow indicates the break was exceeded by +5minutes
36 // red indicates the pomodoro was never completed
37 fmt.Printf("[")
38 for i, pomodoro := range task.Pomodoros {
39 if i > 0 {
40 fmt.Printf(" ")
41 }
42 // pomodoro exceeded it's expected duration by more than 5m
43 if pomodoro.Duration() > task.Duration+5*time.Minute {
44 color.New(color.FgYellow).Printf("X")
45 } else {
46 // pomodoro completed normally
47 color.New(color.FgGreen).Printf("X")
48 }
49 }
50 // each missed pomodoro
51 for i := 0; i < task.NPomodoros-len(task.Pomodoros); i++ {
52 if i > 0 || i == 0 && len(task.Pomodoros) > 0 {
53 fmt.Printf(" ")
54 }
55 color.New(color.FgRed).Printf("X")
56 }
57 fmt.Printf("]")
58 // Tags
59 if len(task.Tags) > 0 {
60 fmt.Printf(" [")
61 for i, tag := range task.Tags {
62 if i > 0 && i != len(task.Tags) {
63 fmt.Printf(" ")
64 }
65 // user specified color mapping exists
66 if color := config.Colors.Get(tag); color != nil {
67 color.Printf("%s", tag)
68 } else {
69 // no color mapping
70 fmt.Printf("%s", tag)
71 }
72 }
73 fmt.Printf("]")
74 }
75 fmt.Printf(" - %s", task.Message)
76 fmt.Printf("\n")
77 }
78}
79
80func outputStatus(status Status) {
81 state := "?"
82 if status.State >= RUNNING {
83 state = string(status.State.String()[0])
84 }
85 if status.State == RUNNING {
86 fmt.Printf("%s [%d/%d] %s", state, status.Count, status.NPomodoros, status.Remaining)
87 } else {
88 fmt.Printf("%s [%d/%d] -", state, status.Count, status.NPomodoros)
89 }
90}