class Shellout::Calendar
Public Class Methods
new(date=Date.today)
click to toggle source
# File lib/shellout/calendar.rb, line 6 def initialize(date=Date.today) @date = date @list_of_days = "Mo Tu We Th Fr Sa Su" end
Public Instance Methods
print(out=$stdout)
click to toggle source
# File lib/shellout/calendar.rb, line 11 def print(out=$stdout) print_helper(out, Array(@date)) end
print3(out=$stdout)
click to toggle source
# File lib/shellout/calendar.rb, line 15 def print3(out=$stdout) print_helper(out, [@date.prev_month, @date, @date.next_month]) end
Private Instance Methods
ansi_reverse_color(s)
click to toggle source
# File lib/shellout/calendar.rb, line 120 def ansi_reverse_color(s); "\e[7m#{s}\e[0m"; end
days(for_date)
click to toggle source
# File lib/shellout/calendar.rb, line 98 def days(for_date) days = (1..last_day_of_month(for_date).day).to_a unshift_empty_strings_for_missing_week(days, for_date) end
first_day_of_month(date)
click to toggle source
# File lib/shellout/calendar.rb, line 112 def first_day_of_month(date) Date.new(date.year, date.month) end
is_current_month?(date)
click to toggle source
# File lib/shellout/calendar.rb, line 90 def is_current_month?(date) Date.today.month == date.month end
is_current_year?(date)
click to toggle source
# File lib/shellout/calendar.rb, line 94 def is_current_year?(date) Date.today.year == date.year end
last_day_of_month(date)
click to toggle source
# File lib/shellout/calendar.rb, line 116 def last_day_of_month(date) first_day_of_month(date).next_month-1 end
print_days(out, dates)
click to toggle source
# File lib/shellout/calendar.rb, line 62 def print_days(out, dates) # FIXME: UGLY!!! foo = dates.map do |for_date| bar = days(for_date) if is_current_month?(for_date) && is_current_year?(for_date) bar.map! {|d| d == Date.today.day ? ansi_reverse_color(d) : d} end bar end i = 0 while true num_done = 0 foo.each do |bar| if i >= bar.length num_done += 1 else week = bar[i..i+6] week = push_empty_strings_for_missing_week_days(week) out.print week.map {|s| "%2s" % s}.join " " out.print " " end end break if num_done == foo.length i += 7 out.puts end end
print_heading_months(out, dates)
click to toggle source
# File lib/shellout/calendar.rb, line 44 def print_heading_months(out, dates) dates.each do |for_date| head = Date::MONTHNAMES[for_date.month] head += " #{for_date.year}" unless print_year_on_a_separate_line?(dates) out.print head.center(@list_of_days.length) out.print " " end out.puts end
print_heading_name_of_days(out, dates)
click to toggle source
# File lib/shellout/calendar.rb, line 54 def print_heading_name_of_days(out, dates) dates.each do |for_date| out.print @list_of_days out.print " " end out.puts end
print_heading_year(out, dates)
click to toggle source
# File lib/shellout/calendar.rb, line 38 def print_heading_year(out, dates) if print_year_on_a_separate_line?(dates) out.puts dates.first.year.to_s.center((@list_of_days.length + 2) * (dates.length) - 2) end end
print_headings(out, dates)
click to toggle source
# File lib/shellout/calendar.rb, line 26 def print_headings(out, dates) print_heading_year(out, dates) print_heading_months(out, dates) print_heading_name_of_days(out, dates) end
print_helper(out, dates)
click to toggle source
# File lib/shellout/calendar.rb, line 21 def print_helper(out, dates) print_headings(out, dates) print_days(out, dates) end
print_year_on_a_separate_line?(dates)
click to toggle source
# File lib/shellout/calendar.rb, line 32 def print_year_on_a_separate_line?(dates) first_year = dates.first.year same_year = dates.reduce(true) {|res, d| res && d.year == first_year} same_year and dates.length > 1 end
push_empty_strings_for_missing_week_days(week)
click to toggle source
# File lib/shellout/calendar.rb, line 108 def push_empty_strings_for_missing_week_days(week) week + Array.new(7 - week.length, "") end
unshift_empty_strings_for_missing_week(days, date)
click to toggle source
# File lib/shellout/calendar.rb, line 103 def unshift_empty_strings_for_missing_week(days, date) wday = (first_day_of_month(date).wday - 1) % 7 Array.new(wday, "") + days end