Skip to content

Instantly share code, notes, and snippets.

@mwgamera
Last active March 18, 2021 23:02
Show Gist options
  • Save mwgamera/5149975c269bd776e9bea14bad3cfc07 to your computer and use it in GitHub Desktop.
Save mwgamera/5149975c269bd776e9bea14bad3cfc07 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# VT100 banner clock
# klg, Dec 2017; Apr 2018
use strict;
use Time::HiRes qw/time sleep/;
use Time::Local 'timelocal';
my @fonts;
while (<DATA>) {
s/#.*//; next if /^\s*$/;
die 'bad font syntax' unless my ($w, $h, $str) =
m/^\s*([0-9]+)\s*x\s*([0-9]+)\s*[:;,]\s*"((?:""|[^"]++)+)"\s*$/;
$str =~ s/^([^ ]*)$/$1 /;
my @c = split //, $str =~ s/""/"/gr;
my %f = (-width => $w, -height => $h);
for my $i (0 .. $h-1) {
my @b = unpack "(a$w)*", pack 'A'.($w*@c), scalar <DATA>;
s/(?<!#)#/\e[7m#/g, s/#(?!#)/#\e[m/g, s/[\s\.#]/ /g for @b;
$f{$c[$_]}[$i] = $b[$_] for 0 ..$#c;
}
push @fonts, {%f};
}
my ($width, $height);
my %font = %{$fonts[0]};
my $ft;
my $tm = time;
local $SIG{WINCH} = sub {
($height, $width) = split /\s+/, `stty size` || '24 80';
for (@fonts) {
%font = %$_ if $_
and $$_{-width}*8.5 <= $width-2
and $$_{-height} <= $height-2;
}
$ft = fmt(localtime $tm);
};
$SIG{WINCH}();
local $SIG{INT} = sub {
print STDERR"\e[0m\e[H\e[J";
exit;
};
while (1) {
local $| = 1;
my $t = time;
if ($t >= $tm-0.5 && $t < $tm+0.5) {
print $ft;
} else {
print fmt(localtime int time);
}
$tm = int $t+1;
if ($tm % 86400 == 0 && dtai($tm+1) > dtai($tm-1)) {
$ft = fmt(60, (localtime int $tm-1)[1..5]);
} else {
$ft = fmt(localtime $tm);
}
my $dt = $tm - time;
sleep $dt if $dt > 0;
}
sub fmt {
my @s = split //, sprintf '%3$ 2u:%2$02u:%1$02u', @_;
my $b = sprintf "\e[H\e[%uB\e[1J", ($height - $font{-height}) >> 1;
my $k = ($width - 1 - @s * $font{-width}) >> 1;
for my $i (0 .. $font{-height}-1) {
$b .= sprintf("\e[%uC\e[1K%s\e[K\r\n",
$k, join '', map $font{$_}[$i], @s);
}
$b =~ s/\r\n\z//;
return $b . "\e[J";
}
sub dtai {
my $ut = int(shift // time);
local $ENV{TZ} = 'right/UTC';
return timelocal(gmtime $ut) - $ut + 10;
}
__DATA__
1x1:"1234567890:"
1234567890:
4x5,"1234567890:"
.#. ### ### #.. ### ##. ### ### ### ### ...
.#. ..# ..# #.. #.. #.. ..# #.# #.# #.# .#.
.#. ### .## #.# ### ### ..# ### ### #.# ...
.#. #.. ..# ### ..# #.# ..# #.# ..# #.# .#.
.#. ### ### ..# ### ### ..# ### .## ### ...
8x7,"1234567890:"
...##.. ..####. ..####. .##.... .###### ...###. .###### ..####. ..####. ..####. .......
..###.. .##..## .##..## .##.... .##.... ..##... .....## .##..## .##..## .##..## .......
...##.. .....## .....## .##..## .#####. .##.... ....##. .##..## .##..## .##..## ...##..
...##.. ...###. ....##. .##..## .....## .#####. ...##.. ..####. ..##### .##..## .......
...##.. ..##... .....## .###### .....## .##..## ..##... .##..## .....## .##..## .......
...##.. .##.... .##..## .....## .##..## .##..## ..##... .##..## ....##. .##..## ...##..
..####. .###### ..####. .....## ..####. ..####. ..##... ..####. ..###.. ..####. .......
16x14,"1234567890:"
.......###..... .....######.... .....######.... ....###........ ..############. .......#####... ..############. .....######.... .....######.... .....######.... ...............
......####..... ...##########.. ...##########.. ....###........ ..############. .....#######... ..############. ...##########.. ...##########.. ...##########.. ...............
....######..... ..####....####. ..####....####. ....###........ ..###.......... ....####....... ..........####. ..####....####. ..####....####. ..####....####. ...............
....######..... ..###......###. ..###......###. ...####........ ..###.......... ...###......... ..........####. ..###......###. ..###......###. ..###......###. ...............
......####..... ...........###. ...........###. ...###......... ..###.......... ..###.......... .........####.. ..###......###. ..###......###. ..###......###. ......###......
......####..... ..........####. ..........###.. ...###....###.. ..##########... ..###.......... ........####... ...###....###.. ..####.....###. ..###......###. ......###......
......####..... ........#####.. .......#####... ..####....###.. ..###########.. ..#########.... .......####.... ....########... ...###########. ..###......###. ...............
......####..... ......#####.... .......######.. ..###.....###.. ..........####. ..###########.. ......####..... ...##########.. .....#########. ..###......###. ...............
......####..... ....#####...... ..........####. ..############. ...........###. ..###.....####. .....####...... ..####....####. ...........###. ..###......###. ...............
......####..... ...####........ ...........###. ..############. ...........###. ..###......###. ....####....... ..###......###. ...........###. ..###......###. ...............
......####..... ..####......... ..###......###. ..........###.. ..###......###. ..###......###. ....####....... ..###......###. ..........###.. ..###......###. ......###......
......####..... ..####......... ..####....####. ..........###.. ..####....####. ..####....####. ....####....... ..####....####. ........####... ..####....####. ......###......
....########... ..############. ...##########.. ..........###.. ...##########.. ...##########.. ....####....... ...##########.. ....#######.... ...##########.. ...............
....########... ..############. .....######.... ..........###.. .....######.... .....######.... ....####....... .....######.... ....#####...... .....######.... ...............
# vim:ft=perl nowrap:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment