forked from danibram/time-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.js
More file actions
200 lines (177 loc) · 5.78 KB
/
Copy pathoutput.js
File metadata and controls
200 lines (177 loc) · 5.78 KB
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
import moment from 'moment'
import Table from 'cli-table2'
import colors from 'colors'
import { humanParseDiff, calcRate, getSeconds } from './utils'
export const sumarize = function(search, tasks, rate, full, format) {
let table = new Table({
head: ['Name', 'Description', 'Dates', 'Duration'],
colAligns: ['left', 'center', 'center', 'left'],
chars: {
top: '═',
'top-mid': '╤',
'top-left': '╔',
'top-right': '╗',
bottom: '═',
'bottom-mid': '╧',
'bottom-left': '╚',
'bottom-right': '╝',
left: '║',
'left-mid': '╟',
right: '║',
'right-mid': '╢'
},
style: { head: ['green'] }
})
let total = 0
let head = `Search: ${search} \n`
tasks.forEach((task, index) => {
let description =
task.task.description() && task.task.description() !== ''
? task.task.description()
: '-'
let name = task.name
task = task.task
let duration = task.getSeconds()
total += duration
// Avoid excesive width for proper console fit
let splitWidth = (str, len) => {
let arr = []
while (str != '') {
if (str.length > len) {
arr.push(str.substring(0, len))
str = str.substring(len)
} else {
arr.push(str)
break
}
}
return arr.join('\n')
}
description = splitWidth(description, 51)
name = splitWidth(name, 40)
table.push([
name,
description,
moment(task.getStartDate()).format(format),
humanParseDiff(duration)
])
})
if (full) {
table.push([])
if (rate) {
table.push(
[
{
rowSpan: 2,
content: `${colors.red('Search:')} "${search}"`,
vAlign: 'center'
},
colors.red('Total time'),
humanParseDiff(total)
],
[colors.red('Rate'), calcRate(rate, total)]
)
} else {
table.push(
[
{
rowSpan: 2,
content: `${colors.red('Search:')} "${search}"`,
vAlign: 'center'
},
{ rowSpan: 2, content: '', vAlign: 'center' },
{
rowSpan: 2,
content: colors.red('Total time'),
vAlign: 'center'
},
{
rowSpan: 2,
content: humanParseDiff(total),
vAlign: 'center'
}
],
[]
)
}
}
console.log(table.toString())
}
export const outputConfig = function(config) {
let table = new Table({
head: ['Key', 'value'],
chars: {
top: '═',
'top-mid': '╤',
'top-left': '╔',
'top-right': '╗',
bottom: '═',
'bottom-mid': '╧',
'bottom-left': '╚',
'bottom-right': '╝',
left: '║',
'left-mid': '╟',
right: '║',
'right-mid': '╢'
},
colAligns: ['center', 'center'],
style: { head: ['green'] }
})
Object.keys(config).map(e => table.push([e, config[e]]))
console.log(table.toString())
}
export const outputVertical = function(...args) {
let table2 = new Table()
let key = args.splice(0, 1)
table2.push({ [key]: args })
return table2.toString()
}
export const cliError = function(err) {
console.error(colors.red(`Error: ${err}`))
}
export const cliSuccess = function(err) {
console.log(colors.green(err))
}
export const markdown = function(tasks, expanded = false) {
let body = '| Start | End | Hours | Subtotal | Description |\n'
body += '| ----- | --- | -----:| -------: | ----------- |\n'
let total = 0
//tasks = start || end ? this.filterDates(tasks, start, end) : tasks;
tasks.forEach(task => {
let name = task.name
task = task.task
if (!task.filtered) {
let duration = task.getSeconds()
total += duration
if (expanded && task.task.timings.length > 1) {
let subtotal = 0
let times = 0
task.task.timings.forEach(timing => {
let secs = getSeconds(timing)
subtotal += secs
times += 1
body +=
`|${moment(timing.start).format('YYYY/MM/DD kk:mm')}` +
`|${moment(timing.stop).format('YYYY/MM/DD kk:mm')}` +
`|${humanParseDiff(secs)}` +
`|${humanParseDiff(subtotal)}` +
`|${name} (part ${times})` +
`|\n`
})
} else {
let start = task.getStartDate()
let stop = task.getEndDate()
body +=
`|${moment(start).format('YYYY/MM/DD kk:mm')}` +
`|${moment(stop).format('YYYY/MM/DD kk:mm')}` +
`|${humanParseDiff(duration)}` +
`|${humanParseDiff(total)}` +
`|${name}` +
`|\n`
}
}
})
body += '| | | |\n'
body += `| Total | | ${humanParseDiff(total)} | | |\n`
return body
}