Skip to content

Commit

Permalink
enable setting color with calour command opts
Browse files Browse the repository at this point in the history
  • Loading branch information
melborne committed Dec 19, 2010
1 parent 4c425f6 commit 65c7708
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/calour/runner.rb
Expand Up @@ -6,6 +6,7 @@


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


class << self class << self
def start def start
Expand All @@ -31,6 +32,7 @@ def parse_options
opt.on('-f') { |v| OPTS[:footer] = false } opt.on('-f') { |v| OPTS[:footer] = false }
opt.on('-y') { |v| @y_opt = true } opt.on('-y') { |v| @y_opt = true }
opt.on('-h', '--help') { |v| puts colored_help; exit } opt.on('-h', '--help') { |v| puts colored_help; exit }
opt.on('--color [COLOR_SET]') { |v| parse_color v }


opt.parse! ARGV rescue opt.parse! ['-h'] opt.parse! ARGV rescue opt.parse! ['-h']
end end
Expand Down Expand Up @@ -82,8 +84,10 @@ def help
end end


def colored_help def colored_help
c = %w() color = %w(magenta green blue yellow red cyan)
help.gsub(/^[A-Z]+/) { $&.green }.gsub(/calour/) { $&.magenta.bold }.gsub(/-[\w\d]+/) { $&.cyan } help.gsub(/^[A-Z]+/) { $&.green }
.gsub(/calour/) { $&.split(//).map.with_index { |chr,i| chr.send color[i] }.join.bold }
.gsub(/-[\w\d]+/) { $&.cyan }
end end


def parse_argument(args) def parse_argument(args)
Expand All @@ -101,12 +105,27 @@ def parse_argument(args)
return mon, year return mon, year
end end


def parse_color(args)
h = args.split(/[, ]+/)
.inject({}) { |h, arg| k, v = arg.split(':'); h[t_title k] = t_color(v); h }
@color_opts.update h
end

def t_title(key)
%w(year month today saturday sunday holiday neighbor).detect { |t| t =~ /#{key}/ }.intern
end

def t_color(value)
Term::ANSIColor.attributes.detect { |c| c =~ /#{value}/ }
end

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

0 comments on commit 65c7708

Please sign in to comment.