module Mocal
Constants
- VERSION
Public Class Methods
build_cal(cal, label=false)
click to toggle source
# File lib/mocal.rb, line 38 def build_cal(cal, label=false) today = Date.today first = Date.new(cal.year, cal.month, 1) last = first.next_month - 1 buf = Array.new(8, "") # buf[0] = "#{'%2d' % cal.month}月 #{cal.year if label}".center(20) + " " # buf[1] = "月 火 水 木 金 土 日 " buf[0] = "#{cal.strftime('%B')} #{cal.year if label}".center(21) + " " buf[1] = "Mo Tu We Th Fr Sa Su " i = 2 buf[i] = " " * ((first.wday+6)%7) (first..last).each do |d| _day = "%2d" % d.day if STDOUT.isatty _day = _day.bright.underscore if d.holiday? || user_offday?(d) _day = _day.reverse if d == today end buf[i] += "#{_day} " if d.sunday? buf[i] += " " i += 1 end end buf[i] += " " * (7 - last.wday) + " " (i+1..7).each do |j| buf[j] = " " * 22 end buf#.map{|b| b+"|"} end
config_json()
click to toggle source
# File lib/mocal.rb, line 16 def config_json @config_json ||= open(File.expand_path("~/.mocal.json")) do |io| JSON.load(io) end rescue {} end
offdays()
click to toggle source
# File lib/mocal.rb, line 22 def offdays offday = [] json_data = config_json["offday"] || config_json["offdays"] || {} json_data.keys.each do |k| json_data[k].each do |d| offday << "#{k}-#{d}" end end offday end
print_month(year, month)
click to toggle source
# File lib/mocal.rb, line 69 def print_month(year, month) print_n_months(year, month, 1, true) end
print_n_months(year, month, n=3, label=false)
click to toggle source
# File lib/mocal.rb, line 73 def print_n_months(year, month, n=3, label=false) cal = Date.new(year, month, 1) m = [] n.times do |i| m << build_cal(cal, label) cal = cal.next_month end 8.times do |i| n.times {|j| print m[j][i] } print "\n" end print "\n" end
print_year(year)
click to toggle source
# File lib/mocal.rb, line 87 def print_year(year) col = [4, terminal_width/22].min puts "[ #{ARGV[0]} ]".center(col*22-2) (12/col).times {|m| print_n_months(year, m*col+1, col) } end
run(_argv)
click to toggle source
# File lib/mocal.rb, line 93 def run(_argv) case _argv.size when 0 today = Date.today.prev_month print_n_months(today.year, today.month, 3, true) when 1 argv = _argv[0].to_i if argv <= 12 print_month(Date.today.year, argv) else print_year(argv) end else print_month(_argv[1].to_i, _argv[0].to_i) end end
terminal_width()
click to toggle source
# File lib/mocal.rb, line 12 def terminal_width %x(stty size).split.map(&:to_i)[1] end
user_offday?(day)
click to toggle source
# File lib/mocal.rb, line 33 def user_offday?(day) @user_offdays ||= offdays @user_offdays.include?(day.to_s) end