1 ! Copyright (C) 2016 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3 USING: accessors calendar calendar.english combinators
4 command-line formatting grouping io kernel math.parser
5 math.ranges namespaces sequences sequences.extras strings.tables ;
10 : days ( timestamp -- days )
12 [ day-of-week " " <repetition> ]
13 [ days-in-month [1,b] [ "%2d" sprintf ] map ] bi append
16 : month-header ( timestamp -- str )
17 "%B %Y" strftime 20 CHAR: \s pad-center ;
19 : year-header ( timestamp -- str )
20 "%Y" strftime 64 CHAR: \s pad-center ;
22 : month-rows ( timestamp -- rows )
23 days 7 group day-abbreviations2 prefix format-table ;
27 : month. ( timestamp -- )
28 [ month-header print ] [ month-rows [ print ] each ] bi ;
30 : year. ( timestamp -- )
31 dup year-header print nl 12 [1,b] [
32 >>month [ month-rows ] [ month-name ] bi
33 20 CHAR: \s pad-center prefix
35 [ first3 [ "%s %s %s\n" printf ] 3each ] each ;
39 : cal-args ( -- timestamp year? )
40 now command-line get [
44 { "-m" [ rest ?first2 swap f ] }
45 { "-y" [ rest ?first2 dup [ swap ] when t ] }
46 [ drop ?first2 dup [ swap ] when dup not ]
50 [ [ >>month ] when* ] bi*
57 cal-args [ year. ] [ month. ] if ;