Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 75 lines (58 sloc) 2.28 KB
#! /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(*@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;
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
}