Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add to color scheme #274

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# https://github.com/bbatsov/rubocop/tree/master/config

AllCops:
TargetRubyVersion: 2.1
TargetRubyVersion: 3.3
NewCops: enable

# General

Expand Down Expand Up @@ -58,7 +59,7 @@ Style/OptionalArguments:
- 'lib/highline/list_renderer.rb'

# TemplateRenderer should never fail on method missing.
Style/MethodMissing:
Style/MissingRespondToMissing:
Exclude:
- 'lib/highline/template_renderer.rb'

Expand All @@ -73,7 +74,7 @@ Style/SymbolArray:
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
Layout/IndentHeredoc:
Layout/HeredocIndentation:
Exclude:
- 'examples/page_and_wrap.rb'
- 'highline.gemspec'
Expand Down
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

### 3.1.1 / 2024-08-18
* PR #274 / I #273 (@costa)
* Add Highline#add_to_color_scheme
* Dockerize the test environment. Just run `bin/test` and voilá!

### 3.1.0 / 2024-07-15
* PR #272 / I #271 - Readline is now completed deprecated over Reline (@abinoam, issue by @64kramsystem)
* PR #269 - Provide a 'Changelog' link on rubygems.org/gems/highline (@mark-young-atg)
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ group :code_quality do
gem "flog", require: false
gem "pronto", require: false, platform: :ruby
gem "pronto-flay", require: false, platform: :ruby
gem "path_expander", "1.1.1", require: false # Remove this lock when path_expander > 1.1.2 and flay > 2.13.3 is released.
# gem "pronto-poper", require: false, platform: :ruby
gem "pronto-reek", require: false, platform: :ruby
gem "pronto-rubocop", require: false, platform: :ruby
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ Contributing
- ```rake acceptance```
- ```pronto run```

Alternatively, if you're in a [Docker](https://www.docker.com)ised environment,
don't care about installing anything locally -- just run `bin/test` instead.

8. Commit your changes
- ```git commit -am "Your commit message"```

Expand Down
11 changes: 11 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -e
test "$#" -ne 0 && echo "Unsupported args: $@" >&2 && exit 145
cd "$( dirname "${BASH_SOURCE[0]}" )"/..

export COMPOSE_FILE=test/docker-compose.yml
export COMPOSE_PROJECT_NAME=highline_dev

docker compose rm -svf
docker compose build --force-rm

docker compose run --rm tester && docker compose rm -svf || ( docker compose logs && exit 1 )
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
2 changes: 1 addition & 1 deletion lib/highline/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class HighLine
# The version of the installed library.
VERSION = "3.1.0".freeze
VERSION = "3.1.1".freeze
end
15 changes: 15 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM ruby

WORKDIR /highline


RUN bash -ec 'apt update; apt -y install cmake'

ADD Gemfile highline.gemspec .git* ./
ADD lib/highline/version.rb ./lib/highline/version.rb
RUN bundle install

ADD . .

ENTRYPOINT ["bundle", "exec"]
CMD ["bash", "-ec", "rake test; rake acceptance; pronto run"]
5 changes: 5 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
tester:
build:
context: ..
dockerfile: test/Dockerfile
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