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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
use fmt;
use ev;
use getopt;
use os;
use time;
use strings;
use strconv;
use math;
type parseerror = !void;
fn timer_handler(file: *ev::file) void = {
const loop = ev::getloop(file);
const t = ev::getuser(file): *time::instant;
const t = *t;
if (print_countdown(t)) {
ev::stop(loop);
};
};
fn print_countdown(instant: time::instant) bool = {
const tn = time::now(time::clock::MONOTONIC);
const currentduration = time::diff(tn, instant);
// man page console_codes(4) is useful
fmt::error("\x1B[1K\r")!;
const count = human_readable(currentduration);
defer free(count);
fmt::error(count)!;
return currentduration <= 0;
};
// Transform a duration to a human readable string representation.
fn human_readable(duration: time::duration) str = {
const hours = if (duration > time::HOUR) {
let hours = math::floorf64(duration: f64 / time::HOUR: f64): u64;
duration = (duration % time::HOUR);
yield hours;
} else {
yield 0u64;
};
const mins = if (duration > time::MINUTE) {
let mins = math::floorf64(duration: f64 / time::MINUTE: f64): u64;
duration = (duration % time::MINUTE);
yield mins;
} else {
yield 0u64;
};
const secs = if (duration > time::SECOND) {
yield math::floorf64(duration: f64 / time::SECOND: f64): u64;
} else {
yield 0u64;
};
const mods = &fmt::mods {
pad = '0',
width = 2,
...
};
return fmt::asprintf("{%}:{%}:{%}", hours, mods, mins, mods, secs, mods)!;
};
fn parse_duration(duration: const str) (time::duration | parseerror | strconv::invalid | strconv::overflow) = {
const spl = strings::split(duration, ":")!;
defer free(spl);
if (len(spl) >= 4 || len(spl) < 1) {
return parseerror;
};
const hours = if (len(spl) == 3) {
yield strconv::stoi(spl[0])?;
} else {
yield 0;
};
const mins = if (len(spl) >= 2) {
yield strconv::stoi(spl[len(spl)-2])?;
} else {
yield 0;
};
const secs = strconv::stoi(spl[len(spl)-1])?;
const hours = hours * time::HOUR;
const mins = mins * time::MINUTE;
const secs = secs * time::SECOND;
return hours + mins + secs;
};
fn parse_duration_hms(duration: const str) (time::duration | parseerror | strconv::invalid | strconv::overflow) = {
let start = 0z;
const hours = match (strings::index(duration, 'h')) {
case let i: size =>
const s = strconv::stoi(strings::sub(duration, start, i))!;
start = i + 1;
yield s;
case void =>
yield 0;
};
const mins = match (strings::index(duration, 'm')) {
case let i: size =>
const s = strconv::stoi(strings::sub(duration, start, i))!;
start = i + 1;
yield s;
case void =>
yield 0;
};
const secs = match (strings::index(duration, 's')) {
case let i: size =>
fmt::error(start)!;
yield strconv::stoi(strings::sub(duration, start, i))!;
case void =>
yield 0;
};
const hours = hours * time::HOUR;
const mins = mins * time::MINUTE;
const secs = secs * time::SECOND;
return hours + mins + secs;
};
export fn main() void = {
const cmd = getopt::parse(os::args,
"cli timer with a countdown",
"duration",
);
defer getopt::finish(&cmd);
if (len(cmd.args) != 1) {
getopt::printusage(os::stderr, os::args[0], cmd.help)!;
os::exit(os::status::FAILURE);
};
const duration = cmd.args[0];
const f = if (strings::contains(duration, ':')) {
yield &parse_duration;
} else {
yield &parse_duration_hms;
};
const duration = match (f(duration)) {
case let d: time::duration =>
yield d;
case parseerror =>
fmt::fatal("Error parsing the given duration");
case let e: strconv::invalid =>
fmt::fatal(strconv::strerror(e));
case let e: strconv::overflow =>
fmt::fatal(strconv::strerror(e));
};
const loop = ev::newloop()!;
const t = time::now(time::clock::MONOTONIC);
const t = time::add(t, duration);
defer ev::finish(&loop);
const f = ev::newtimer(&loop, &timer_handler, time::clock::MONOTONIC)!;
ev::setuser(f, &t);
print_countdown(t);
ev::timer_configure(f, 1 * time::SECOND, 1 * time::SECOND);
for (ev::dispatch(&loop, -1)!) void;
fmt::errorln()!;
};
|