Skip to content

Instantly share code, notes, and snippets.

@soren
Last active April 26, 2021 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soren/066048ecc58e2b3f167516f902623f92 to your computer and use it in GitHub Desktop.
Save soren/066048ecc58e2b3f167516f902623f92 to your computer and use it in GitHub Desktop.
# Display a big clock using blocks made from reverse video spaces
#!/usr/bin/env perl
# Display a big clock using blocks made from reverse video spaces
use warnings;
use strict;
use feature qw(signatures);
no warnings qw(experimental::signatures);
use utf8;
use Getopt::Std;
use Module::Load::Conditional qw(can_load);
use POSIX qw(strftime);
use Term::ANSIColor;
# We only need this on Windows, where it should be available when
# running Strawberry Perl.
can_load(modules => {'Win32::Console::ANSI' => undef});
sub usage($exit_code=0) {
print "Usage: $0 [-rsh]\n";
print "Display a big clock. The clock is displayed using ANSI escape codes.\n\n";
print " -r run clock continuously. Default is to display the current time and exit\n";
print " -s show seconds. Default is to show hours and minutes\n";
print " -h display this help\n";
exit $exit_code;
}
sub init() {
my %opt;
getopts('rsh', \%opt) or usage(1);
usage() if $opt{h};
return \%opt;
}
sub cls() { "\e[2J" }
sub savepos() { "\e[s" }
sub loadpos() { "\e[u" }
sub down($repeat=1) { "\e[B" x $repeat }
sub left($repeat=1) { "\e[D" x $repeat }
sub locate($row, $col) { "\e[${row};${col}H" }
sub make_big_char($str) {
if (not ref $str) {
my @str = ($str) x 5;
return make_big_char(\@str);
}
(my $raw_str = $str->[0]) =~ s/\e\[\d+m//g;
my $width = length($raw_str);
return
$str->[0] . savepos . down . left($width) .
$str->[1] . down . left($width) .
$str->[2] . down . left($width) .
$str->[3] . down . left($width) .
$str->[4] . loadpos;
}
my $opt = init();
my @digits = (
make_big_char(["\e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m "," \e[7m \e[27m "," \e[7m \e[27m "," \e[7m \e[27m ","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m"," \e[7m \e[27m","\e[7m \e[27m","\e[7m \e[27m ","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m"," \e[7m \e[27m","\e[7m \e[27m"," \e[7m \e[27m","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m"," \e[7m \e[27m"," \e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m","\e[7m \e[27m ","\e[7m \e[27m"," \e[7m \e[27m","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m ","\e[7m \e[27m ","\e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m"," \e[7m \e[27m"," \e[7m \e[27m"," \e[7m \e[27m"," \e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m"]),
make_big_char(["\e[7m \e[27m","\e[7m \e[27m \e[7m \e[27m","\e[7m \e[27m"," \e[7m \e[27m"," \e[7m \e[27m"])
);
my $space = make_big_char(" ");
my $dots = make_big_char([" ","\e[7m \e[27m"," ","\e[7m \e[27m"," "]);
my $frame_width = $opt->{s} ? 58 : 38;
$SIG{INT} = \&reset_term;
sub reset_term {
color('reset');
print "\e[?25h\n"; # show cursor
exit 0;
}
binmode STDOUT, ":encoding(UTF-8)";
print cls . "\e[?25l"; # hide cursor
$|=1; # flush immediately when print'ing
do {
my @time = split //, strftime "%H%M%S", localtime time;
print
locate(2,3) .
color('green on_black'),
" " x $frame_width, down, left($frame_width),
$space,
color('red on_black'),
$digits[$time[0]], $space,
$digits[$time[1]], $space,
$opt->{s} || $time[5] % 2 ? $dots : $space, $space,
$digits[$time[2]], $space,
$digits[$time[3]];
print
$space,
$dots, $space,
$digits[$time[4]], $space,
$digits[$time[5]] if $opt->{s};
print
color('green on_black'),
$space, down(5), left($frame_width),
" " x $frame_width,
color('reset');
} while ($opt->{r} && sleep(1));
reset_term;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment