-
Notifications
You must be signed in to change notification settings - Fork 2
/
step-4.js
46 lines (37 loc) · 1.04 KB
/
step-4.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
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 rainbowClock(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)
));
}
process.stdout.write(rainbowClock(getTime()));
setInterval(() => {
const currentTimeString = rainbowClock(getTime());
process.stdout.write(ansiEscapes.eraseLines(7) + currentTimeString);
}, 1000);