#!/usr/bin/ruby

var mons = [
              31,
              28,
              31,
              30,
              31,
              30,
              31,
              31,
              30,
              31,
              30,
              31,
           ]

var t = Time.local

var day  = t.day;
var mon  = t.mon;
var year = t.year;

if (year % 400 == 0 || (year % 4 == 0 && (year % 100 != 0))) {
    mons[1] = 29
}

year--
var st = (1 + year*365 + year//4 - year//100 + year//400);
{|i| st += mons[i] } * (mon-1)

#
## Displaying the calendar
#

var monName = t.strftime("%B");

"%*s\n%s\n".printf(
                    (monName.len + 5) / 2 + 11,
                    "%s %d".sprintf(monName, year + 1),
                    "Su Mo Tu We Th Fr Sa",
                  );

' ' * 3 * (st % 7) -> print;

for i in (1 .. mons[mon-1]) {
    printf(i == day ? "\e[7m%2d\e[0m " : "%2d ", i);
    st+i %% 7 && ((i != mons[mon-1]) && print "\n");
}

print "\n"
