forked from diit/overtime-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (57 loc) · 1.65 KB
/
index.js
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
#!/usr/bin/env node
'use strict'
const Table = require('cli-table2')
const assert = require('assert')
const { DateTime } = require('luxon')
const chalk = require('chalk')
const vorpal = require('vorpal')()
const NIGHT_TIME_START = 20
const NIGHT_TIME_END = 9
const ALIAS_SPLITTER = '@';
vorpal.command('show [regions...]', 'Generates time overlap table')
.alias('table')
.action(function (args, cb) {
// TODO: BETTER VALIDATION
assert(args.regions.length > 0)
const pairs = args.regions.map((region) => {
return (region.includes(ALIAS_SPLITTER)) ? region.split(ALIAS_SPLITTER) : [region, region]
});
const regions = pairs.map(pair => pair[0]);
const aliases = pairs.map(pair => pair[1]);
const table = new Table({
head: aliases
})
// TODO: Pivot from primary
const getRegionsTime = region => {
const now = DateTime.local()
return now.setZone(region)
}
const formatTime = time => {
const displayString = ` ${time.toLocaleString({
hour: 'numeric',
minute: 'numeric',
hour12: true
})} `
if (time.hour > NIGHT_TIME_START || time.hour < NIGHT_TIME_END) {
return chalk.grey.bgBlack(displayString)
}
return chalk.black.bgYellow(displayString)
}
for (let hour = 0; hour < 24; hour++) {
const row = []
regions.forEach(region => {
row.push(formatTime(getRegionsTime(region).plus({
hours: hour
}).set({
minutes: 0
})))
})
table.push(row)
}
this.log(table.toString())
cb(null, table.toString())
})
vorpal
.delimiter('$ ')
.show()
.parse(process.argv)