-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstep-5.js
88 lines (69 loc) · 1.95 KB
/
step-5.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
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 run() {
const program = require("commander");
program
.version("1.0.0")
.option("--mode <mode>", "display mode, can be with 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
};