Ascii-clock:
There is an analog clock showing 10h10'30":
. . . 12. . . 11. . 1 . . . . . . 10 2 . M M . . H M . . H H M M . . H M . 9 H M 3 . . . . . . . . 8 4 . . . . . . 7 . s . 5 . . . 6 . . .
On this page, you will find some implementations:
Feature | Description |
---|---|
Variables | Accept 1 argument: radius of display, |
Default | By default, outer size is 25x25 |
Sync | To ensure sync, sleep time is computed each second |
Drawing | Full circle, full hour and minute path and a seconds tick |
Numeric | Hours, Minutes and Seconds will be prompted in corners |
Sleep | Top left corner display a `S` during sleep times |
Perl, Perl/PNG, Bash, Javascript
- A Perl version:
#! /usr/bin/perl -s use Time::HiRes qw|sleep time| ;$k=11;$k=$ARGV[0]if$ARGV[0];$g=atan2 (1,1)/7.5;$e=4*$k;$d=2*$k+1;@r=(0..2*$d);sub p{printf@_} sub b{ return "\e[1m". pop."\e[0m"} ;$| =p"\e[?25l". "\e[H\e[J";$SIG {'INT'} =sub {p"\e[?12l" ."\e[?25h" ."\e" ."[%" ."dH\e[J", $d+2;exit ;}; @c= map{[map{ " "}@r]}( 0..2*$d); @m=map{[ @{$c[$_] }]}@r;sub c{($b,$o,$q) =@_;$c[$k-$o* cos ($b*$g)+.5][$k+$o *sin($b* $g)+.5]=$q;} for$x (0..$k){$y =int( sqrt($k **2-$x**2)+ .5);$c[$k-$x][ $k-$y] =".";$c [$k+$x][$k-$y] =".";$c[$k-$x][ $k+$y]= ".";$c [$k+$x][$k+$y] =".";$c[$k-$y][$ k-$x]= ".";$c[ $k+$y][$k -$x]=".";$c[$k-$y][$k+$x ]=".";$ c[$k+$y ][$k+$x]="."};map{c$_*5,$k,b$ _}(1..12 );@p=map{[@{$ c[$_]}]}@r;while(::){ @t=localtime ;p"\e[H\e[1;" .${e}."H%02d\e[${d }H%02d\e[${d };${e}" ."H%02d",$t[2],$t[1] ,$t[0]; @c=map {[@{$p[$_]}]}(0.. 2*${d}) ;for$i ('H:' ."65:" ."5:2:4" ,"M:78:" . 1 .":1:2" ,"s:87" .":1:0" .":6"){ ($ l, $n,$j,$ s,$f)=split":" ,$i;if ($a){$h =0;map{$h=$h /60+$t[$_]}( 6*0..$s) }else{$h =$t[$s] };map{c $j*$h,$_, b( "" ."\e[3${f }m$l")}(do {$s? 1:$n }/100*$k.. $n/100*$k) ;}$c [$k ][$k ]=b((".","" ."+","*","o"x1, "O" )[$t[.0]%5]); for$x(@r){for $y( @r){$m[$ x][$ y]ne$c[$x][$y]?p"\e[%d;%dH".$c[$x][$y],$x+1,2 *$y+1:''};};@m=map{[@{$c[$_]}]}@r;p"\e[" ."H\e[7mS\e[0m";sleep 1.01-$1if time=~/(\..*)$/;}
ascii-clock.pl - A Perl in PNG version: could be run by using following command:
curl fhauri.cartou.ch/ascii-clock/ascii-clock.png | perl -e ' use GD;GD::Image->trueColor(1);$i=GD::Image->newFromPng(STDIN );my($x,$y)=$i->getBounds();for my$k(0..$x-1){for my$l(0..$y- 1){$_.=pack"U"x3,$i->rgb($i->getPixel($k,$l))};};eval'
or, for using argument "-a":curl fhauri.cartou.ch/ascii-clock/ascii-clock.png | perl -s <(echo ' use GD;GD::Image->trueColor(1);$i=GD::Image->newFromPng(STDIN );my($x,$y)=$i->getBounds();for my$k(0..$x-1){for my$l(0..$y- 1){$_.=pack"U"x3,$i->rgb($i->getPixel($k,$l))};};eval') -a
You may need to install GD libraries for Perl. Under Debian:sudo apt-get install libgd-gd2-perl
- A Bash version, for challenge
#!/bin/bash # Analog clock in Ascii-Art written in BASH V4.2 += RAY=${1:-12} NowH=$2 NowM=$3 LANG=C # Wait until next second, using nanosecond infos # there is a need to store offset in a static var if [ -f /proc/timer_list ] ;then _c=10 mapfile </proc/timer_list _timer_list for ((_i=0;_i<${#_timer_list[@]};_i++));do ((_c+=${#_timer_list[_i]})) [[ ${_timer_list[_i]} =~ ^now ]] && TIMER_LIST_READ=$_c [[ ${_timer_list[_i]} =~ offset:.*[1-9] ]] && \ TIMER_LIST_OFFSET=${_timer_list[_i]//[a-z.: ]} && \ break done unset _i _timer_list _c readonly TIMER_LIST_OFFSET TIMER_LIST_READ waitNextSecondHires() { local nsnow nsslp read -N$TIMER_LIST_READ nsnow </proc/timer_list nsnow=${nsnow%% nsecs*} nsnow=$((${nsnow##* }+TIMER_LIST_OFFSET)) nsslp=$((2000000000-10#${nsnow:${#nsnow}-9})) read -t .${nsslp:1} foo } else waitNextSecondHires() { local _ut read -d\ _ut < /proc/uptime _ut=$((200-10#${_ut#*.})) read -t .${_ut:1} foo } fi # Square root using integer sqrt() { local -a _xx=(600000 200000) local _x1=${_xx[$(($1&1))]} _x0=1 while [ $_x0 -ne $_x1 ] ;do _x0=$_x1 [ $_x0 -eq 0 ] && _x1=0000 || printf -v _x1 "%u" $(( (${_x0}000 + ${1}00000000000/${_x0} )/2 )) printf -v _x1 "%.0f" ${_x1:0:${#_x1}-3}.${_x1:${#_x1}-3} done _x1=0000$_x1 printf -v $2 "%.3f" ${_x1:0:${#_x1}-4}.${_x1:${#_x1}-4} } # Sin/Cos, but only 60 point for the circle. Use of a table. clksin() { # $1:moment [0-60], $2:path length, $3:variable name local _csin=(0 104528 207912 309017 406737 500000 587785 669131 743145 809017 866025 913545 951057 978148 994522 1000000) local xsign=1 x=$1 ysign=-1 y=$1 [ $x -gt 30 ] && xsign=-1 x=$((60-x)) [ $x -gt 15 ] && x=$((30-x)) x=00000$((RAY*1000000+xsign*${2:-10}*${_csin[$x]})) [ $y -gt 30 ] && y=$((60-y)) [ $y -gt 15 ] && ysign=1 y=$((30-y)) y=00000$((RAY*1000000+ysign*${2:-10}*${_csin[15-$y]})) printf -v $3 "%.0f %.0f" \ ${y:0:${#y}-6}.${y:${#y}-6} ${x:0:${#x}-6}.${x:${#x}-6} }; SLEN=000$((870*RAY)) printf -v SLEN "%.0f" ${SLEN:0:${#SLEN}-3}.${SLEN:${#SLEN}-3} MLEN=000$((780*RAY)) printf -v MLEN "%.0f" ${MLEN:0:${#MLEN}-3}.${MLEN:${#MLEN}-3} HLEN=000$((650*RAY)) printf -v HLEN "%.0f" ${HLEN:0:${#HLEN}-3}.${HLEN:${#HLEN}-3} declare -A ticks for ((i=1;i<=12;i++));do clksin $((5*(i%12))) $RAY tick ticks[$tick]=$i done printf -v centers "\e[1m%s\e[0m " . + \* o O centers=(${centers}) ticks["$RAY $RAY"]=${centers} printf "\e[?25l\e[H\e[J" # Civis Home Clear trap "printf '\e[?12l\e[?25h\e[$((2*RAY+3))H\e[J';exit" 0 1 2 3 6 9 15 printf -v DIGITAL "\\e[1;%dH%%(%%H)T\\e[%dH%%(%%M)T\\e[%d;%dH%%(%%S)T" \ $((4*RAY)) $((2*RAY+1)) $((2*RAY+1)) $((4*RAY)) declare -A map map["$((2*RAY)) $((2*$RAY))"]=" " while :;do [ "$NowM" ] || printf -v NowM "%(%M)T" -1 unset MPath declare -A MPath for ((i=1;i<=MLEN;i++));do clksin ${NowM#0} $i tick MPath[$tick]=M done [ "$NowH" ] || printf -v NowH "%(%H)T" -1 unset HPath declare -A HPath for ((i=1;i<=HLEN;i++));do clksin $((5*(${NowH#0}%12))) $i tick HPath[$tick]=H done printf -v NowS "%(%S)T" -1 clksin ${NowS#0} $SLEN STick [ "$2" ] || echo -en \\e[H; # ANSI sequence for top left of console for ((i=0;i<=2*RAY;i++));do x=$((RAY-i)) sqrt $((RAY**2 - ${x#-}**2 )) y0 printf -v y0 "%.0f" $y0 for ((l=0;l<=2*RAY;l++));do y=$((RAY-l)); sqrt $((RAY**2 - ${y#-}**2 )) x0 printf -v x0 "%.0f" $x0 c=" " if [ "$i $l" = "$STick" ] ;then c=$'\e[36;1ms \e[0m' elif [ "${MPath["$i $l"]}" ] ;then c=$'\e[32;1m'${MPath["$i $l"]}$' \e[0m' elif [ "${HPath["$i $l"]}" ] ;then c=$'\e[34;1m'${HPath["$i $l"]}$' \e[0m' elif [ "${ticks["$i $l"]}" ] ;then printf -v c "%-2s" "${ticks["$i $l"]}" elif [ ${x#-} -eq $x0 ] || [ ${y#-} -eq $y0 ] ;then c=.\ else c=" " fi [ "$c" != "${map["$i $l"]}" ] && map["$i $l"]="$c" && printf "\e[%s;%sH%-2s" $((1+i)) $((1+l*2)) "$c" done done [ "$2" ] && break # Exit if at least Hour was specified printf "\e[H\e[7mS\e[0m" waitNextSecondHires printf "\e[H $DIGITAL" -1 -1 -1 ticks["$RAY $RAY"]=${centers[$((10#$NowS%${#centers[@]}))]} unset NowH NowM done
Warning: Do not use!!
Even, if this script works quite well, thebash
is not a programming language. Without real garbage collector, this script is going to consume memory chronically:
The main advantage is that after a few hours, if you feel a need for memory, just kill time ascii-clock.shAfter CPU (%) Memory (%) Memory (Kb) 0'30" 0.0% 21.6% 12.98M 10'30" 1.0% 20.9% 48.91M 1h 0'30" 5.6% 20.8% 228.63M 2h 0'31" 11.2% 20.8% 444.25M 3h 0'32" 16.8% 20.8% 659.91M 5h 0'00" 27.9% 20.8% 1.064G - A Javascript version
In top right corner of this page, size are changeable: , , , .
To see this in his own window: , ascii-clock?12, or even .