Skip to content
Browse files

add 3 months format

organize files
  • Loading branch information...
1 parent ca836fd commit 68f73da14181795c5c8a77beafb677f3026228ae @melborne committed
Showing with 70 additions and 41 deletions.
  1. +13 −0 lib/caline.rb
  2. +35 −0 lib/column.rb
  3. +15 −6 lib/month.rb
  4. +3 −33 lib/year.rb
  5. +3 −1 test/tc_month.rb
  6. +1 −1 test/tc_year.rb
View
13 lib/caline.rb
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+#-*-encoding: utf-8-*-
+require "date"
+require "term/ansicolor"
+require_relative "gcalendar"
+require_relative 'month'
+require_relative 'year'
+
+
+module Caline
+ String.send(:include, Term::ANSIColor)
+end
+
View
35 lib/column.rb
@@ -0,0 +1,35 @@
+module Caline
+ module Column
+ def three_column_form
+ 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)
+ end
+ left, center, right = align_size(left, center, right)
+ year_label, *body = left.zip(center, right).map { |line| line.join(" ") }
+ out << body
+ end
+ out.unshift year_label.sub(/(\d{4})(.+\d{4}.+)(\d{4})/, ' \2 ')
+ end
+ end
+
+ # align height size to max one
+ def align_size(*args)
+ max = args.map(&:size).max
+ if args[0].size != max
+ args[0] << " " * MONTH_WIDTH()
+ end
+ if args[1].size != max
+ args[1] << " " * MONTH_WIDTH()
+ end
+ args
+ end
+
+ def MONTH_WIDTH
+ 20
+ end
+ end
+end
View
21 lib/month.rb
@@ -1,13 +1,9 @@
#!/usr/bin/env ruby
#-*-encoding: utf-8-*-
-require "date"
-require "term/ansicolor"
-require_relative "gcalendar"
-
+require_relative "column"
module Caline
- String.send(:include, Term::ANSIColor)
-
class Month
+ include Column
@@holidays = Hash.new{ |h, k| h[k] = {} }
attr_reader :year, :month, :last, :colors
@@ -41,6 +37,9 @@ def format(style=:week, from=0, color=false)
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
@@ -59,6 +58,16 @@ def holidays=(code)
@@holidays[@year][@code] ||= GCalendar.new(@year).holidays(@code)
end
+ def +(month)
+ mon = @first.next_month(month)
+ Month.new(mon.year, mon.mon, @colors)
+ end
+
+ def -(month)
+ mon = @first.prev_month(month)
+ Month.new(mon.year, mon.mon, @colors)
+ end
+
private
def last_date(year, month, day=[28, 29, 30, 31])
Date.new(year, month, day.pop) rescue retry
View
36 lib/year.rb
@@ -1,12 +1,9 @@
#!/usr/bin/env ruby
#-*-encoding: utf-8-*-
-require_relative 'month'
-require_relative 'gcalendar'
-
+require_relative "column"
module Caline
class Year
- @@holidays = Hash.new{ |h, k| h[k] = {} }
- @@month_width = 20
+ include Column
def initialize(year, opts={})
@year = year
@holidays = opts.delete(:holidays)
@@ -18,16 +15,7 @@ def format(style=:week, from=0, color=false)
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
- out.unshift year_label.sub(/(\d{4})(.+\d{4}.+)(\d{4})/, ' \2 ')
+ three_column_form[@months, from, color]
when :month
@months.each_with_index do |mon, i|
mon.colors = {neighbor: :black}
@@ -41,23 +29,5 @@ def format(style=:week, from=0, color=false)
def color_format(style=:week, from=0)
format(style, from, true)
end
-
- private
- def align_size(*args)
- max = args.map(&:size).max
- if args[0].size != max
- args[0] << " " * @@month_width
- end
- if args[1].size != max
- args[1] << " " * @@month_width
- end
- args
- end
-
- def year_label(color)
- label = "#{@year}".center(@@month_width*3+4)
- color ? label.yellow : label
- end
-
end
end
View
4 test/tc_month.rb
@@ -1,6 +1,6 @@
require "test/unit"
-require_relative "../lib/month"
+require_relative "../lib/caline"
class TestMonth < Test::Unit::TestCase
def setup
@@ -54,6 +54,8 @@ 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_colors
View
2 test/tc_year.rb
@@ -1,6 +1,6 @@
require "test/unit"
-require_relative "../lib/year"
+require_relative "../lib/caline"
class TestYear < Test::Unit::TestCase
def setup

0 comments on commit 68f73da

Please sign in to comment.
Something went wrong with that request. Please try again.