public
Created

  • Download Gist
cal.pl
Perl
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
#!/usr/bin/env perl6
 
use Term::ANSIColor;
 
constant @months = <January February March April May June July
August September October November December>;
my @days = <Su Mo Tu We Th Fr Sa>;
 
 
sub center(Str $text, Int $width) {
my $prefix = ' ' x ($width - $text.chars) div 2;
my $suffix = ' ' x $width - $text.chars - $prefix.chars;
return $prefix ~ $text ~ $suffix;
}
 
sub MAIN(Bool :$m = False, Int :$year = Date.today.year, Int :$month = Date.today.month) {
my $dt-month-start = Date.new(:year($year), :month($month), :day(1) );
my $dt = $dt-month-start;
my $ss = ($dt.day-of-week - $m) % 7;
my @slots = ' ' xx $ss;
 
if $m {
@days.push: @days.shift
}
 
my $days-in-month = $dt.days-in-month;
for $ss ..^ $ss + $days-in-month {
@slots[$_] = $dt.day.fmt("%2d");
$dt++
}
 
my $weekdays = @days.join: " ";
my $months = center(@months[$month-1] ~ " " ~ $year, $weekdays.chars);
my $output = $months ~ "\n" ~ $weekdays ~ "\n";
 
my $today = Date.today;
$dt = $dt-month-start;
for @slots.kv -> $k, $v {
$output ~= BOLD if $dt == $today;
$output ~= "$v ";
$output ~= RESET if $dt == $today;
next if $v eq ' ';
$dt++;
$output ~= "\n" if ($k+1) %% 7 or $v == $days-in-month;
}
 
print $output;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.