Skip to content

Commit

Permalink
move option and command handling to runner module.
Browse files Browse the repository at this point in the history
rewrite help.
  • Loading branch information
melborne committed Dec 18, 2010
1 parent ee8df4a commit 4c425f6
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 79 deletions.
52 changes: 1 addition & 51 deletions bin/calour
@@ -1,56 +1,6 @@
#!/usr/bin/env ruby
#-*-encoding: utf-8-*-
require "optparse"
require "date"
require_relative "../lib/calour"
Version = Calour::VERSION

def parse_argument(args)
mon, year = args.sort
if mon.nil? && year.nil?
mon, year = Date.today.mon, Date.today.year
elsif year.nil?
if mon >= 100
mon, year = year, mon
else
year = Date.today.year
end
end
mon, year = nil, Date.today.year if @y_opt
return mon, year
end

def banner
<<-EOS
calour - displays a calendar with color
calour [-3lmnty] [-c [country_code]] [[month] year]
EOS
end

opt = OptionParser.new(banner)
OPTS = {style: :block, from:0, color:true, holidays: :ja_ja, footer:false}

opt.on('-3', '3 months block mode') { |v| OPTS[:style] = :block3 }
opt.on('-l', 'line mode') { |v| OPTS[:style] = :line }
opt.on('-m', 'monday first') { |v| OPTS[:from] = 1 }
opt.on('-n', 'non-color mode') { |v| OPTS[:color] = false }
opt.on('-c [COUNTRY_CODE]' , 'holidays mark on/off. no code let it off. ex. ja us au..') { |v|
v = v.sub(/^:/, '').intern unless v.nil?
OPTS[:holidays] = v
}
opt.on('-t', 'append holiday titles') { |v| OPTS[:footer] = true }
opt.on('-y', 'this year') { |v| @y_opt = true }

opt.parse! ARGV rescue opt.parse! ['-h']

mon, year = parse_argument ARGV.map(&:to_i)

if year > 100 && !mon
cal = Calour::Year.new(year, holidays: OPTS[:holidays])
puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
else
cal = Calour::Month.new(year, mon)
cal.holidays = OPTS[:holidays] if OPTS[:color]
puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
end
Calour.run

10 changes: 7 additions & 3 deletions lib/calour.rb
Expand Up @@ -2,12 +2,16 @@
#-*-encoding: utf-8-*-
require "date"
require "term/ansicolor"
require_relative 'calour/month'
require_relative 'calour/year'
require_relative "calour/gcalendar"

module Calour
String.send(:include, Term::ANSIColor)
VERSION = '1.0'
def self.run
Runner.start
end
end

require_relative 'calour/month'
require_relative 'calour/year'
require_relative "calour/gcalendar"
require_relative "calour/runner"
48 changes: 23 additions & 25 deletions lib/calour/column.rb
@@ -1,32 +1,30 @@
module Calour
module ColumnForm
def three_columns_formatter(months, from, color)
out, year_label = [], nil
months.each_slice(3) do |gr|
left, center, right = gr.map do |mon|
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(" ") }
out << body
module Calour::ColumnForm
def three_columns_formatter(months, from, color)
out, year_label = [], nil
months.each_slice(3) do |gr|
left, center, right = gr.map do |mon|
mon.format(:block, from, color, false)
end
out.unshift year_label.sub(/(\d{4})(.+\d{4}.+)(\d{4})/, ' \2 ')
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

# 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
# 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

def MONTH_WIDTH
20
if args[1].size != max
args[1] << " " * MONTH_WIDTH()
end
args
end

def MONTH_WIDTH
20
end
end
116 changes: 116 additions & 0 deletions lib/calour/runner.rb
@@ -0,0 +1,116 @@
#!/usr/bin/env ruby
#-*-encoding: utf-8-*-
require "optparse"
require "date"
Version = Calour::VERSION

module Calour::Runner
OPTS = {style: :block, from:0, color:true, holidays: nil, footer:false}

class << self
def start
parse_options
mon, year = parse_argument ARGV.map(&:to_i)
print_calendar(mon, year)
end

private
def parse_options
opt = OptionParser.new(colored_help)

opt.on('-3') { |v| OPTS[:style] = :block3 }
opt.on('-l') { |v| OPTS[:style] = :line }
opt.on('-b') { |v| OPTS[:style] = :block }
opt.on('-m') { |v| OPTS[:from] = 1 }
opt.on('-s') { |v| OPTS[:from] = 0 }
opt.on('-n') { |v| OPTS[:color] = false }
opt.on('-k') { |v| OPTS[:color] = true }
opt.on('-c [COUNTRY_CODE]') { |v| v = v.sub(/^:/, '').intern unless v.nil?
OPTS[:holidays] = v }
opt.on('-t') { |v| OPTS[:footer] = true }
opt.on('-f') { |v| OPTS[:footer] = false }
opt.on('-y') { |v| @y_opt = true }
opt.on('-h', '--help') { |v| puts colored_help; exit }

opt.parse! ARGV rescue opt.parse! ['-h']
end

def help
<<-EOS
NAME
calour - displays a calendar with color
SYNOPSIS
calour [-3lmnty] [-c [country_code]] [[month] year]
DESCRIPTION
The options are as follows:
-3 3 months block mode
-l line mode
-b block mode (default)
-m monday first
-s sunday first (default)
-n no color mode
-k color mode (default)
-c country_code
holidays mark on/off. without country code, let it off.
acceptable code: ja us au ja_ja cn fr de it kr tw gb
-t append holiday titles
-f no holiday titles (default)
-y this year
-h display this help
-v display version
EOS
end

def colored_help
c = %w()
help.gsub(/^[A-Z]+/) { $&.green }.gsub(/calour/) { $&.magenta.bold }.gsub(/-[\w\d]+/) { $&.cyan }
end

def parse_argument(args)
mon, year = args.sort
if mon.nil? && year.nil?
mon, year = Date.today.mon, Date.today.year
elsif year.nil?
if mon >= 100
mon, year = year, mon
else
year = Date.today.year
end
end
mon, year = nil, Date.today.year if @y_opt
return mon, year
end

def print_calendar(mon, year)
if year > 100 && !mon
cal = Calour::Year.new(year, holidays: OPTS[:holidays])
puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
else
cal = Calour::Month.new(year, mon)
cal.holidays = OPTS[:holidays] if OPTS[:color]
puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
end
end
end
end

0 comments on commit 4c425f6

Please sign in to comment.