Advertisement
cd62131

calendar

Jul 5th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.12 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'active_support/time'
  3.  
  4. class Calendar
  5.   def build
  6.     header
  7.     body
  8.     out
  9.   end
  10.   private
  11.   def header
  12.     @today = Date.today
  13.     @target = @today.change day: 1
  14.     header = ' SU MO TU WE TH FR SA'
  15.     @out = ['', '']
  16.     [-1, 0, 1].each do |m|
  17.       m3 = @target.advance months: m
  18.       @out[0] << "#{m3.year}-#{m3.month}".center(header.size) << '  '
  19.     end
  20.     3.times do
  21.       @out[1] << header << '  '
  22.     end
  23.   end
  24.   def body
  25.     [-1, 0, 1].each do |m|
  26.       m3 = @target.advance months: m
  27.       line_no = 2
  28.       day = nil
  29.       m3.upto((m3.advance months: 1) - 1) do |d|
  30.         day = d
  31.         @out[line_no] ||= ''
  32.         @out[line_no] << '  ' if m != -1 and (d.day == 1 or d.wday == 0)
  33.         @out[line_no] << '   ' * d.wday if d.day == 1
  34.         if @today == d
  35.           @out[line_no] << ' **'
  36.         else
  37.           @out[line_no] << '%3d' % d.day
  38.         end
  39.         line_no += 1 if d.wday == 6
  40.       end
  41.       @out.last << '   ' * (6 - day.wday)
  42.     end
  43.   end
  44.   def out
  45.     @out.each {|e| puts e }
  46.   end
  47. end
  48.  
  49. Calendar.new.build if $0 == __FILE__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement