-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcal.pl
executable file
·224 lines (192 loc) · 5.89 KB
/
pcal.pl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use v5.010;
use DateTime;
use Path::Tiny 'path';
use Text::Table;
use Clone 'clone';
use Term::ANSIColor qw(colored);
use Getopt::Long qw(:config bundling pass_through);
my $VERSION = '0.3.2';
my $datafile = $ENV{HOME} . "/.config/pcal/pcal.dat";
my $mtb = Text::Table->new(
{ title => '', align_title => 'right' },
{ is_sep => 1, body => '│' },
map +{ title => $_, align_title => 'right' },
qw(Mo Tu We Th Fr Sa Su)
);
binmode STDOUT, ":utf8";
sub usage {
say <<EOB;
pcal (c)2018 Jari Matilainen
Usage: $0 [+|-<#>|<#> [<#>]] [-m|--month <month>] [-y|--year <year>] [-B|--before <before>]
[-A|--after <after>] [-H|--noholidays] [-F|--nofullweek] [-C|--nocolors]
[-v...v|--verbose] [-h|--help|--usage]
Details:
-|+<num> Display month that is <num> months before/after the current one
<month> [<year>] Display <month> for current year or for <year>
-m|--month <month> Display <month>
-y|--year <year> Display full <year> if -m not specified, otherwise display <month> of <year>
-B|--before <num> Display <num> months before the current one
-A|--after <num> Display <num> months after the current one
-H|--noholidays Hide Swedish holidays
-F|--nofullweek Hide display of full week at start and end of month
-C|--nocolors Don't use colors
-v|--verbose Print more verbose output
-h|--help|--usage Show this help
EOB
exit(0);
}
my @freeform;
my $add = 0;
my $holidays;
my ($month, $year, $before, $after, $hide_holidays, $hide_fullweek, $hide_colors, $verbose);
my $options = GetOptions(
'month|m=i' => \$month,
'year|y:i' => \$year,
'before|B=i' => \$before,
'after|A=i' => \$after,
'noholidays|H' => \$hide_holidays,
'nofullweek|F' => \$hide_fullweek,
'nocolors|C' => \$hide_colors,
'verbose|v+' => \$verbose,
'help|usage|h' => \&usage,
'<>' => \&process
);
unless($options) {
die "Failed to parse commandline\n";
}
my $dfy = defined $year && !$month;
my @errors;
push @errors, "You can't use -A and -B when displaying a full-year calendar" if $dfy && ($after || $before);
$before = $before ? abs($before) * -1 : 0;
$after = $after ? abs($after) : $dfy ? 11 : 0;
$hide_holidays = 0 unless $hide_holidays;
die join("\n", @errors) . "\n" if @errors;
my $now = DateTime->today(time_zone => 'local');
$month //= $freeform[0] // $now->month;
$year ||= $freeform[1] // $now->year;
if($dfy) {
$now->set(year => $year);
$now->truncate(to => 'year');
}
else {
$now->set(month => $month);
$now->set(year => $year);
$now->add(months => $add) if $add;
}
begin();
my @calendars;
for($before .. $after) {
state $index = 0;
state $i = 0;
$i++;
my $cal = $now->clone;
$cal->add(months => $_);
push @{$calendars[$index]}, { title => '', table => '' } if $calendars[$index];
push @{$calendars[$index]}, make_table($cal);
if($i == 3) {
$index++;
$i = 0;
}
}
for my $c (@calendars) {
my $tb = Text::Table->new(map +{ title => $_->{title}, align_title => 'center' }, @$c);
$tb->add(map { $_->{table} } @$c);
say $tb;
}
end();
##########################################################
sub process {
my $input = shift;
if($input =~ /^[+-]\d+/) {
$add = 0+$input unless $add;
}
else {
push @freeform, $input if $input =~ /\d+/;
}
}
sub fetch_holidays {
my $year = shift;
say "Fetching data for $year" if $verbose;
require Mojo::UserAgent;
return [ Mojo::UserAgent->new->max_redirects(5)->get("https://www.kalender.se/helgdagar/$year")->result->dom('table.table.table-striped tbody tr td:last-child')->map('text')->each ];
}
sub is_holiday {
my $cal = shift;
if($cal->dow == 7) {
return 1;
}
if(!$hide_holidays) {
unless(exists $holidays->{$cal->year}) {
$holidays->{$cal->year} = fetch_holidays($cal->year);
}
if(grep { $_ == $cal->doy } @{$holidays->{$cal->year}} ) {
return 1;
}
}
return 0;
}
sub make_table {
my ($cal) = @_;
my @rows;
my $som = $cal->truncate(to => 'month');
my $yearnumber = $som->year;
my $monthname = $som->month_name;
my $t_month = $som->month;
$som = $cal->truncate(to => 'week') unless $hide_fullweek;
do {
my $week = $som->week_number;
my $wstr = sprintf("%2s", $week);
my @days = $hide_colors ? $wstr : colored($wstr, 'on_grey10');
unless($hide_fullweek) {
while($som->month != $t_month) {
push @days, $hide_colors ? $som->day : colored($som->day, 'grey5');
$som->add(days => 1);
}
}
else {
push @days, ('') x (($som->dow) - 1);
}
do {
my $cur = !$hide_colors && $som->doy == DateTime->now->doy && $cal->year == DateTime->now->year ? colored($som->day, 'reverse') : $som->day;
push @days, !$hide_colors && is_holiday($som) ? colored($cur, 'bright_red') : $cur;
$som->add(days => 1);
} while $week == $som->week && $som->day != 1;
unless($hide_fullweek) {
while($week == $som->week) {
push @days, $hide_colors ? $som->day : colored($som->day, 'grey5');
$som->add(days => 1);
}
}
push @rows, [ @days ];
} while $som->month == $t_month;
my $ttb = clone $mtb;
$ttb->load(@rows);
return { title => "$monthname $yearnumber", table => $ttb->stringify };
}
sub begin {
if(!$hide_holidays && path($datafile)->exists) {
eval {
require File::Slurper;
File::Slurper->import(qw(write_binary read_binary));
require Sereal::Decoder;
Sereal::Decoder->import(qw(sereal_decode_with_object));
require Sereal::Encoder;
Sereal::Encoder->import(qw(sereal_encode_with_object));
};
die $@ if $@;
$holidays = sereal_decode_with_object(Sereal::Decoder->new, read_binary($datafile));
}
}
sub end {
unless($hide_holidays) {
my $path = path($datafile);
if(!$path->exists) {
$path->touchpath;
}
write_binary($datafile, sereal_encode_with_object(Sereal::Encoder->new, $holidays));
}
}