File: gdc

package info (click to toggle)
libcurses-perl 1.08b-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 624 kB
  • ctags: 754
  • sloc: ansic: 8,714; perl: 1,773; makefile: 36; sh: 9
file content (149 lines) | stat: -rwxr-xr-x 3,407 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl
# This file a port from test/gdc.c in the ncurses-1.9.8a distribution.
# No copyright exists, but I don't think the writer would mind the port.
# It's not exact, because I was simplifying things to find a bug in my
# port.
#
# Also note that this is basically a direct port.  If it looks like C
# written in perl, that's because it is.  :-)
#
# /*
#  * Grand digital clock for curses compatible terminals
#  * Usage: gdc [-s] [n]   -- run for n seconds (default infinity)
#  * Flags: -s: scroll
#  *
#  * modified 10-18-89 for curses (jrl)
#  * 10-18-89 added signal handling
#  */

use ExtUtils::testlib;
use Curses;

$YBASE   = 10;
$XBASE   = 10;
$YDEPTH  =  5;
$XLENGTH = 54;

@disp = (075557, 011111, 071747, 071717, 055711, 074717,
	 074757, 071111, 075757, 075717, 002020);


$SIG{INT}  = \&sighndl;
$SIG{TERM} = \&sighndl;

initscr();
cbreak();
noecho();
clear();
refresh();

$n = -1;
for (@ARGV) {
    /-s/ and $scroll = 1;
    $n = $_;
}
$hascolor = eval { has_colors() };

if ($hascolor) {	
    start_color();
    init_pair(1, COLOR_BLACK, COLOR_RED);
    init_pair(2, COLOR_RED, COLOR_BLACK);
    init_pair(3, COLOR_WHITE, COLOR_BLACK);
    attrset(COLOR_PAIR(3));

    addch($YBASE - 1,  $XBASE - 1, ACS_ULCORNER);
    hline(ACS_HLINE, $XLENGTH);
    addch($YBASE - 1,  $XBASE + $XLENGTH, ACS_URCORNER);

    addch($YBASE + $YDEPTH,  $XBASE - 1, ACS_LLCORNER);
    hline(ACS_HLINE, $XLENGTH);
    addch($YBASE + $YDEPTH,  $XBASE + $XLENGTH, ACS_LRCORNER);

    move($YBASE,  $XBASE - 1);
    vline(ACS_VLINE, $YDEPTH);

    move($YBASE,  $XBASE + $XLENGTH);
    vline(ACS_VLINE, $YDEPTH);

    attrset(COLOR_PAIR(2));
}

while ($n--) {
    $mask = 0;
    $time = time;
    my($sec, $min, $hour) = localtime $time;
    set($sec  % 10,  0);
    set($sec  / 10,  4);
    set($min  % 10, 10);
    set($min  / 10, 14);
    set($hour % 10, 20);
    set($hour / 10, 24);
    set(10,          7);
    set(10,         17);
    foreach $k (0..5) {
	if($scroll) {
	    foreach $i (0..4) {
		$new[$i] = ($new[$i] & ~$mask) | ($new[$i+1] & $mask);
	    }
	    $new[5] = ($new[5] & ~$mask) | ($next[$k] & $mask);
	}
	else { $new[$k] = ($new[$k] & ~$mask) | ($next[$k] & $mask) }
	$next[$k] = 0;
	for($s = 1; $s >= 0; $s--) {
	    standt($s);
	    foreach $i (0..5) {
		if($a = (($new[$i] ^ $old[$i]) & ($s ? $new[$i] : $old[$i]))) {
		    for ($j = 0, $t = 1 << 26; $t; $t >>= 1, $j++) {
			if($a & $t) {
			    if(!($a & ($t << 1))) {
				move($YBASE + $i, $XBASE + 2*$j);
			    }
			    addstr("  ");
			}
		    }
		}
		if(!$s) { $old[$i] = $new[$i]; }
	    }
	}
	refresh();
    }
#    /* this depends on the detailed format of ctime(3) */
    my($ctime) = scalar localtime $time;
    addstr(16, 30, substr($ctime, 0, 10) . substr($ctime, 19));
    
    move(0, 0);
    refresh();
    sleep(1);
    if ($sigtermed) {
	last;
    }
}
standend();
clear();
refresh();
endwin();
print STDERR "gdc terminated by signal $sigtermed\n" if $sigtermed;

sub set {
    my($t, $n) = @_;
    my($m)     = 7 << $n;

    foreach $i (0..4) {
	$next[$i] |= (($disp[$t] >> (4-$i)*3) & 07) << $n;
	$mask     |= ($next[$i] ^ $old[$i]) & $m;
    }
    if ($mask & $m) { $mask |= $m }
}

sub standt {
    my($on) = @_;

    if ($on) { $hascolor ? attron(COLOR_PAIR(1)) : standout() }
    else     { $hascolor ? attron(COLOR_PAIR(2)) : standend() }
}

sub sighndl {
    local($sig) = @_;

    $sigtermed = $sig;
}