Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'active_support/time'
- class Calendar
- def build
- header
- body
- out
- end
- private
- def header
- @today = Date.today
- @target = @today.change day: 1
- header = ' SU MO TU WE TH FR SA'
- @out = ['', '']
- [-1, 0, 1].each do |m|
- m3 = @target.advance months: m
- @out[0] << "#{m3.year}-#{m3.month}".center(header.size) << ' '
- end
- 3.times do
- @out[1] << header << ' '
- end
- end
- def body
- [-1, 0, 1].each do |m|
- m3 = @target.advance months: m
- line_no = 2
- day = nil
- m3.upto((m3.advance months: 1) - 1) do |d|
- day = d
- @out[line_no] ||= ''
- @out[line_no] << ' ' if m != -1 and (d.day == 1 or d.wday == 0)
- @out[line_no] << ' ' * d.wday if d.day == 1
- if @today == d
- @out[line_no] << ' **'
- else
- @out[line_no] << '%3d' % d.day
- end
- line_no += 1 if d.wday == 6
- end
- @out.last << ' ' * (6 - day.wday)
- end
- end
- def out
- @out.each {|e| puts e }
- end
- end
- Calendar.new.build if $0 == __FILE__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement