Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change format with labels

provide monthly line format
fix some bags
  • Loading branch information...
commit bd93b93a31bb4855725edd287a6e79a23dd75dd7 1 parent a72f058
@melborne authored
Showing with 72 additions and 22 deletions.
  1. +28 −10 lib/month.rb
  2. +19 −8 lib/year.rb
  3. +2 −2 test/tc_month.rb
  4. +23 −2 test/tc_year.rb
View
38 lib/month.rb
@@ -22,14 +22,14 @@ def initialize(year, month, colors={})
@code = nil
end
- def dates
- (@first..@last).to_a
+ def dates(from=0, pre=true, pos=true)
+ pre = pre ? (1..(@first.wday-from)%7).map { |i| @first - i }.reverse : []
+ pos = pos ? (1..(6-@last.wday+from)%7).map { |i| @last + i } : []
+ pre + (@first..@last).to_a + pos
end
def dates_by_week(from=0)
- pre = (1..(@first.wday-from)%7).map { |i| @first - i }.reverse
- pos = (1..(6-@last.wday+from)%7).map { |i| @last + i }
- (pre + dates + pos).each_slice(7).to_a
+ dates(from).each_slice(7)
end
alias formaty format
@@ -37,10 +37,11 @@ def format(style=:week, from=0, color=false)
form = color ? color_proc : mono_proc
case style
when :week
- header(from, color) +
+ header(from, color, :block) +
dates_by_week(from).map { |w| form[w] }
when :month
- form[dates]
+ header(from, color, :line) +
+ Array( form[dates from, true, false] )
else raise ArgumentError
end
end
@@ -65,7 +66,16 @@ def last_date(year, month, day=[28, 29, 30, 31])
end
def mono_proc
- ->w{ w.map{ |d| formaty "%2d", d.day }.join(" ") }
+ ->w{
+ w.map do |d|
+ str = "%2d"
+ str = case d
+ when neighbor? then str.replace " "
+ else str
+ end
+ formaty str, d.day
+ end.join(" ")
+ }
end
def color_proc
@@ -111,14 +121,22 @@ def today_format(str)
attrs.inject(str) { |s, color| s.send(color) }
end
- def header(from, color)
- weeks = " " + %w(S M T W T F S).rotate(from).join(" ")
+ def header(from, color, style)
+ weeks = week_label(from, style)
year, month = %w(%Y %B).map { |f| @first.strftime(f).center(weeks.size) }
if color
year, month = year.send(@colors[:year]), month.send(@colors[:month])
end
[year, month, weeks]
end
+
+ def week_label(from, style)
+ weeks = " " + %w(S M T W T F S).rotate(from).join(" ")
+ case style
+ when :block then weeks
+ when :line then (weeks + " ") * 5
+ end
+ end
end
end
View
27 lib/year.rb
@@ -15,16 +15,27 @@ def initialize(year, opts={})
alias formaty format
def format(style=:week, from=0, color=false)
- out = []
- @months.each_slice(3) do |gr|
- left, center, right = gr.map do |mon|
- mon.holidays = @holidays if @holidays
- mon.format(:week, 0, color)[1..-1]
+ out, year_label = [], nil
+ case style
+ when :week
+ @months.each_slice(3) do |gr|
+ left, center, right = gr.map do |mon|
+ mon.holidays = @holidays if @holidays
+ mon.format(:week, from, color)
+ end
+ left, center, right = align_size(left, center, right)
+ year_label, *body = left.zip(center, right).map { |line| line.join(" ") }
+ out << body
end
- left, center, right = align_size(left, center, right)
- out << left.zip(center, right).map { |line| line.join(" ") }
+ out.unshift year_label.sub(/(\d{4})(.+\d{4}.+)(\d{4})/, ' \2 ')
+ when :month
+ @months.each_with_index do |mon, i|
+ mon.colors = {neighbor: :black}
+ year_label, *body = mon.format(:month, from, color)
+ out << body
+ end
+ out.unshift year_label
end
- out.unshift year_label(color)
end
def color_format(style=:week, from=0)
View
4 test/tc_month.rb
@@ -18,8 +18,8 @@ def test_year_month
end
def test_dates
- assert_equal((1..28).to_a, Caline::Month.new(2011, 2).dates.map(&:day))
- assert_equal((1..31).to_a, @m.dates.map(&:day))
+ assert_equal((1..28).to_a, Caline::Month.new(2011, 2).dates(0, false, false).map(&:day))
+ assert_equal((1..31).to_a, @m.dates(0, false, false).map(&:day))
end
def test_dates_by_week
View
25 test/tc_year.rb
@@ -7,7 +7,28 @@ def setup
@y2010 = Caline::Year.new(2010)
end
- def test_format
- puts @y2010.format
+ def test_format_with_weekline
+ puts @y2010.format(:week, 1)
+ puts @y2010.color_format(:week)
+ end
+
+ def test_fromat_with_monthline
+ puts @y2010.format(:month)
+ puts @y2010.color_format(:month, 1)
+ end
+
+ def test_color_options
+ cal = Caline::Year.new(2010, today:[:red, :blink, :on_blue], year: :magenta)
+ puts cal.color_format
+ end
+
+ def test_neighbor_option
+ cal = Caline::Year.new(2009, neighbor: :black)
+ puts cal.color_format
+ end
+
+ def test_holidays_option
+ cal = Caline::Year.new(2012, holidays: :us)
+ puts cal.color_format
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.