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
99 lines (90 loc) · 2.65 KB
/
Copy pathoutput.js
File metadata and controls
99 lines (90 loc) · 2.65 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
import moment from 'moment'
import Table from 'cli-table'
import chalk from 'chalk'
import {recognizeModifierTiming, humanParseDiff, calcRate} from './utils'
export const summarize = function(args) {
let table = new Table({
head: ['Duration', 'Dates', 'Task'],
chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''},
colAligns: ['right', 'center', 'left'],
style: { head: ['green'] }
});
let total = 0
let head = `Search: ${args.search} \n`
args.tasks.forEach((task, index) => {
let name = task.name
task = task.task
let duration = task.getSeconds()
total += duration
table.push([humanParseDiff(duration), moment(task.getStartDate()).format(args.format), name])
})
console.log(table.toString());
if (args.full) {
let table2 = new Table()
table2.push(
{'Search': ['\"' + args.search + '\"']},
{'Total time': [humanParseDiff(total)]}
)
if (args.rate)
table2.push({'Rate': [calcRate(args.rate, total)]})
if (args.timespan) {
let obj = {}
obj['Time (' + args.timespan + ')'] = [calcTime(args.timespan, args.tasks)]
table2.push(obj)
}
console.log(table2.toString());
}
}
export const calcTime = function(timespan, tasks) {
let results = []
let pTimespan = recognizeModifierTiming(timespan)[0]
let limit = moment().subtract(pTimespan.value, pTimespan.momentKey)
let multipleOutput = (tasks.length > 1)? true : false
tasks.forEach(el => {
console.log()
let checkins = el.task.task.timings.reverse()
let sum = 0
let overflow = 0
let result = ''
for (let i = 0; i < checkins.length; i++) {
if (moment(checkins[i].stop).isBefore(limit))
break
sum += moment(checkins[i].stop).diff(checkins[i].start, 's')
if (moment(checkins[i].start).isBefore(limit)) {
overflow = moment(limit).diff(checkins[i].start, 's')
break
}
}
if (multipleOutput)
result += el.name + '\n'
result += humanParseDiff(sum, true, true)
if (overflow)
result += '\n- ' + humanParseDiff(overflow, true)
results.push(result)
})
return results.join('\n\n')
}
export const outputConfig = function (config) {
let table = new Table({
head: ['Key', 'value'],
chars: {'mid': '', 'left-mid': '', 'mid-mid': '', '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(chalk.red(`Error: ${err}`))
}
export const cliSuccess = function(err) {
console.log(chalk.green(err))
}