… | |
… | |
3 | # this creates a simple digital clock by overwriting the refresh hooks |
3 | # this creates a simple digital clock by overwriting the refresh hooks |
4 | |
4 | |
5 | sub on_init { |
5 | sub on_init { |
6 | my ($self) = @_; |
6 | my ($self) = @_; |
7 | |
7 | |
|
|
8 | # force a refresh every second |
8 | $self->{digital_clock_refresh} = urxvt::timer |
9 | $self->{digital_clock_refresh} = urxvt::timer |
9 | ->new |
10 | ->new |
10 | ->start (urxvt::NOW) |
11 | ->start (urxvt::NOW) |
11 | ->cb (sub { |
12 | ->cb (sub { |
12 | $self->{digital_clock_refresh}->start ($self->{digital_clock_refresh}->at + 1); |
13 | $self->{digital_clock_refresh}->start ($self->{digital_clock_refresh}->at + 1); |
… | |
… | |
14 | }); |
15 | }); |
15 | |
16 | |
16 | () |
17 | () |
17 | } |
18 | } |
18 | |
19 | |
|
|
20 | # before refreshing: replace upper right with the clock display |
19 | sub on_refresh_begin { |
21 | sub on_refresh_begin { |
20 | my ($self) = @_; |
22 | my ($self) = @_; |
21 | |
23 | |
22 | my $time = sprintf "%2d:%02d:%02d", (localtime urxvt::NOW)[2, 1, 0]; |
24 | my $time = sprintf "%2d:%02d:%02d", (localtime urxvt::NOW)[2, 1, 0]; |
23 | my $xpos = $self->ncol - length $time; |
25 | my $xpos = $self->ncol - length $time; |
24 | |
26 | |
|
|
27 | $xpos >= 0 |
|
|
28 | or return; |
|
|
29 | |
25 | $self->{digital_clock_rend} = $self->ROW_r (0, [(0x00020000 + urxvt::DEFAULT_RSTYLE) x length $time], $xpos); |
30 | $self->{digital_clock_rend} = $self->ROW_r (0, [(urxvt::DEFAULT_RSTYLE) x length $time], $xpos); |
26 | $self->{digital_clock_text} = $self->ROW_t (0, $time, $xpos); |
31 | $self->{digital_clock_text} = $self->ROW_t (0, $time, $xpos); |
27 | |
32 | |
28 | () |
33 | () |
29 | } |
34 | } |
30 | |
35 | |
|
|
36 | # after refreshing: restore previous screen contents |
31 | sub on_refresh_end { |
37 | sub on_refresh_end { |
32 | my ($self) = @_; |
38 | my ($self) = @_; |
33 | |
39 | |
|
|
40 | exists $self->{digital_clock_text} |
|
|
41 | or return; |
|
|
42 | |
34 | $self->ROW_r (0, $self->{digital_clock_rend}); |
43 | $self->ROW_r (0, delete $self->{digital_clock_rend}); |
35 | $self->ROW_t (0, $self->{digital_clock_text}); |
44 | $self->ROW_t (0, delete $self->{digital_clock_text}); |
36 | |
45 | |
37 | () |
46 | () |
38 | } |
47 | } |
39 | |
48 | |
40 | |
49 | |