module Year where
import System.Environment (getArgs)
import Calendar
import Picture
import S03
import Data.List
rjustify :: Int -> String -> String
rjustify n xs
| l <= n = replicate (n l) ' ' ++ xs
| otherwise = error ("text of length " ++ show l ++ " does not fit in box of width " ++ show n)
where
l = length xs
months :: [String]
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
groupsOfSize :: Int -> [a] -> [[a]]
groupsOfSize n xs = if null ys then [] else ys : groupsOfSize n zs
where
(ys, zs) = splitAt n xs
daysOfMonth :: Month -> Year -> [Picture]
daysOfMonth m y =
map (row . rjustify 3 . pic) [1d..42d]
where
(d, t) = monthInfo m y
pic n = if 1 <= n && n <= t then show n else ""
month :: Month -> Year -> Picture
month m y = tile $ groupsOfSize 7 $ daysOfMonth m y
monthPic :: Year -> Month -> Picture
monthPic y m = smartStack [header, weekdays, month m y]
where
weekdays = row " Su Mo Tu We Th Fr Sa"
header = row (months !! (m1))
yearPic :: Year -> Int -> Picture
yearPic y c = smartTile $
map (intersperse (row " ")) $
groupsOfSize c $ map (monthPic y) [1..12]
printYear y c = putStr $ showPic $ smartStack [row (show y), row "", yearPic y c]
main = do
args <- getArgs
case args of
[y] -> printYear (read y) 3
[y, c] -> printYear (read y) (read c)
_ -> error "expecting 1 or 2 arguments (year and optional number of columns)"