Permalink
Browse files

add color setting description and color on help

  • Loading branch information...
1 parent 65c7708 commit 92f04e61b5951e7700dc0a3b9a6c6fbb6a70aa0e @melborne committed Dec 19, 2010
Showing with 85 additions and 44 deletions.
  1. +85 −44 lib/calour/runner.rb
View
@@ -37,6 +37,57 @@ def parse_options
opt.parse! ARGV rescue opt.parse! ['-h']
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 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)
+ if year > 100 && !mon
+ opts = @color_opts.merge(holidays: OPTS[:holidays])
+ cal = Calour::Year.new(year, opts)
+ puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
+ else
+ cal = Calour::Month.new(year, mon, @color_opts)
+ cal.holidays = OPTS[:holidays] if OPTS[:color]
+ puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
+ end
+ end
+
+ def colored_help
+ attrs = Term::ANSIColor.attributes
+ colors = (attrs*' ')[/bold.*on_white/].split(' ')
+ help.gsub(/^[A-Z].*$/) { $&.green }
+ .gsub(/calour/) { $&.split(//).map.with_index { |chr,i| chr.send colors[i+11] }.join.bold }
+ .gsub(/\B-+[\w\d]+/) { $&.cyan }
+ .gsub(/(?<=\s\s)\w+(?=:\s)/) { $&.cyan }
+ .gsub(/(?<=\s)(#{colors.join('|')})(?=[,)\]\s])/) { $&.send $& }
+ end
+
def help
<<-EOS
@@ -46,7 +97,7 @@ def help
SYNOPSIS
- calour [-3lmnty] [-c [country_code]] [[month] year]
+ calour [-3lmnty] [-c [country_code]] [--color color_set] [[month] year]
DESCRIPTION
@@ -80,55 +131,45 @@ def help
-v display version
- EOS
- end
+COLOR SETTINGS
- def colored_help
- color = %w(magenta green blue yellow red 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
+ Colors can be changed with --color COLOR_SET option. COLOR_SET is constructed
+ as combination of target-color-pairs.
- 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
+ examples;
- 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
+ --color year:magenta,today:on_green,holiday:red
- def t_title(key)
- %w(year month today saturday sunday holiday neighbor).detect { |t| t =~ /#{key}/ }.intern
- end
+ --color mon:yel,hol:blu,nei:bla
- def t_color(value)
- Term::ANSIColor.attributes.detect { |c| c =~ /#{value}/ }
- end
+ --color 'sat:blue sun:red tod:blink'
- def print_calendar(mon, year)
- if year > 100 && !mon
- opts = @color_opts.merge(holidays: OPTS[:holidays])
- cal = Calour::Year.new(year, opts)
- puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
- else
- cal = Calour::Month.new(year, mon, @color_opts)
- cal.holidays = OPTS[:holidays] if OPTS[:color]
- puts cal.format(OPTS[:style], OPTS[:from], OPTS[:color], OPTS[:footer])
- end
+ Like examples, each pairs are joined with ','(comma), target and color are
+ joined with ':'(colon). names of targets and colors can be reduced.
+
+ Target names are as follows:
+
+ year: year label color (default: yellow)
+
+ month: month label color (default: green)
+
+ today: today date color (default: green, underline])
+
+ saturday: saturday dates color (default: cyan)
+
+ sunday: sunday dates color (default: magenta)
+
+ holiday: holiday dates color (default: red)
+
+ neightbor: neighbor months dates color (default: nil)
+
+ Color names are as follows:
+
+ black, red, green, yellow, blue, magenta, cyan, white,
+ on_black, on_red, on_green, on_yellow, on_blue, on_magenta,
+ on_cyan, on_white, bold, underline, blink
+
+ EOS
end
end
end

0 comments on commit 92f04e6

Please sign in to comment.