-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcount-down
More file actions
executable file
·86 lines (67 loc) · 2.67 KB
/
Copy pathcount-down
File metadata and controls
executable file
·86 lines (67 loc) · 2.67 KB
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
#! /usr/bin/env perl6
use v6.c;
constant term:<ANSI-SAME-LINE> = "\e[1F\e[0K";
subset Seconds of Numeric;
my regex number { \d+ [ '.' \d+ ]? }
my regex suffix { <[dhms]> };
constant %unit-multipliers = %{'d' => 60*60*24, 'h' => 60*60, 'm' => 60, 's' => 1};
constant %unit-check = %{ 'd' => -> { now.DateTime.day }, 'h' => -> { now.DateTime.hour }, 'm' => -> { now.DateTime.minute }, 's' => -> { now.DateTime.second } };
multi sub pretty-print(Seconds $seconds is copy --> Str) {
my @ret;
for %unit-multipliers.sort(-*.value) -> (:key($unit), :value($multiplier)) {
@ret.push: $seconds.Int div $multiplier ~ $unit if $seconds.Int div $multiplier;
$seconds = $seconds % $multiplier;
}
@ret.join: ' ';
}
multi sub MAIN(Str $until where .Str ~~ /[\d]?\d ':' \d\d/) {
my ($hour, $minute) = $until.split(':');
my $today = Date.today;
my $then = DateTime.new(:date($today), :$hour, :$minute, :second(0), :timezone($*TZ));
if $then < DateTime.now {
$then.=later(:day(1));
}
MAIN( $then - DateTime.now );
}
multi sub MAIN(Seconds $to-wait) {
MAIN($to-wait ~ 's');
}
multi sub MAIN() {
note 'Missing operand. Please provide a time or duration of the form HH:MM or 11h 22m 33s.';
exit 1
}
multi sub MAIN(*@timicles where .all ~~ /<number> <suffix>/) {
# my Seconds $to-wait = @timicles»\
# .match(/<number> <suffix>+/)».hash\ # the +-quantifier is a workaround
# .map(-> % ( Rat(Any) :$number, Str(Any) :$suffix ) { %unit-multipliers{$suffix} * $number })\
# .sum;
my Seconds $to-wait = (@timicles
==> map(* ~~ /<number> <suffix>+/) # the +-quantifier is a workaround
==> map(*.hash)
==> map(-> % ( Rat(Any) :$number, Str(Any) :$suffix ) { %unit-multipliers{$suffix} * $number })
==> sum);
react {
whenever Promise.in($to-wait) {
exit 0;
}
whenever signal(SIGINT) {
exit 1;
}
whenever Supply.interval(1) {
state $count-down = $to-wait;
say ANSI-SAME-LINE ~ pretty-print($count-down--);
}
}
}
sub USAGE {
print Q:c:to/EOH/;
Usage: {$*PROGRAM-NAME} NUMBER[SUFFIX]… | [HH:MM]
Display a countdown for the specified time. Decimal fractions are supported for
NUMBER and suffixes for [d]ays, [h]ours, [m]inutes or [s]econds are
recognized. If the countdown is exhausted exit with 0.
If a HH:MM timespec is provided a count-down until the given system time in
the local timezone will be started. For timespecs younger then the local
time, the next day at that time is assumed.
Receiving SIGINT will interrupt the countdown and result in exitcode 1.
EOH
}