Permalink
Browse files

fix holiday name footer

change some names
  • Loading branch information...
1 parent 68f73da commit e3902c4943cd67190effafa72456f802d257b82a @melborne committed Dec 15, 2010
Showing with 119 additions and 80 deletions.
  1. +2 −2 lib/caline.rb
  2. +4 −5 lib/column.rb
  3. +42 −22 lib/month.rb
  4. +9 −9 lib/year.rb
  5. +57 −37 test/tc_month.rb
  6. +5 −5 test/tc_year.rb
View
@@ -2,12 +2,12 @@
#-*-encoding: utf-8-*-
require "date"
require "term/ansicolor"
-require_relative "gcalendar"
require_relative 'month'
require_relative 'year'
-
+require_relative "gcalendar"
module Caline
String.send(:include, Term::ANSIColor)
+ VERSION = '1.0'
end
View
@@ -1,12 +1,11 @@
module Caline
- module Column
- def three_column_form
+ module ColumnForm
+ def three_columns_formatter
lambda do |months, from, color|
out, year_label = [], nil
months.each_slice(3) do |gr|
left, center, right = gr.map do |mon|
- mon.holidays = @holidays if @holidays
- mon.format(:week, from, color)
+ mon.format(:block, from, color, false)
end
left, center, right = align_size(left, center, right)
year_label, *body = left.zip(center, right).map { |line| line.join(" ") }
@@ -32,4 +31,4 @@ def MONTH_WIDTH
20
end
end
-end
+end
View
@@ -1,9 +1,10 @@
#!/usr/bin/env ruby
#-*-encoding: utf-8-*-
require_relative "column"
+
module Caline
class Month
- include Column
+ include ColumnForm
@@holidays = Hash.new{ |h, k| h[k] = {} }
attr_reader :year, :month, :last, :colors
@@ -23,29 +24,31 @@ def dates(from=0, pre=true, pos=true)
pre + (@first..@last).to_a + pos
end
- def dates_by_week(from=0)
+ def dates_by_block(from=0)
dates(from).each_slice(7)
end
alias formaty format
- def format(style=:week, from=0, color=false)
- form = color ? color_proc : mono_proc
- case style
- when :week
- header(from, color, :block) +
- dates_by_week(from).map { |w| form[w] }
- when :month
- header(from, color, :line) +
- Array( form[dates from, true, false] )
- when :week3
- months = [self-1, self, self+1]
- three_column_form[months, from, color]
- else raise ArgumentError
- end
- end
-
- def color_format(style=:week, from=0)
- format(style, from, true)
+ def format(style=:block, from=0, color=false, footer=true)
+ formatter = color ? color_proc : mono_proc
+ body =
+ case style
+ when :block
+ header(from, color, :block) +
+ dates_by_block(from).map { |w| formatter[w] }
+ when :line
+ header(from, color, :line) +
+ Array( formatter[dates from, true, false] )
+ when :block3
+ months = [self-1, self, self+1]
+ three_columns_formatter[months, from, color]
+ else raise ArgumentError
+ end
+ footer ? body + holiday_names(style==:block3 ? months.map(&:month) : Array(@month)) : body
+ end
+
+ def color_format(style=:block, from=0, footer=true)
+ format(style, from, true, footer)
end
def colors=(colors)
@@ -60,15 +63,26 @@ def holidays=(code)
def +(month)
mon = @first.next_month(month)
- Month.new(mon.year, mon.mon, @colors)
+ m = Month.new(mon.year, mon.mon, @colors)
+ set_holiday(m)
end
def -(month)
mon = @first.prev_month(month)
- Month.new(mon.year, mon.mon, @colors)
+ m = Month.new(mon.year, mon.mon, @colors)
+ set_holiday(m)
end
private
+ # set holidays opt if base month has set holidays
+ def set_holiday(mon)
+ if @@holidays[@year][@code]
+ mon.holidays = @code; mon
+ else
+ mon
+ end
+ end
+
def last_date(year, month, day=[28, 29, 30, 31])
Date.new(year, month, day.pop) rescue retry
end
@@ -145,6 +159,12 @@ def week_label(from, style)
when :line then (weeks + " ") * 5
end
end
+
+ def holiday_names(months)
+ return [] unless @@holidays[@year][@code]
+ @@holidays[@year][@code].select { |d, _| months.include? d.mon }.sort_by { |d, _| d }
+ .map { |date, name| date.strftime('%_m/%_d').yellow + ": " + name.green }
+ end
end
end
View
@@ -3,31 +3,31 @@
require_relative "column"
module Caline
class Year
- include Column
+ include ColumnForm
def initialize(year, opts={})
@year = year
@holidays = opts.delete(:holidays)
@months = (1..12).map { |m| Caline::Month.new(@year, m, opts) }
end
alias formaty format
- def format(style=:week, from=0, color=false)
+ def format(style=:block, from=0, color=false, ho_name=true)
out, year_label = [], nil
case style
- when :week
- three_column_form[@months, from, color]
- when :month
+ when :block
+ three_columns_formatter[@months, from, color, ho_name]
+ when :line
@months.each_with_index do |mon, i|
mon.colors = {neighbor: :black}
- year_label, *body = mon.format(:month, from, color)
+ year_label, *body = mon.format(:line, from, color, ho_name)
out << body
end
out.unshift year_label
end
end
- def color_format(style=:week, from=0)
- format(style, from, true)
+ def color_format(style=:block, from=0, ho_name=true)
+ format(style, from, true, ho_name)
end
end
-end
+end
View
@@ -22,7 +22,7 @@ def test_dates
assert_equal((1..31).to_a, @m.dates(0, false, false).map(&:day))
end
- def test_dates_by_week
+ def test_dates_by_block
f2010_12_from0 = [
[28, 29, 30, 1, 2, 3, 4],
[5, 6, 7, 8, 9, 10, 11],
@@ -37,46 +37,66 @@ def test_dates_by_week
[20, 21, 22, 23, 24, 25, 26],
[27, 28, 29, 30, 31, 1, 2]
]
- assert_equal(f2010_12_from0, @m.dates_by_week.map{ |w| w.map(&:day) })
- assert_equal(f2010_12_from1, @m.dates_by_week(1).map{ |w| w.map(&:day) })
+ assert_equal(f2010_12_from0, @m.dates_by_block.map{ |w| w.map(&:day) })
+ assert_equal(f2010_12_from1, @m.dates_by_block(1).map{ |w| w.map(&:day) })
end
- def test_format
- puts
- puts @m.format
- puts
- puts @m.format(:week, 1)
- puts
- puts @m.format(:month)
- end
+ #def test_format
+ #puts
+ #puts @m.format
+ #puts
+ #puts @m.format(:block, 1)
+ #puts
+ #puts @m.format(:line)
+ #end
- def test_color_format
- puts @m.color_format(:week, 0)
- puts @m.color_format(:week, 1)
- puts @m.color_format(:month, 1)
- puts @m.color_format(:week3)
- puts Caline::Month.new(2008, 1).color_format(:week3)
- end
+ #def test_color_format
+ #puts @m.color_format(:block, 0)
+ #puts @m.color_format(:block, 1)
+ #puts @m.color_format(:line, 1)
+ #puts @m.color_format(:block3)
+ #end
- def test_colors
- m = Caline::Month.new(2011, 2, sunday: :yellow, saturday: :green)
- puts
- puts m.color_format(:week, 0)
- m.colors = {sunday: :red, saturday: :blue}
- puts m.color_format(:month, 1)
- end
+ #def test_colors
+ #m = Caline::Month.new(2011, 2, sunday: :yellow, saturday: :green)
+ #puts
+ #puts m.color_format(:block, 0)
+ #m.colors = {sunday: :red, saturday: :blue}
+ #puts m.color_format(:line, 1)
+ #end
+
+ #def test_holidays
+ #months = (1..12).map { |m| Caline::Month.new(2011, m) }
+ #months.each do |mon|
+ #mon.holidays = :ja_ja
+ #puts
+ #puts mon.color_format
+ #end
+ #months.each do |mon|
+ #mon.holidays = :us
+ #puts
+ #puts mon.color_format
+ #end
+ #end
+ #
+ #def test_holiday_label_with_different_country
+ #m = Caline::Month.new(2010, 4)
+ #puts m.color_format(:block, 0)
+ #m.holidays = :ja_ja
+ #puts m.color_format(:block, 0)
+ #m.holidays = :us
+ #puts m.color_format(:block, 0)
+ #m.holidays = :au
+ #puts m.color_format(:block, 0)
+ #puts 'no label'
+ #puts m.color_format(:block, 0, false)
+ #end
- def test_holidays
- months = (1..12).map { |m| Caline::Month.new(2011, m) }
- months.each do |mon|
- mon.holidays = :ja_ja
- puts
- puts mon.color_format
- end
- months.each do |mon|
- mon.holidays = :us
- puts
- puts mon.color_format
- end
+ def test_holiday_label_with_different_style
+ m = Caline::Month.new(2010, 12)
+ m.holidays = :ja_ja
+ puts m.color_format(:block, 0)
+ puts m.color_format(:line, 0)
+ puts m.color_format(:block3, 0)
end
end
View
@@ -8,13 +8,13 @@ def setup
end
def test_format_with_weekline
- puts @y2010.format(:week, 1)
- puts @y2010.color_format(:week)
+ puts @y2010.format(:block, 1)
+ puts @y2010.color_format(:block)
end
def test_fromat_with_monthline
- puts @y2010.format(:month)
- puts @y2010.color_format(:month, 1)
+ puts @y2010.format(:line)
+ puts @y2010.color_format(:line, 1)
end
def test_color_options
@@ -31,4 +31,4 @@ def test_holidays_option
cal = Caline::Year.new(2012, holidays: :us)
puts cal.color_format
end
-end
+end

0 comments on commit e3902c4

Please sign in to comment.