-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·112 lines (96 loc) · 2.38 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
#!/usr/bin/env node
const chalk = require("chalk");
const clear = require("clear");
const figlet = require("figlet");
const inquirer = require("inquirer");
const util = require("lodash/util");
const player = require("play-sound")((opts = {}));
const ProgressBar = require("progress");
function format_time(seconds) {
var date = new Date(0);
date.setSeconds(seconds);
var timeString = date.toISOString().substr(11, 8);
return timeString;
}
clear();
console.log(
chalk.cyanBright(
figlet.textSync("Meditation", { horizontalLayout: "fitted" })
)
);
function askTimer() {
const questions = [
{
name: "minutes",
type: "list",
default: 29,
choices: util.range(1, 121),
message: "Select meditation length (min.):",
},
{
name: "interval",
type: "list",
default: "None",
choices: util.range(1, 31).concat("None", "Middle"),
message: "Select interval timer (min.):",
},
];
return inquirer.prompt(questions);
}
function setBar(results, finish) {
return new ProgressBar(
`:my_time :bar Finish: ${finish.toTimeString().slice(0, 5)}`,
{
complete: "=",
head: ">",
incomplete: " ",
width: 60,
total: results.minutes * 60,
}
);
}
function setTimer(bar, finish, interval) {
setInterval(function () {
bar.tick({
my_time: format_time(bar.curr),
finish: finish,
});
if (bar.complete) {
clearInterval(this);
player.play(__dirname + "/sounds/bell1.mp3", function (err) {
if (err) throw err;
});
}
if (interval && bar.curr > 0 && !bar.complete) {
if (bar.curr % interval == 0) {
player.play(__dirname + "/sounds/bell2.mp3", function (err) {
if (err) throw err;
});
}
}
}, 1000);
}
async function run() {
const results = await askTimer();
console.log();
player.play(__dirname + "/sounds/bell1.mp3", function (err) {
if (err) throw err;
});
var now = new Date();
var finish = new Date(now.setMinutes(now.getMinutes() + results.minutes));
var interval;
switch (results.interval) {
case "None":
interval = null;
break;
case "Middle":
interval = (results.minutes / 2) * 60;
break;
default:
interval = results.interval * 60;
break;
}
var bar = setBar(results, finish);
var timer = setTimer(bar, finish, interval);
}
run();