-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (96 loc) · 2.8 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
let blessed = require("blessed");
const figlet = require('figlet');
// Create a screen object.
let screen = blessed.screen({
smartCSR: true,
});
screen.title = "Timer";
let box = blessed.box({
top: "center",
left: "center",
width: "100%",
height: "100%",
align: "center",
valign: "middle",
tags: true,
border: {
type: "line",
},
style: {
fg: "white",
border: {
fg: "#f0f0f0",
},
hover: {
bg: "green",
},
},
});
screen.append(box);
screen.key(["escape", "q", "C-c"], function (ch, key) {
return process.exit(0);
});
// Focus our element.
box.focus();
// Render the screen.
screen.render();
let toCountFrom = process.argv[2];
function getExpiry(str) {
const timeFactor = str.substring(str.length - 1);
const time = str.substring(0, str.length - 1);
const multipliers = {
s: (t) => t,
m: (t) => t * 60,
h: (t) => t * 3600,
// d: (t) => t * 86400,
// w: (t) => t * 1.6534e-6,
// M: (t) => t * 2.628e6,
};
return multipliers[timeFactor](time);
}
function convertToTimerString(t) {
var time = t;
let hours = Math.floor(time / 3600);
time = time - hours * 3600;
let minutes = Math.floor(time / 60);
let seconds = time - minutes * 60;
return [{ hours, minutes, seconds }, t];
}
const sleep = () => new Promise((res) => setTimeout(res, 1000));
async function startCountDown([timerObj, time]) {
while (true) {
// console.log(typeof timerObj.hours);
let hoursText = timerObj.hours < 10 ? `0${timerObj.hours}` : timerObj.hours;
let minutesText = timerObj.minutes < 10 ? `0${timerObj.minutes}` : timerObj.minutes;
let secondsText = timerObj.seconds < 10 ? `0${timerObj.seconds}` : timerObj.seconds;
let timerText = `${hoursText} : ${minutesText} : ${secondsText}`;
let text = figlet.textSync(timerText, "Varsity");
box.setContent(text);
screen.render();
await sleep();
if (timerObj.seconds >= 0) {
if (timerObj.seconds === 0) {
timerObj.seconds = 60;
if (timerObj.minutes >= 0) {
if (timerObj.minutes === 0) {
timerObj.minutes = 60;
if (timerObj.hours >= 0) {
timerObj.hours -= 1;
}
}
timerObj.minutes -= 1;
}
}
timerObj.seconds -= 1;
}
if (
timerObj.seconds === 0 &&
timerObj.minutes === 0 &&
timerObj.hours === 0
) {
process.exit(0);
}
}
return [timerObj, time];
}
startCountDown(convertToTimerString(getExpiry(toCountFrom)));