diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2017-06-25 01:50:26 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-25 01:50:26 -0300 |
commit | c227ef5d49e79d1b9bd7bb76d96cf506a2954e4f (patch) | |
tree | 87e8cabd869b007b8a0250760918d26cb4be9695 | |
parent | 8bc8dbfd6ad73c8a9f2c7b33e42387ac504e0403 (diff) | |
parent | 31ec9d422fd8fd70ff1760d3059d6606e09e4ed9 (diff) | |
download | highline-c227ef5d49e79d1b9bd7bb76d96cf506a2954e4f.tar.gz |
Merge pull request #212 from JEG2/pr_211_option
Make use_color= and use_color? instance methods
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | Changelog.md | 3 | ||||
-rwxr-xr-x | lib/highline.rb | 26 | ||||
-rw-r--r-- | lib/highline/import.rb | 3 | ||||
-rw-r--r-- | lib/highline/version.rb | 2 | ||||
-rw-r--r-- | test/test_helper.rb | 5 | ||||
-rwxr-xr-x | test/test_highline.rb | 110 |
7 files changed, 132 insertions, 19 deletions
diff --git a/.travis.yml b/.travis.yml index fde2b7b..a13fe9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ --- language: ruby sudo: false -script: "bundle exec rake test" +script: "bundle exec rake test && bundle exec codeclimate-test-reporter" rvm: - 1.9 - 2.0 diff --git a/Changelog.md b/Changelog.md index 7534c97..d524b90 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,9 @@ Below is a complete listing of changes for each revision of HighLine. +### 2.0.0-develop.9 / 2017-06-24 + +* PR #211 / PR #212 - HighLine#use_color= and use_color? as instance methods (@abinoam, @phiggins) * PR #203 / I #191 - Default values are shown in menus by Frederico (@fredrb) * PR #201 / I #198 - Confirm in question now accepts Proc (@mmihira) * PR #197 - Some HighLine::Menu improvements diff --git a/lib/highline.rb b/lib/highline.rb index 70734cb..868f508 100755 --- a/lib/highline.rb +++ b/lib/highline.rb @@ -44,17 +44,17 @@ class HighLine include BuiltinStyles include CustomErrors - # The setting used to disable color output. - @use_color = true + # Set it to false to disable ANSI coloring + attr_writer :use_color - # Pass +false+ to _setting_ to turn off HighLine's color escapes. - def self.use_color=( setting ) - @use_color = setting + # Returns true if HighLine instance is currently using color escapes. + def use_color? + @use_color end - # Returns true if HighLine is currently using color escapes. - def self.use_color? - @use_color + # Resets the use of color. + def reset_use_color + @use_color = true end # For checking if the current version of HighLine supports RGB colors @@ -101,10 +101,11 @@ class HighLine end # Reset HighLine to default. - # Clears Style index and reset color scheme. + # Clears Style index and resets color_scheme and use_color settings. def self.reset Style.clear_index reset_color_scheme + reset_use_color end # Reset color scheme to default (+nil+) @@ -138,6 +139,7 @@ class HighLine @header = nil @prompt = nil @key = nil + @use_color = true @terminal = HighLine::Terminal.get_terminal(input, output) end @@ -307,9 +309,11 @@ class HighLine end # (see .color) - # Convenience instance method. It delegates to the class method. + # This method is a clone of the HighLine.color class method. + # But it checks for use_color? per instance def color(string, *colors) - self.class.color(string, *colors) + return string unless use_color? + HighLine.Style(*colors).color(string) end # In case you just want the color code, without the embedding and diff --git a/lib/highline/import.rb b/lib/highline/import.rb index 6f31de8..157752d 100644 --- a/lib/highline/import.rb +++ b/lib/highline/import.rb @@ -24,7 +24,8 @@ $terminal = HighLine.new # module Kernel extend Forwardable - def_delegators :$terminal, :agree, :ask, :choose, :say + def_delegators :$terminal, :agree, :ask, :choose, :say, + :use_color=, :use_color?, :reset_use_color end # When requiring 'highline/import' HighLine adds {#or_ask} to Object so diff --git a/lib/highline/version.rb b/lib/highline/version.rb index fa4ded5..4b8609e 100644 --- a/lib/highline/version.rb +++ b/lib/highline/version.rb @@ -2,5 +2,5 @@ class HighLine # The version of the installed library. - VERSION = "2.0.0-develop.8".freeze + VERSION = "2.0.0-develop.9".freeze end diff --git a/test/test_helper.rb b/test/test_helper.rb index 449c7d2..161912b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,11 +3,6 @@ require 'simplecov' -if ENV['CODECLIMATE_REPO_TOKEN'] - require "codeclimate-test-reporter" - CodeClimate::TestReporter.start -end - # Compatibility module for StringIO, File # and Tempfile. Necessary for some tests. require "io_console_compatible" diff --git a/test/test_highline.rb b/test/test_highline.rb index 9cdfeb4..ce1233d 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -502,9 +502,119 @@ class TestHighLine < Minitest::Test # turn off color old_setting = HighLine.use_color? HighLine.use_color = false + @terminal.use_color = false @terminal.say("This should be <%= color('cyan', CYAN) %>!") assert_equal("This should be cyan!\n", @output.string) HighLine.use_color = old_setting + @terminal.use_color = old_setting + end + + def test_color_setting_per_instance + require 'highline/import' + old_glob_term = $terminal + old_setting = HighLine.use_color? + + gterm_input = StringIO.new + gterm_output = StringIO.new + $terminal = HighLine.new(gterm_input, gterm_output) + + # It can set coloring at HighLine class + cli_input = StringIO.new + cli_output = StringIO.new + + cli = HighLine.new(cli_input, cli_output) + + # Testing with both use_color setted to true + HighLine.use_color = true + @terminal.use_color = true + cli.use_color = true + + say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be \e[36mcyan\e[0m!\n", gterm_output.string) + + @terminal.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string) + + cli.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be \e[36mcyan\e[0m!\n", cli_output.string) + + gterm_output.truncate(gterm_output.rewind) + @output.truncate(@output.rewind) + cli_output.truncate(cli_output.rewind) + + # Testing with both use_color setted to false + HighLine.use_color = false + @terminal.use_color = false + cli.use_color = false + + say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be cyan!\n", gterm_output.string) + + @terminal.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be cyan!\n", @output.string) + + cli.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be cyan!\n", cli_output.string) + + gterm_output.truncate(gterm_output.rewind) + @output.truncate(@output.rewind) + cli_output.truncate(cli_output.rewind) + + # Now check when class and instance doesn't agree about use_color + + # Class false, instance true + HighLine.use_color = false + @terminal.use_color = false + cli.use_color = true + + say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be cyan!\n", gterm_output.string) + + @terminal.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be cyan!\n", @output.string) + + cli.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be \e[36mcyan\e[0m!\n", cli_output.string) + + gterm_output.truncate(gterm_output.rewind) + @output.truncate(@output.rewind) + cli_output.truncate(cli_output.rewind) + + # Class true, instance false + HighLine.use_color = true + @terminal.use_color = true + cli.use_color = false + + say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be \e[36mcyan\e[0m!\n", gterm_output.string) + + @terminal.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string) + + cli.say("This should be <%= color('cyan', CYAN) %>!") + assert_equal("This should be cyan!\n", cli_output.string ) + + gterm_output.truncate(gterm_output.rewind) + @output.truncate(@output.rewind) + cli_output.truncate(cli_output.rewind) + + HighLine.use_color = old_setting + @terminal.use_color = old_setting + $terminal = old_glob_term + end + + def test_reset_use_color + HighLine.use_color = false + refute HighLine.use_color? + HighLine.reset_use_color + assert HighLine.use_color? + end + + def test_reset_use_color_when_highline_reset + HighLine.use_color = false + refute HighLine.use_color? + HighLine.reset + assert HighLine.use_color? end def test_uncolor |