Skip to content

Commit

Permalink
added HighLine.add_to_color_scheme: trivial functionality & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
costa committed Aug 3, 2024
1 parent da68dcf commit 041c45f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def using_color_scheme?
true if @color_scheme
end

# Pass a +Hash+ to add +new+ colors to the current scheme.
def add_to_color_scheme(hash)
old_hash = (color_scheme || {}).to_hash
fail "Overlapping color schemes: #{old_hash.keys & hash.keys}" unless
(old_hash.keys & hash.keys).empty?
self.color_scheme = ColorScheme.new(old_hash.merge hash)
end

# Reset color scheme to default (+nil+)
def reset_color_scheme
self.color_scheme = nil
Expand Down
16 changes: 12 additions & 4 deletions test/test_color_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def test_scheme
info debug row_even row_odd].sort,
HighLine.color_scheme.keys.sort

HighLine.add_to_color_scheme calming: [:blue]

# Color scheme doesn't care if we use symbols or strings.
# And it isn't case-sensitive
warning1 = HighLine.color_scheme[:warning]
Expand All @@ -64,6 +66,7 @@ def test_scheme
assert_equal warning1, warning2
assert_equal warning1, warning3
assert_equal warning1, warning4
assert_instance_of HighLine::Style, HighLine.color_scheme[:calming]

# Nonexistent keys return nil
assert_nil HighLine.color_scheme[:nonexistent]
Expand All @@ -81,18 +84,23 @@ def test_scheme
assert_equal [:bold, :yellow], defn2
assert_equal [:bold, :yellow], defn3
assert_equal [:bold, :yellow], defn4
assert_equal [:blue], HighLine.color_scheme.definition(:calming)
assert_nil HighLine.color_scheme.definition(:nonexistent)

color_scheme_hash = HighLine.color_scheme.to_hash
assert_instance_of Hash, color_scheme_hash
assert_equal %w[critical error warning notice
assert_equal %w[calming 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)

# adding a color already present should raise an exception
assert_raises(StandardError) do
HighLine.add_to_color_scheme :critical, [:black]
end

# turn it back off, should raise an exception
HighLine.color_scheme = nil
HighLine.reset_color_scheme
assert_nil HighLine.color_scheme
assert_raises(NameError) do
@terminal.say("This should be <%= color('nothing at all', :error) %>.")
end
Expand Down

0 comments on commit 041c45f

Please sign in to comment.