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