-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdefault.go
186 lines (154 loc) · 4.15 KB
/
default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package timeentry
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/lucassabreu/clockify-cli/api/dto"
"github.com/lucassabreu/clockify-cli/pkg/output/util"
"github.com/olekukonko/tablewriter"
"golang.org/x/term"
)
func sumTimeEntriesDuration(ts []dto.TimeEntry) time.Duration {
s := time.Duration(0)
for i := 0; i < len(ts); i++ {
end := time.Now()
if ts[i].TimeInterval.End != nil {
end = *ts[i].TimeInterval.End
}
d := end.Sub(ts[i].TimeInterval.Start)
s = s + d
}
return s
}
const (
TimeFormatFull = "2006-01-02 15:04:05"
TimeFormatSimple = "15:04:05"
)
// TimeEntryOutputOptions sets how the "table" format should print the time
// entries
type TimeEntryOutputOptions struct {
ShowTasks bool
ShowClients bool
ShowTotalDuration bool
TimeFormat string
}
// NewTimeEntryOutputOptions creates a default TimeEntryOutputOptions
func NewTimeEntryOutputOptions() TimeEntryOutputOptions {
return TimeEntryOutputOptions{
TimeFormat: TimeFormatSimple,
ShowTasks: false,
ShowClients: false,
ShowTotalDuration: false,
}
}
// WithTimeFormat sets the date-time output format
func (teo TimeEntryOutputOptions) WithTimeFormat(
format string) TimeEntryOutputOptions {
teo.TimeFormat = format
return teo
}
// WithShowTasks shows a new column with the task of the time entry
func (teo TimeEntryOutputOptions) WithShowTasks() TimeEntryOutputOptions {
teo.ShowTasks = true
return teo
}
// WithShowCliens shows a new column with the client of the time entry
func (teo TimeEntryOutputOptions) WithShowClients() TimeEntryOutputOptions {
teo.ShowClients = true
return teo
}
// WithTotalDuration shows a footer with the sum of the durations of the time
// entries
func (teo TimeEntryOutputOptions) WithTotalDuration() TimeEntryOutputOptions {
teo.ShowTotalDuration = true
return teo
}
// TimeEntriesPrint will print more details
func TimeEntriesPrint(
options TimeEntryOutputOptions) func([]dto.TimeEntry, io.Writer) error {
return func(timeEntries []dto.TimeEntry, w io.Writer) error {
tw := tablewriter.NewWriter(w)
projectColumn := 4
header := []string{"ID", "Start", "End", "Dur", "Project"}
if options.ShowTasks {
header = append(header, "Client")
}
if options.ShowTasks {
header = append(header, "Task")
}
header = append(header, "Description", "Tags")
tw.SetHeader(header)
tw.SetRowLine(true)
if width, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil {
tw.SetColWidth(width / 3)
}
colors := make([]tablewriter.Colors, len(header))
for i := 0; i < len(timeEntries); i++ {
t := timeEntries[i]
end := time.Now()
if t.TimeInterval.End != nil {
end = *t.TimeInterval.End
}
projectName := ""
colors[projectColumn] = []int{}
if t.Project != nil {
colors[projectColumn] = util.ColorToTermColor(t.Project.Color)
projectName = t.Project.Name
}
line := []string{
t.ID,
t.TimeInterval.Start.In(time.Local).Format(options.TimeFormat),
end.In(time.Local).Format(options.TimeFormat),
durationToString(end.Sub(t.TimeInterval.Start)),
projectName,
}
if options.ShowClients {
client := ""
if t.Project.ClientName != "" {
colors[len(line)] = colors[projectColumn]
client = t.Project.ClientName
}
line = append(line, client)
}
if options.ShowTasks {
task := ""
if t.Task != nil {
task = fmt.Sprintf("%s (%s)", t.Task.Name, t.Task.ID)
}
line = append(line, task)
}
line = append(
line,
t.Description,
strings.Join(tagsToStringSlice(t.Tags), "\n"),
)
tw.Rich(line, colors)
}
if options.ShowTotalDuration {
line := make([]string, len(header))
line[0] = "TOTAL"
line[3] = durationToString(sumTimeEntriesDuration(timeEntries))
tw.Append(line)
}
tw.Render()
return nil
}
}
func tagsToStringSlice(tags []dto.Tag) []string {
s := make([]string, len(tags))
for i, t := range tags {
s[i] = fmt.Sprintf("%s (%s)", t.Name, t.ID)
}
return s
}
func durationToString(d time.Duration) string {
p := ""
if d < 0 {
p = "-"
d = d * -1
}
return p + fmt.Sprintf("%d:%02d:%02d",
int64(d.Hours()), int64(d.Minutes())%60, int64(d.Seconds())%60)
}