#!/usr/bin/perl -s use strict; use Term::ReadKey; use Time::HiRes; our ($v,$max,$sty,$m,$c,$g,$after); my $dsounds = "/usr/lib/libreoffice/share/gallery/sounds"; my $gong = "$dsounds/theetone.wav"; my $tik = "$dsounds/laser.wav"; if($g == 2){ $gong = "$dsounds/laser.wav"; $tik = "$dsounds/laser.wav"; } if($g == 3){ $gong = "$dsounds/gong.wav" ; $tik = "$dsounds/laser.wav"; } $m = shift if @ARGV; $sty ||= 1; $max ||= 60; my $warn ||= 600000; ## warn in the infinite $max = $m if $m; if($m =~ m{(\S+)/(\S+)}) { $warn=$1; $g||=1 ;$max=$2 } $v = 1 if $m; ## v = show current timer value $sty = 2 if $m; $max = $1*60 if $max =~ /((\d*\.)?\d+)m/i; $warn = $1*60 if $warn =~ /((\d*\.)?\d+)m/i; $max = $1*60+$2 if $max =~ /(\d+):(\d\d)/; $warn = $1*60+$2 if $warn =~ /(\d+):(\d\d)/; die("Can't find $gong\n") if $g && !can_gong(); die("Can't find $tik\n") if $g && !can_tik(); my $key='?'; clear() if $v || $c ; ReadMode 3; # Turn off controls keys my $start = Time::HiRes::time(); my $stoped = 0; my $display = -1; if($v){ $| = 1; ## stdout not buffered while (($key = ReadKey(0.01)) ne "\n") { if(defined $key){ print "\r ";## one key pressed print pp(0,Time::HiRes::time()-$start-$stoped)," "; if ($key eq '('){ label_point() } elsif($key eq ':'){ label_point2() } elsif($key eq '0'){ restart_timer();} elsif($key eq '-'){ pause_timer(); } elsif($key eq 'p'){ pause_timer(); } else { print "\n";} } else{ ## normal tic-tac my $t = int(Time::HiRes::time()-$start-$stoped); if ($t != $display){ print "\r",pp($sty,$display=$t),"\t\t";} } } } else { while (($key = ReadKey(0)) ne "\n") { print pp(0,Time::HiRes::time()-$start-$stoped)," "; if ($key eq '('){ label_point() } elsif($key eq ':'){ label_point2() } elsif($key eq '0'){ restart_timer();} elsif($key eq '-'){ pause_timer(); } elsif($key eq 'p'){ pause_timer(); } else { print "\n";} } } print "\n",pp(0,Time::HiRes::time()-$start-$stoped); leave(); sub leave{ ReadMode 0; # Reset tty mode before exiting print "\n"; system("$after &") if $after; exit 0; } sub label_point{ print " ("; while (($key = ReadKey(0)) ne ")") { print $key; } print ")\n"; } sub label_point2{ print " "; ReadMode 0; my $l = ReadLine(0); while (($l = ReadLine(0)) =~ /\S/) { } #{ print "\t$l"; } ReadMode 3; } sub pp{my ($sty,$t)=@_; ## PP time based on style if ($sty==0){ my $m=int($t/60); my $s=$t-60*$m; if($m==0){ return sprintf('%5.2f', $s);} else { return sprintf('%2d:%05.2f', $m,$s);} } if ($sty==1) { return sprintf('%02d:%02d', int($t/60), int($t)%60);} if ($sty==2){ my $a=$max-int($t); my $b=$warn-int($t); tik() if $warn && $b == 0; gong() if $g && $a == 0; leave() if $a < 0; return sprintf('%02d:%02d', int($a/60), $a %60); } # ERROR unknwou style ? } sub pause_timer{ ## wait until key; compute stoped time my $t= Time::HiRes::time(); print " paused - "; $key = ReadKey(0); print "restarted\n"; $stoped += (Time::HiRes::time() - $t); } sub restart_timer{ print "\n----------------Start\n "; $start = Time::HiRes::time(); $stoped = 0; $display = -1; } sub clear{ print "\e[H\e[2J"; } ##FIXME: this is ugly sub can_gong{ -f $gong }; sub can_tik { -f $tik }; sub gong{ system("cvlc -q --norepeat '$gong' vlc://quit 2>/dev/null & "); } sub tik { system("cvlc -q --norepeat '$tik' vlc://quit 2>/dev/null & "); } __END__ =encoding utf-8 =head1 NAME ptimer - a very simple chronemeter =head1 SYNOPSIS ptimer (start a silent count up timer) ptimer -v (show the counter evolution) ptimer 20 (count down 20s timer) ptimer -g 20 (count down 20s and ring in the end) ptimer 12m/15m (count down, warn at 12m ring at 15m) ptimer -c force clear screen (def in -v -g) ptimer -after='date' =head1 DESCRIPTION \n show time and leave - or p pause the chronometer (any key = continue) 0 restarts from 0 any key show the current time (and continue counting) (...) adds a timed subtitle =head1 AUTHOR J.João Almeida, jj@di.uminho.pt =head1 SEE ALSO perl(1). =cut cvlc /usr/lib/libreoffice/share/gallery/sounds/theetone.wav vlc://quit