Create a gist now

Instantly share code, notes, and snippets.

simple timer
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Getopt::Long;
use constant DIGITAL_NUMBERS => (
[ qw(
1110
1010
1010
1010
1110
) ],
[ qw(
0100
1100
0100
0100
1110
) ],
[ qw(
1110
0010
1110
1000
1110
) ],
[ qw(
1110
0010
1110
0010
1110
) ],
[ qw(
1010
1010
1110
0010
0010
) ],
[ qw(
1110
1000
1110
0010
1110
) ],
[ qw(
1110
1000
1110
1010
1110
) ],
[ qw(
1110
1010
0010
0010
0010
) ],
[ qw(
1110
1010
1110
1010
1110
) ],
[ qw(
1110
1010
1110
0010
0010
) ],
);
use constant COLON => [ qw(
10
10
00
10
10
) ];
use constant COLOR_MAP => {
'black' => "\e[40m",
'red' => "\e[41m",
'green' => "\e[42m",
'yellow' => "\e[43m",
'blue' => "\e[44m",
'magenta' => "\e[45m",
'cyan' => "\e[46m",
'white' => "\e[47m",
'bright_black' => "\e[100m",
'bright_red' => "\e[101m",
'bright_green' => "\e[102m",
'bright_yellow' => "\e[103m",
'bright_blue' => "\e[104m",
'bright_magenda' => "\e[105m",
'bright_cyan' => "\e[106m",
'bright_white' => "\e[107m",
};
GetOptions(
'timer=s' => \my $timer,
'color=s' => \my $color,
'scale=s' => \my $scale,
);
$timer ||= 150;
$color ||= 'white';
$scale ||= 3;
&countdown({ timer => $timer, color => $color, scale => $scale });
sub countdown {
my $args = shift;
my $total_second = $args->{timer};
my $color = $args->{color};
my $scale = $args->{scale};
my $scaled_numbers = &get_scaled_numbers( $scale );
my $scaled_colon = &get_scaled_colon( $scale );
print "\033[2J";
for my $past_second ( 0..$total_second ) {
my $remain_second = $total_second - $past_second;
my $minute = int( $remain_second / 60 );
my $second = int( $remain_second % 60 );
$minute = ( $minute =~ m!^\d$! ) ? '0' . $minute : $minute;
$second = ( $second =~ m!^\d$! ) ? '0' . $second : $second;
my $time_str = sprintf('%s:%s', $minute, $second);
my @display;
for my $char ( split('', $time_str ) ) {
if( $char eq ':' ) {
#for my $key ( keys @{ $scaled_colon } ) {
for ( my $key = 0; $key < scalar @{ $scaled_colon }; $key++ ) {
$display[$key] .= $scaled_colon->[$key];
}
}
else {
#for my $key ( keys @{ $scaled_numbers->[$char] } ) {
for ( my $key = 0; $key < scalar @{ $scaled_numbers->[$char] }; $key++ ) {
$display[$key] .= $scaled_numbers->[$char][$key];
}
}
}
my $colored_display;
for my $line ( @display ) {
for my $char ( split('', $line) ) {
if( $char == 1 ) {
$colored_display .= COLOR_MAP->{$color} . " ";
}
else {
$colored_display .= "\e[0m" . " ";
}
}
$colored_display .= "\n";
}
print "\033[0;0H";
print $colored_display;
print "\n";
sleep 1;
}
}
sub get_scaled_numbers {
my $scale = shift;
return &bulk_scaling( $scale, DIGITAL_NUMBERS() );
}
sub get_scaled_colon {
my $scale = shift;
return &scaling( $scale, COLON() );
}
sub bulk_scaling {
my $scale = shift;
my @digitals = @_;
my @result;
for my $digital ( @digitals ) {
my $scaled_digital = &scaling( $scale, $digital );
push @result, $scaled_digital;
}
return \@result;
}
sub scaling {
my $scale = shift || 3;
my $digital = shift;
my @scaled_digital;
for my $line ( @{ $digital } ) {
my $scaled_line;
for my $char ( split('', $line) ) {
$scaled_line .= $char x $scale;
}
for ( 1..$scale ) {
push @scaled_digital, $scaled_line;
}
}
return \@scaled_digital;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment