Created
December 25, 2012 14:58
-
-
Save vadz/4373595 to your computer and use it in GitHub Desktop.
Modified version of cal in Perl6 from http://perl6advent.wordpress.com/2012/12/24/day-24-an-advent-calendar/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment