#!/usr/bin/perl
#
# Triangular clock written by Tony Finch <dot@dotat.at>
# Inspired by http://arxiv.org/pdf/1006.1373v1.pdf
#
# You may do anything with this. It has no warranty.
# <http://creativecommons.org/publicdomain/zero/1.0/>
#
# $dotat: scripts/triclock,v 1.5 2013/05/16 15:00:09 fanf2 Exp $

use integer;
use warnings;
use strict;
use POSIX;

sub triclock {
	my $t = $_[2]*60 + $_[1];
	for (my ($m,$w) = (1,6); $w > 1; $m *= $w--) {
		my $n = $t % $w; $t /= $w;
		printf "  %-3d%s\e[1m%s\e[0m%s\n",
		    $m, "  " x (6 - $w),
		      " (o)" x $n,
		      " ( )" x ($w - $n - 1);
	}
}

$SIG{INT} = sub {
	# normal cursor
	print "\e[?25h", "\n" x 7;
	exit;
};

# clear all; move to top left; hide cursor
print "\e[2J\e[H\e[?25l\n";

for (;;) {
	my @t = localtime;
	print strftime "  %Y-%m-%d %H:%M:%S %z\n", @t;
	triclock @t;
	print "\e[6A"; # cursor up 6
	sleep 1;
}
