summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam P. Marques Jr <abinoam@gmail.com>2017-06-30 11:00:36 -0300
committerGitHub <noreply@github.com>2017-06-30 11:00:36 -0300
commit66b8ece6560879a4e7d34dd6bd0cf20d48f3b3a7 (patch)
tree83fbcac37cabd1b4ad820bb749b1615a4b08bbec
parent536d7e29c490df37d6ffeac43de64ba54563e53f (diff)
parent7b8c6ea0e827e5985ce8cbaf340b22e2e98e968c (diff)
downloadhighline-66b8ece6560879a4e7d34dd6bd0cf20d48f3b3a7.tar.gz
Merge pull request #214 from JEG2/add_default_instance
Remove the use of $terminal global variable
-rw-r--r--Changelog.md5
-rw-r--r--examples/asking_for_arrays.rb2
-rw-r--r--examples/basic_usage.rb2
-rw-r--r--examples/get_character.rb2
-rw-r--r--examples/limit.rb2
-rw-r--r--examples/menus.rb2
-rw-r--r--examples/overwrite.rb2
-rw-r--r--examples/page_and_wrap.rb4
-rw-r--r--examples/password.rb2
-rw-r--r--examples/repeat_entry.rb2
-rwxr-xr-xlib/highline.rb152
-rw-r--r--lib/highline/import.rb15
-rw-r--r--lib/highline/simulate.rb8
-rw-r--r--lib/highline/version.rb2
-rw-r--r--test/acceptance/acceptance.rb2
-rwxr-xr-xtest/test_highline.rb16
-rw-r--r--test/test_import.rb20
-rw-r--r--test/test_simulator.rb2
18 files changed, 119 insertions, 123 deletions
diff --git a/Changelog.md b/Changelog.md
index d524b90..4c81472 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,6 +2,11 @@
Below is a complete listing of changes for each revision of HighLine.
+### 2.0.0-develop.10 / 2017-06-29
+* PR #214 - Remove `$terminal` (global variable)
+ * Use HighLine.default_instance instead
+ * Reorganize/Group code at lib/highline.rb
+
### 2.0.0-develop.9 / 2017-06-24
* PR #211 / PR #212 - HighLine#use_color= and use_color? as instance methods (@abinoam, @phiggins)
diff --git a/examples/asking_for_arrays.rb b/examples/asking_for_arrays.rb
index fa05a3e..42c3268 100644
--- a/examples/asking_for_arrays.rb
+++ b/examples/asking_for_arrays.rb
@@ -9,7 +9,7 @@ require "rubygems"
require "highline/import"
require "pp"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.class}"
puts
grades = ask( "Enter test scores (or a blank line to quit):",
diff --git a/examples/basic_usage.rb b/examples/basic_usage.rb
index 126c64a..62e3ccb 100644
--- a/examples/basic_usage.rb
+++ b/examples/basic_usage.rb
@@ -9,7 +9,7 @@ require "rubygems"
require "highline/import"
require "yaml"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
contacts = [ ]
diff --git a/examples/get_character.rb b/examples/get_character.rb
index 48e4dba..c0a666f 100644
--- a/examples/get_character.rb
+++ b/examples/get_character.rb
@@ -3,7 +3,7 @@
require "rubygems"
require "highline/import"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
choices = "ynaq"
diff --git a/examples/limit.rb b/examples/limit.rb
index 6d25cef..6115580 100644
--- a/examples/limit.rb
+++ b/examples/limit.rb
@@ -8,7 +8,7 @@
require "rubygems"
require "highline/import"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
text = ask("Enter text (max 10 chars): ") { |q| q.limit = 10 }
diff --git a/examples/menus.rb b/examples/menus.rb
index c925c07..258c1ca 100644
--- a/examples/menus.rb
+++ b/examples/menus.rb
@@ -3,7 +3,7 @@
require "rubygems"
require "highline/import"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
# The old way, using ask() and say()...
diff --git a/examples/overwrite.rb b/examples/overwrite.rb
index 83d51a5..f3c9754 100644
--- a/examples/overwrite.rb
+++ b/examples/overwrite.rb
@@ -8,7 +8,7 @@
require 'rubygems'
require 'highline/import'
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
prompt = "here is your password:"
diff --git a/examples/page_and_wrap.rb b/examples/page_and_wrap.rb
index 25cd6ef..24dd52d 100644
--- a/examples/page_and_wrap.rb
+++ b/examples/page_and_wrap.rb
@@ -8,8 +8,8 @@
require "rubygems"
require "highline/import"
-$terminal.wrap_at = 80
-$terminal.page_at = 22
+HighLine.default_instance.wrap_at = 80
+HighLine.default_instance.page_at = 22
say(<<END)
THE UNITED STATES CONSTITUTION
diff --git a/examples/password.rb b/examples/password.rb
index a9e591a..698829d 100644
--- a/examples/password.rb
+++ b/examples/password.rb
@@ -3,7 +3,7 @@
require "rubygems"
require "highline/import"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
pass = ask("Enter your password: ") { |q| q.echo = false }
diff --git a/examples/repeat_entry.rb b/examples/repeat_entry.rb
index 553b7a6..5e67f7d 100644
--- a/examples/repeat_entry.rb
+++ b/examples/repeat_entry.rb
@@ -3,7 +3,7 @@
require "rubygems"
require "highline/import"
-puts "Using: #{$terminal.terminal.class}"
+puts "Using: #{HighLine.default_instance.terminal.class}"
puts
tounge_twister = ask("... try saying that three times fast") do |q|
diff --git a/lib/highline.rb b/lib/highline.rb
index 868f508..a94bbed 100755
--- a/lib/highline.rb
+++ b/lib/highline.rb
@@ -44,75 +44,48 @@ class HighLine
include BuiltinStyles
include CustomErrors
- # Set it to false to disable ANSI coloring
- attr_writer :use_color
+ extend SingleForwardable
+ def_single_delegators :@default_instance, :agree, :ask, :choose, :say,
+ :use_color=, :use_color?, :reset_use_color,
+ :track_eof=, :track_eof?,
+ :color, :uncolor, :color_code
- # Returns true if HighLine instance is currently using color escapes.
- def use_color?
- @use_color
- end
+ class << self
+ attr_accessor :default_instance
- # Resets the use of color.
- def reset_use_color
- @use_color = true
- end
+ # Pass ColorScheme to set a HighLine color scheme.
+ attr_accessor :color_scheme
- # For checking if the current version of HighLine supports RGB colors
- # Usage: HighLine.supports_rgb_color? rescue false # rescue for compatibility with older versions
- # Note: color usage also depends on HighLine.use_color being set
- def self.supports_rgb_color?
- true
- end
-
- # The setting used to disable EOF tracking.
- @track_eof = true
+ # Returns +true+ if HighLine is currently using a color scheme.
+ def using_color_scheme?
+ !!@color_scheme
+ end
- # Pass +false+ to _setting_ to turn off HighLine's EOF tracking.
- def self.track_eof=( setting )
- @track_eof = setting
- end
+ # Reset color scheme to default (+nil+)
+ def reset_color_scheme
+ self.color_scheme = nil
+ end
- # Returns true if HighLine is currently tracking EOF for input.
- def self.track_eof?
- @track_eof
- end
+ # Reset HighLine to default.
+ # Clears Style index and resets color_scheme and use_color settings.
+ def reset
+ Style.clear_index
+ reset_color_scheme
+ reset_use_color
+ end
- # (see HighLine.track_eof?)
- def track_eof?
- self.class.track_eof?
+ # For checking if the current version of HighLine supports RGB colors
+ # Usage: HighLine.supports_rgb_color? rescue false # rescue for compatibility with older versions
+ # Note: color usage also depends on HighLine.use_color being set
+ # TODO: Discuss removing this method
+ def supports_rgb_color?
+ true
+ end
end
# The setting used to control color schemes.
@color_scheme = nil
- # Pass ColorScheme to _setting_ to set a HighLine color scheme.
- def self.color_scheme=( setting )
- @color_scheme = setting
- end
-
- # Returns the current color scheme.
- def self.color_scheme
- @color_scheme
- end
-
- # Returns +true+ if HighLine is currently using a color scheme.
- def self.using_color_scheme?
- !!@color_scheme
- end
-
- # Reset HighLine to default.
- # 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+)
- def self.reset_color_scheme
- self.color_scheme = nil
- end
-
#
# Create an instance of HighLine connected to the given _input_
# and _output_ streams.
@@ -140,10 +113,32 @@ class HighLine
@prompt = nil
@key = nil
@use_color = true
+ @track_eof = true # The setting used to disable EOF tracking.
@terminal = HighLine::Terminal.get_terminal(input, output)
end
+ # Set it to false to disable ANSI coloring
+ attr_accessor :use_color
+
+ # Returns truethy if HighLine instance is currently using color escapes.
+ def use_color?
+ !!use_color
+ end
+
+ # Resets the use of color.
+ def reset_use_color
+ @use_color = true
+ end
+
+ # Pass +false+ to turn off HighLine's EOF tracking.
+ attr_accessor :track_eof
+
+ # Returns true if HighLine is currently tracking EOF for input.
+ def track_eof?
+ !!track_eof
+ end
+
# @return [Integer] The current column setting for wrapping output.
attr_reader :wrap_at
@@ -294,23 +289,20 @@ class HighLine
# (:blue for BLUE, for example). A CLEAR will automatically be embedded to
# the end of the returned String.
#
- # This method returns the original _string_ unchanged if HighLine::use_color?
+ # This method returns the original _string_ unchanged if use_color?
# is +false+.
#
# @param string [String] string to be colored
# @param colors [Array<Symbol>] array of colors like [:red, :blue]
# @return [String] (ANSI escaped) colored string
# @example
- # HighLine.color("Sustainable", :green, :bold)
+ # cli = HighLine.new
+ # cli.color("Sustainable", :green, :bold)
# # => "\e[32m\e[1mSustainable\e[0m"
- def self.color( string, *colors )
- return string unless self.use_color?
- Style(*colors).color(string)
- end
-
- # (see .color)
- # This method is a clone of the HighLine.color class method.
- # But it checks for use_color? per instance
+ #
+ # # As class method (delegating to HighLine.default_instance)
+ # HighLine.color("Sustainable", :green, :bold)
+ #
def color(string, *colors)
return string unless use_color?
HighLine.Style(*colors).color(string)
@@ -327,29 +319,19 @@ class HighLine
# s.code # => "\e[31m\e[34m"
#
# HighLine.color_code(:red, :blue) # => "\e[31m\e[34m"
-
- def self.color_code(*colors)
- Style(*colors).code
- end
-
- # (see HighLine.color_code)
- # Convenience instance method. It delegates to the class method.
+ #
+ # cli = HighLine.new
+ # cli.color_code(:red, :blue) # => "\e[31m\e[34m"
+ #
def color_code(*colors)
- self.class.color_code(*colors)
+ HighLine.Style(*colors).code
end
# Remove color codes from a string.
# @param string [String] to be decolorized
# @return [String] without the ANSI escape sequence (colors)
- def self.uncolor(string)
- Style.uncolor(string)
- end
-
- # (see .uncolor)
- # Convenience instance method. It delegates to the class method.
-
def uncolor(string)
- self.class.uncolor(string)
+ Style.uncolor(string)
end
# Renders a list of itens using a {ListRenderer}
@@ -631,4 +613,6 @@ class HighLine
end
end
+HighLine.default_instance = HighLine.new
+
require "highline/string"
diff --git a/lib/highline/import.rb b/lib/highline/import.rb
index 157752d..c2e6084 100644
--- a/lib/highline/import.rb
+++ b/lib/highline/import.rb
@@ -10,22 +10,19 @@
require "highline"
require "forwardable"
-$terminal = HighLine.new
-
#
# <tt>require "highline/import"</tt> adds shortcut methods to Kernel, making
# {HighLine#agree}, {HighLine#ask}, {HighLine#choose} and {HighLine#say}
# globally available. This is handy for
-# quick and dirty input and output. These methods use the HighLine object in
-# the global variable <tt>$terminal</tt>, which is initialized to use
-# <tt>$stdin</tt> and <tt>$stdout</tt> (you are free to change this).
-# Otherwise, these methods are identical to their {HighLine} counterparts, see that
-# class for detailed explanations.
+# quick and dirty input and output. These methods use HighLine.default_instance
+# which is initialized to use <tt>$stdin</tt> and <tt>$stdout</tt> (you are free
+# to change this).
+# Otherwise, these methods are identical to their {HighLine} counterparts,
+# see that class for detailed explanations.
#
module Kernel
extend Forwardable
- def_delegators :$terminal, :agree, :ask, :choose, :say,
- :use_color=, :use_color?, :reset_use_color
+ def_instance_delegators :HighLine, :agree, :ask, :choose, :say
end
# When requiring 'highline/import' HighLine adds {#or_ask} to Object so
diff --git a/lib/highline/simulate.rb b/lib/highline/simulate.rb
index a39c1f1..c1ee6df 100644
--- a/lib/highline/simulate.rb
+++ b/lib/highline/simulate.rb
@@ -44,18 +44,18 @@ class HighLine
end
# A wrapper method that temporarily replaces the Highline
- # instance in $terminal with an instance of this object
+ # instance in HighLine.default_instance with an instance of this object
# for the duration of the block
#
# @param strings [String] preloaded string buffer that
# will feed the input operations when simulating.
def self.with(*strings)
- @input = $terminal.instance_variable_get :@input
- $terminal.instance_variable_set :@input, new(strings)
+ @input = HighLine.default_instance.instance_variable_get :@input
+ HighLine.default_instance.instance_variable_set :@input, new(strings)
yield
ensure
- $terminal.instance_variable_set :@input, @input
+ HighLine.default_instance.instance_variable_set :@input, @input
end
end
end
diff --git a/lib/highline/version.rb b/lib/highline/version.rb
index 4b8609e..5b6b4f8 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.9".freeze
+ VERSION = "2.0.0-develop.10".freeze
end
diff --git a/test/acceptance/acceptance.rb b/test/acceptance/acceptance.rb
index edef458..5ba6784 100644
--- a/test/acceptance/acceptance.rb
+++ b/test/acceptance/acceptance.rb
@@ -35,7 +35,7 @@ james@grayproductions.net
=== HighLine Acceptance Tests Report
Date: #{Time.now.utc}
HighLine::VERSION: #{HighLine::VERSION}
-Terminal: #{$terminal.terminal.class}
+Terminal: #{HighLine.default_instance.terminal.class}
RUBY_DESCRIPTION: #{RUBY_DESCRIPTION rescue 'not available'}
Readline::VERSION: #{Readline::VERSION rescue 'not availabe'}
ENV['SHELL']: #{ENV['SHELL']}
diff --git a/test/test_highline.rb b/test/test_highline.rb
index ce1233d..76ed572 100755
--- a/test/test_highline.rb
+++ b/test/test_highline.rb
@@ -511,12 +511,12 @@ class TestHighLine < Minitest::Test
def test_color_setting_per_instance
require 'highline/import'
- old_glob_term = $terminal
+ old_glob_instance = HighLine.default_instance
old_setting = HighLine.use_color?
gterm_input = StringIO.new
gterm_output = StringIO.new
- $terminal = HighLine.new(gterm_input, gterm_output)
+ HighLine.default_instance = HighLine.new(gterm_input, gterm_output)
# It can set coloring at HighLine class
cli_input = StringIO.new
@@ -600,7 +600,7 @@ class TestHighLine < Minitest::Test
HighLine.use_color = old_setting
@terminal.use_color = old_setting
- $terminal = old_glob_term
+ HighLine.default_instance = old_glob_instance
end
def test_reset_use_color
@@ -1606,16 +1606,18 @@ class TestHighLine < Minitest::Test
def test_track_eof
assert_raises(EOFError) { @terminal.ask("Any input left? ") }
-
+
# turn EOF tracking
- old_setting = HighLine.track_eof?
+ old_instance = HighLine.default_instance
+ HighLine.default_instance = HighLine.new(StringIO.new, StringIO.new)
HighLine.track_eof = false
begin
- @terminal.ask("And now? ") # this will still blow up, nothing available
+ require 'highline/import'
+ ask("And now? ") # this will still blow up, nothing available
rescue
refute_equal(EOFError, $!.class) # but HighLine's safe guards are off
end
- HighLine.track_eof = old_setting
+ HighLine.default_instance = old_instance
end
def test_version
diff --git a/test/test_import.rb b/test/test_import.rb
index b238925..40ff56d 100644
--- a/test/test_import.rb
+++ b/test/test_import.rb
@@ -20,13 +20,21 @@ class TestImport < Minitest::Test
assert_respond_to(self, :choose)
assert_respond_to(self, :say)
end
+
+ def test_healthy_default_instance_after_import
+ refute_nil HighLine.default_instance
+ assert_instance_of HighLine, HighLine.default_instance
+
+ # If correctly initialized, it will contain several ins vars.
+ refute_empty HighLine.default_instance.instance_variables
+ end
def test_or_ask
- old_terminal = $terminal
+ old_instance = HighLine.default_instance
input = StringIO.new
output = StringIO.new
- $terminal = HighLine.new(input, output)
+ HighLine.default_instance = HighLine.new(input, output)
input << "10\n"
input.rewind
@@ -40,16 +48,16 @@ class TestImport < Minitest::Test
assert_equal(10, 20.or_ask("How much? ", Integer) { |q| q.in = 1..10 })
ensure
- $terminal = old_terminal
+ HighLine.default_instance = old_instance
end
def test_redirection
- old_terminal = $terminal
+ old_instance = HighLine.default_instance
- $terminal = HighLine.new(nil, (output = StringIO.new))
+ HighLine.default_instance = HighLine.new(nil, (output = StringIO.new))
say("Testing...")
assert_equal("Testing...\n", output.string)
ensure
- $terminal = old_terminal
+ HighLine.default_instance = old_instance
end
end
diff --git a/test/test_simulator.rb b/test/test_simulator.rb
index dffeb88..5f1f305 100644
--- a/test/test_simulator.rb
+++ b/test/test_simulator.rb
@@ -7,7 +7,7 @@ class SimulatorTest < Minitest::Test
def setup
input = StringIO.new
output = StringIO.new
- $terminal = HighLine.new(input, output)
+ HighLine.default_instance = HighLine.new(input, output)
end
def test_simulator