-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbarclock
executable file
·71 lines (59 loc) · 1.8 KB
/
barclock
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
#!/usr/bin/env perl
#
# Barclock -- display current datetime as progress bars
#
# LICENSE: CC0
# To the extent possible under law, Kang-min Liu has waived all
# copyright and related or neighboring rights to barclock.
# This work is published from: Taiwan.
use v5.18;
use utf8;
use warnings;
package BarClock {
use Time::Piece;
sub new {
my ($class, %attrs) = @_;
return bless {
t => localtime(),
term_width => 0+`tput cols`,
attrs => \%attrs
}, $class;
}
sub tick {
my ($self) = @_;
$self->{t} = localtime();
}
sub display_bar {
my ($self, $left_text, $right_text, $current, $total) = @_;
my $width = $self->{term_width} - 20;
my $done = int($width * $current/$total);
my $togo = $width - $done;
my $bar = ("▓" x $done) . (" " x $togo);
my $x = sprintf ' %6s %'.$width.'s %-5s ', $left_text, $bar, $right_text;
utf8::encode($x);
print $x . "\n";
}
sub display {
my ($self) = @_;
my $t = $self->{t};
# system( "tput", "clear" );
print "\x1b\x5b\x48\x1b\x5b\x32\x4a";
$self->{term_width} = 0+`tput cols`;
print "\n";
$self->display_bar($t->year, $t->year+1, $t->yday, 365 + ($t->is_leap_year ? 1 : 0));
$self->display_bar($t->month, $t->add_months(1)->month, $t->mday, $t->month_last_day);
$self->display_bar($t->mday, $t->mday+1, $t->hour, 24);
$self->display_bar($t->hour, $t->hour+1, $t->min, 60);
$self->display_bar($t->min, $t->min+1, $t->sec, 60);
}
sub run {
my ($self) = @_;
while (1) {
$self->tick;
$self->display;
sleep 1;
}
}
};
my $bc = BarClock->new;
$bc->run;