summaryrefslogtreecommitdiff
path: root/lib/highline/color_scheme.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/highline/color_scheme.rb')
-rw-r--r--lib/highline/color_scheme.rb60
1 files changed, 32 insertions, 28 deletions
diff --git a/lib/highline/color_scheme.rb b/lib/highline/color_scheme.rb
index 11cd687..e1bec88 100644
--- a/lib/highline/color_scheme.rb
+++ b/lib/highline/color_scheme.rb
@@ -8,7 +8,6 @@
#
# This is Free Software. See LICENSE and COPYING for details
-
class HighLine
#
# ColorScheme objects encapsulate a named set of colors to be used in the
@@ -19,7 +18,8 @@ class HighLine
#
# A ColorScheme contains named sets of HighLine color constants.
#
- # @example Instantiating a color scheme, applying it to HighLine, and using it:
+ # @example Instantiating a color scheme, applying it to HighLine,
+ # and using it:
# ft = HighLine::ColorScheme.new do |cs|
# cs[:headline] = [ :bold, :yellow, :on_black ]
# cs[:horizontal_line] = [ :bold, :white ]
@@ -49,15 +49,15 @@ class HighLine
# constants.
#
# @param h [Hash]
- def initialize( h = nil )
- @scheme = Hash.new
+ def initialize(h = nil)
+ @scheme = {}
load_from_hash(h) if h
yield self if block_given?
end
# Load multiple colors from key/value pairs.
# @param h [Hash]
- def load_from_hash( h )
+ def load_from_hash(h)
h.each_pair do |color_tag, constants|
self[color_tag] = constants
end
@@ -66,20 +66,20 @@ class HighLine
# Does this color scheme include the given tag name?
# @param color_tag [#to_sym]
# @return [Boolean]
- def include?( color_tag )
+ def include?(color_tag)
@scheme.keys.include?(to_symbol(color_tag))
end
# Allow the scheme to be accessed like a Hash.
# @param color_tag [#to_sym]
# @return [Style]
- def []( color_tag )
+ def [](color_tag)
@scheme[to_symbol(color_tag)]
end
# Retrieve the original form of the scheme
# @param color_tag [#to_sym]
- def definition( color_tag )
+ def definition(color_tag)
style = @scheme[to_symbol(color_tag)]
style && style.list
end
@@ -93,29 +93,33 @@ class HighLine
# Allow the scheme to be set like a Hash.
# @param color_tag [#to_sym]
# @param constants [Array<Symbol>] Array of Style symbols
- def []=( color_tag, constants )
- @scheme[to_symbol(color_tag)] = HighLine::Style.new(:name=>color_tag.to_s.downcase.to_sym,
- :list=>constants, :no_index=>true)
+ def []=(color_tag, constants)
+ @scheme[to_symbol(color_tag)] =
+ HighLine::Style.new(name: color_tag.to_s.downcase.to_sym,
+ list: constants,
+ no_index: true)
end
# Retrieve the color scheme hash (in original definition format)
# @return [Hash] scheme as Hash. It may be reused in a new ColorScheme.
def to_hash
- @scheme.inject({}) { |hsh, pair| key, value = pair; hsh[key] = value.list; hsh }
+ @scheme.each_with_object({}) do |pair, hsh|
+ key, value = pair
+ hsh[key] = value.list
+ end
end
-
private
# Return a normalized representation of a color name.
- def to_symbol( t )
+ def to_symbol(t)
t.to_s.downcase
end
# Return a normalized representation of a color setting.
- def to_constant( v )
+ def to_constant(v)
v = v.to_s if v.is_a?(Symbol)
- if v.is_a?(::String) then
+ if v.is_a?(::String)
HighLine.const_get(v.upcase)
else
v
@@ -125,23 +129,23 @@ class HighLine
# A sample ColorScheme.
class SampleColorScheme < ColorScheme
+ SAMPLE_SCHEME = {
+ critical: [:yellow, :on_red],
+ error: [:bold, :red],
+ warning: [:bold, :yellow],
+ notice: [:bold, :magenta],
+ info: [:bold, :cyan],
+ debug: [:bold, :green],
+ row_even: [:cyan],
+ row_odd: [:magenta]
+ }.freeze
#
# Builds the sample scheme with settings for <tt>:critical</tt>,
# <tt>:error</tt>, <tt>:warning</tt>, <tt>:notice</tt>, <tt>:info</tt>,
# <tt>:debug</tt>, <tt>:row_even</tt>, and <tt>:row_odd</tt> colors.
#
- def initialize( h = nil )
- scheme = {
- :critical => [ :yellow, :on_red ],
- :error => [ :bold, :red ],
- :warning => [ :bold, :yellow ],
- :notice => [ :bold, :magenta ],
- :info => [ :bold, :cyan ],
- :debug => [ :bold, :green ],
- :row_even => [ :cyan ],
- :row_odd => [ :magenta ]
- }
- super(scheme)
+ def initialize(_h = nil)
+ super(SAMPLE_SCHEME)
end
end
end