Skip to content

Commit

Permalink
Merge pull request JEG2#12 from rleber/master
Browse files Browse the repository at this point in the history
Improvements to color_scheme
  • Loading branch information
JEG2 committed Jul 1, 2011
2 parents efd6533 + ff6d244 commit c9f66eb
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Below is a complete listing of changes for each revision of HighLine.

== 1.6.4

* Provisional update by Richard LeBer
* Add introspection methods to color_scheme: definition, keys, to_hash
* Add tests for new methods

== 1.6.3

* Provisional update by Richard LeBer
Expand Down
2 changes: 1 addition & 1 deletion lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#
class HighLine
# The version of the installed library.
VERSION = "1.6.3".freeze
VERSION = "1.6.4".freeze

# An internal HighLine error. User code does not need to trap this.
class QuestionError < StandardError
Expand Down
21 changes: 19 additions & 2 deletions lib/highline/color_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,29 @@ def include?( color_tag )
def []( color_tag )
@scheme[to_symbol(color_tag)]
end

# Retrieve the original form of the scheme
def definition( color_tag )
style = @scheme[to_symbol(color_tag)]
style && style.list
end

# Retrieve the keys in the scheme
def keys
@scheme.keys
end

# Allow the scheme to be set like a Hash.
def []=( color_tag, constants )
# @scheme[to_symbol(color_tag)] = constants.map { |c| to_constant(c) }
@scheme[to_symbol(color_tag)] = HighLine::Style.new(:name=>color_tag.to_sym, :list=>constants, :no_index=>true)
@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)
def to_hash
@scheme.inject({}) { |hsh, pair| key, value = pair; hsh[key] = value.list; hsh }
end


private

Expand Down
6 changes: 6 additions & 0 deletions lib/highline/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def self.rgb_number(*parts)
16 + parts.inject(0) {|kode, part| kode*6 + (part/256.0*6.0).floor}
end

def self.ansi_rgb_to_hex(ansi_number)
raise "Invalid ANSI rgb code #{ansi_number}" unless (16..231).include?(ansi_number)
parts = (ansi_number-16).to_s(6).rjust(3,'0').scan(/./).map{|d| (d.to_i*255.0/6.0).ceil}
rgb_hex(*parts)
end

def self.list
@@styles ||= {}
end
Expand Down
42 changes: 42 additions & 0 deletions test/tc_color_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ def test_scheme
@terminal.say("This should be <%= color('warning yellow', 'WarNing') %>.")
assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
@output.rewind

# Check that keys are available, and as expected
assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort,
HighLine.color_scheme.keys.sort

# Color scheme doesn't care if we use symbols or strings, and is case-insensitive
warning1 = HighLine.color_scheme[:warning]
warning2 = HighLine.color_scheme["warning"]
warning3 = HighLine.color_scheme[:wArning]
warning4 = HighLine.color_scheme["warniNg"]
assert_instance_of HighLine::Style, warning1
assert_instance_of HighLine::Style, warning2
assert_instance_of HighLine::Style, warning3
assert_instance_of HighLine::Style, warning4
assert_equal warning1, warning2
assert_equal warning1, warning3
assert_equal warning1, warning4

# Nonexistent keys return nil
assert_nil HighLine.color_scheme[:nonexistent]

# Same as above, for definitions
defn1 = HighLine.color_scheme.definition(:warning)
defn2 = HighLine.color_scheme.definition("warning")
defn3 = HighLine.color_scheme.definition(:wArning)
defn4 = HighLine.color_scheme.definition("warniNg")
assert_instance_of Array, defn1
assert_instance_of Array, defn2
assert_instance_of Array, defn3
assert_instance_of Array, defn4
assert_equal [:bold, :yellow], defn1
assert_equal [:bold, :yellow], defn2
assert_equal [:bold, :yellow], defn3
assert_equal [:bold, :yellow], defn4
assert_nil HighLine.color_scheme.definition(:nonexistent)

color_scheme_hash = HighLine.color_scheme.to_hash
assert_instance_of Hash, color_scheme_hash
assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort,
color_scheme_hash.keys.sort
assert_instance_of Array, HighLine.color_scheme.definition(:warning)
assert_equal [:bold, :yellow], HighLine.color_scheme.definition(:warning)

# turn it back off, should raise an exception
HighLine.color_scheme = @old_color_scheme
Expand Down
41 changes: 41 additions & 0 deletions test/tc_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,47 @@ def test_rgb_number
assert_equal 16 + 5*36 + 5*6 + 5, HighLine::Style.rgb_number(255,255,255)
end

def test_ansi_rgb_to_hex
assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)

assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)

assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 1)

assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)

assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)

assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 1)

assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 1)

assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 1)

assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 0)
assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 0)
assert_equal "2b2b2b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 1)

# 0xd5 is the smallest number where n/255.0*6.0 > 5
assert_equal "d5d5d5", HighLine::Style.ansi_rgb_to_hex(16 + 5*36 + 5*6 + 5)
end

def test_list
list_size = HighLine::Style.list.size
# Add a Style with a new name and code
Expand Down

0 comments on commit c9f66eb

Please sign in to comment.