-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
130 lines (103 loc) · 3.26 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
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
const ansiEscapes = require("ansi-escapes");
const { red, yellow, blue } = require("chalk");
const { toBlock, mapBlock, concatBlocks, toString } = require("terminal-block-fonts");
function leftPad(number) {
if (number < 10) {
return "0" + number;
} else {
return String(number);
}
}
function getTime() {
const date = new Date();
return {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
};
}
function rainbowClock12(time) {
const { hour, minute, second } = time;
let ampmHour = hour % 12;
if (ampmHour === 0) {
// for 0 and 12, they're showed as 12AM and 12PM
ampmHour = 12;
}
const hourBlock = toBlock(leftPad(ampmHour));
const minuteBlock = toBlock(leftPad(minute));
const secondBlock = toBlock(leftPad(second));
const sepBlock = toBlock(":");
const ampmBlock = toBlock(hour >= 12 ? " PM" : " AM");
return toString(concatBlocks(
mapBlock(hourBlock, red),
sepBlock,
mapBlock(minuteBlock, yellow),
sepBlock,
mapBlock(secondBlock, blue),
ampmBlock
));
}
function rainbowClock24(time) {
const { hour, minute, second } = time;
const hourBlock = toBlock(leftPad(hour));
const minuteBlock = toBlock(leftPad(minute));
const secondBlock = toBlock(leftPad(second));
const sepBlock = toBlock(":");
return toString(concatBlocks(
mapBlock(hourBlock, red),
sepBlock,
mapBlock(minuteBlock, yellow),
sepBlock,
mapBlock(secondBlock, blue)
));
}
function checkAutocomplete(argv) {
if (argv[2] === "--completion") {
// generate completion script
process.stdout.write(`
_clock_completion() {
COMPREPLY=( $(compgen -W '$(clock --compgen "\${COMP_CWORD}" "\${COMP_WORDS[COMP_CWORD-1]}" "\${COMP_LINE}")' -- "\${COMP_WORDS[COMP_CWORD]}") )
}
complete -F _clock_completion clock
`);
// exit process immediately after completion request
process.exit(0);
} else if (argv[2] === "--compgen") {
// fragment is the above "$COMP_CWORD" telling us
// which word is being worked on
const fragment = parseInt(argv[3], 10);
if (fragment === 1) {
// for the 1st position we return 3 possible option names
process.stdout.write('--mode --help --version');
} else if (fragment === 2) {
// for the 2nd position we return 3 possible candidates
const prevWord = argv[4];
// line is the exact text users can see from the command line
// it can be very useful for more complex autocompletion cases
// const line = argv[5];
if (prevWord === "--mode") {
process.stdout.write('12h 24h');
}
}
// otherwise we output nothing which means nothing to suggest
// exit process immediately after completion request
process.exit(0);
}
}
function run() {
checkAutocomplete(process.argv);
const program = require("commander");
program
.version("1.0.0")
.option("--mode <mode>", "display mode, can be either 12h or 24h", "24h")
.parse(process.argv);
const clock = program.mode === "12h" ? rainbowClock12 : rainbowClock24;
process.stdout.write(clock(getTime()));
setInterval(() => {
const currentTimeString = clock(getTime());
process.stdout.write(ansiEscapes.eraseLines(7) + currentTimeString);
}, 1000);
}
module.exports = {
run
};