forked from naggie/dstask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
293 lines (253 loc) · 6.26 KB
/
display.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package dstask
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/mattn/go-isatty"
)
// DisplayByNext renders the TaskSet's array of tasks.
func (ts *TaskSet) DisplayByNext(ctx Query, truncate bool) error {
ts.SortByCreated(Ascending) // older tasks first (from top) like a FIFO queue
ts.SortByPriority(Ascending) // high priority tasks first, of course
if StdoutIsTTY() {
ctx.PrintContextDescription()
err := ts.renderTable(truncate)
if err != nil {
return err
}
var critical int
var totalCritical int
for _, t := range ts.Tasks() {
if t.Priority == PRIORITY_CRITICAL {
critical++
}
}
// search outside current filter in taskset
for _, t := range ts.AllTasks() {
if t.Priority == PRIORITY_CRITICAL && !StrSliceContains(HIDDEN_STATUSES, t.Status) {
totalCritical++
}
}
if critical < totalCritical {
fmt.Printf(
"\033[38;5;%dm%v critical task(s) outside this context! Use `dstask -- P0` to see them.\033[0m\n",
FG_PRIORITY_CRITICAL,
totalCritical-critical,
)
}
return nil
}
// stdout is not a tty
return ts.renderJSON()
}
func (ts *TaskSet) renderJSON() error {
unfilteredTasks := ts.Tasks()
data, err := json.MarshalIndent(unfilteredTasks, "", " ")
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, bytes.NewBuffer(data))
return err
}
func (ts *TaskSet) renderTable(truncate bool) error {
tasks := ts.Tasks()
total := len(tasks)
if ts.NumTotal() == 0 {
fmt.Println("No tasks found. Run `dstask help` for instructions.")
} else if len(tasks) == 0 {
ExitFail("No matching tasks in given context or filter.")
} else if len(tasks) == 1 {
task := tasks[0]
task.Display()
if task.Notes != "" {
fmt.Printf("\nNotes on task %d:\n\033[38;5;245m%s\033[0m\n\n", task.ID, task.Notes)
}
return nil
} else {
w, h := MustGetTermSize()
maxTasks := h - TERMINAL_HEIGHT_MARGIN // leave room for context message, header and prompt
if maxTasks < MIN_TASKS_SHOWN {
maxTasks = MIN_TASKS_SHOWN
}
if truncate && maxTasks < len(tasks) {
tasks = tasks[:maxTasks]
}
table := NewTable(
w,
"ID",
"Priority",
"Tags",
"Project",
"Summary",
)
for _, t := range tasks {
style := t.Style()
table.AddRow(
[]string{
// id should be at least 2 chars wide to match column header
// (headers can be truncated)
fmt.Sprintf("%-2d", t.ID),
t.Priority,
strings.Join(t.Tags, " "),
t.Project,
t.LongSummary(),
},
style,
)
}
table.Render()
if truncate && maxTasks < total {
fmt.Printf("\n%v/%v tasks shown.\n", maxTasks, total)
} else {
fmt.Printf("\n%v tasks.\n", total)
}
}
return nil
}
func (task *Task) Display() {
w, _ := MustGetTermSize()
table := NewTable(
w,
"Name",
"Value",
)
table.AddRow([]string{"ID", strconv.Itoa(task.ID)}, RowStyle{})
table.AddRow([]string{"Priority", task.Priority}, RowStyle{})
table.AddRow([]string{"Summary", task.Summary}, RowStyle{})
table.AddRow([]string{"Status", task.Status}, RowStyle{})
table.AddRow([]string{"Project", task.Project}, RowStyle{})
table.AddRow([]string{"Tags", strings.Join(task.Tags, ", ")}, RowStyle{})
table.AddRow([]string{"UUID", task.UUID}, RowStyle{})
table.AddRow([]string{"Created", task.Created.String()}, RowStyle{})
if !task.Resolved.IsZero() {
table.AddRow([]string{"Resolved", task.Resolved.String()}, RowStyle{})
}
if !task.Due.IsZero() {
table.AddRow([]string{"Due", task.Due.String()}, RowStyle{})
}
table.Render()
}
func (t *Task) Style() RowStyle {
now := time.Now()
style := RowStyle{}
if t.Status == STATUS_ACTIVE {
style.Fg = FG_ACTIVE
style.Bg = BG_ACTIVE
} else if !t.Due.IsZero() && t.Due.Before(now) {
style.Fg = FG_PRIORITY_HIGH
} else if t.Priority == PRIORITY_CRITICAL {
style.Fg = FG_PRIORITY_CRITICAL
} else if t.Priority == PRIORITY_HIGH {
style.Fg = FG_PRIORITY_HIGH
} else if t.Priority == PRIORITY_LOW {
style.Fg = FG_PRIORITY_LOW
}
if t.Status == STATUS_PAUSED {
style.Bg = BG_PAUSED
}
return style
}
// TODO combine with previous with interface, plus computed Project status
func (p *Project) Style() RowStyle {
style := RowStyle{}
if p.Active {
style.Fg = FG_ACTIVE
style.Bg = BG_ACTIVE
} else if p.Priority == PRIORITY_CRITICAL {
style.Fg = FG_PRIORITY_CRITICAL
} else if p.Priority == PRIORITY_HIGH {
style.Fg = FG_PRIORITY_HIGH
} else if p.Priority == PRIORITY_LOW {
style.Fg = FG_PRIORITY_LOW
}
return style
}
func (ts TaskSet) DisplayByWeek() {
ts.SortByResolved(Ascending)
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
w, _ := MustGetTermSize()
var table *Table
var lastWeek int
tasks := ts.Tasks()
for _, t := range tasks {
_, week := t.Resolved.ISOWeek()
// guaranteed true for first iteration, ISOweek starts with 1.
if week != lastWeek {
if table != nil && len(table.Rows) > 0 {
table.Render()
}
// insert gap
fmt.Printf("\n\n> Week %d, starting %s\n\n", week, t.Resolved.Format("Mon 2 Jan 2006"))
table = NewTable(
w,
"Resolved",
"Priority",
"Tags",
"Project",
"Summary",
)
}
table.AddRow(
[]string{
t.Resolved.Format("Mon 2"),
t.Priority,
strings.Join(t.Tags, " "),
t.Project,
t.LongSummary(),
},
t.Style(),
)
_, lastWeek = t.Resolved.ISOWeek()
}
if table != nil {
table.Render()
}
fmt.Printf("%v tasks.\n", len(tasks))
} else {
// print json
ts.renderJSON()
}
}
func (ts TaskSet) DisplayProjects() error {
if StdoutIsTTY() {
ts.renderProjectsTable()
return nil
}
return ts.renderProjectsJSON()
}
func (ts TaskSet) renderProjectsJSON() error {
data, err := json.MarshalIndent(ts.GetProjects(), "", " ")
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, bytes.NewBuffer(data))
return err
}
func (ts TaskSet) renderProjectsTable() {
projects := ts.GetProjects()
w, _ := MustGetTermSize()
table := NewTable(
w,
"Name",
"Progress",
"Created",
)
for _, project := range projects {
if project.TasksResolved < project.Tasks {
table.AddRow(
[]string{
project.Name,
fmt.Sprintf("%d/%d", project.TasksResolved, project.Tasks),
project.Created.Format("Mon 2 Jan 2006"),
},
project.Style(),
)
}
}
table.Render()
}