highline 1.7.10 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +84 -0
  4. data/.simplecov +5 -0
  5. data/.travis.yml +35 -9
  6. data/Changelog.md +214 -15
  7. data/Gemfile +16 -5
  8. data/README.md +202 -0
  9. data/Rakefile +8 -20
  10. data/appveyor.yml +37 -0
  11. data/examples/ansi_colors.rb +6 -11
  12. data/examples/asking_for_arrays.rb +6 -2
  13. data/examples/basic_usage.rb +31 -21
  14. data/examples/color_scheme.rb +11 -10
  15. data/examples/get_character.rb +8 -4
  16. data/examples/limit.rb +4 -0
  17. data/examples/menus.rb +16 -10
  18. data/examples/overwrite.rb +9 -5
  19. data/examples/page_and_wrap.rb +5 -4
  20. data/examples/password.rb +4 -0
  21. data/examples/repeat_entry.rb +10 -5
  22. data/examples/trapping_eof.rb +2 -1
  23. data/examples/using_readline.rb +2 -1
  24. data/highline.gemspec +25 -27
  25. data/lib/highline/builtin_styles.rb +129 -0
  26. data/lib/highline/color_scheme.rb +49 -32
  27. data/lib/highline/compatibility.rb +11 -4
  28. data/lib/highline/custom_errors.rb +57 -0
  29. data/lib/highline/import.rb +19 -12
  30. data/lib/highline/io_console_compatible.rb +37 -0
  31. data/lib/highline/list.rb +177 -0
  32. data/lib/highline/list_renderer.rb +261 -0
  33. data/lib/highline/menu/item.rb +32 -0
  34. data/lib/highline/menu.rb +306 -111
  35. data/lib/highline/paginator.rb +52 -0
  36. data/lib/highline/question/answer_converter.rb +103 -0
  37. data/lib/highline/question.rb +281 -131
  38. data/lib/highline/question_asker.rb +150 -0
  39. data/lib/highline/simulate.rb +24 -13
  40. data/lib/highline/statement.rb +88 -0
  41. data/lib/highline/string.rb +36 -0
  42. data/lib/highline/string_extensions.rb +83 -64
  43. data/lib/highline/style.rb +196 -63
  44. data/lib/highline/template_renderer.rb +62 -0
  45. data/lib/highline/terminal/io_console.rb +36 -0
  46. data/lib/highline/terminal/ncurses.rb +38 -0
  47. data/lib/highline/terminal/unix_stty.rb +51 -0
  48. data/lib/highline/terminal.rb +190 -0
  49. data/lib/highline/version.rb +3 -1
  50. data/lib/highline/wrapper.rb +53 -0
  51. data/lib/highline.rb +390 -788
  52. metadata +56 -35
  53. data/INSTALL +0 -59
  54. data/README.rdoc +0 -74
  55. data/lib/highline/system_extensions.rb +0 -254
  56. data/setup.rb +0 -1360
  57. data/test/string_methods.rb +0 -32
  58. data/test/tc_color_scheme.rb +0 -96
  59. data/test/tc_highline.rb +0 -1402
  60. data/test/tc_import.rb +0 -52
  61. data/test/tc_menu.rb +0 -439
  62. data/test/tc_simulator.rb +0 -33
  63. data/test/tc_string_extension.rb +0 -33
  64. data/test/tc_string_highline.rb +0 -38
  65. data/test/tc_style.rb +0 -578
@@ -1,3 +1,6 @@
1
+ # coding: utf-8
2
+
3
+ #--
1
4
  # color_scheme.rb
2
5
  #
3
6
  # Created by Jeremy Hinegardner on 2007-01-24
@@ -8,16 +11,15 @@
8
11
  class HighLine
9
12
  #
10
13
  # ColorScheme objects encapsulate a named set of colors to be used in the
11
- # HighLine.colors() method call. For example, by applying a ColorScheme that
14
+ # {HighLine.color} method call. For example, by applying a ColorScheme that
12
15
  # has a <tt>:warning</tt> color then the following could be used:
13
16
  #
14
- # colors("This is a warning", :warning)
17
+ # color("This is a warning", :warning)
15
18
  #
16
19
  # A ColorScheme contains named sets of HighLine color constants.
17
20
  #
18
- # Example: Instantiating a color scheme, applying it to HighLine,
19
- # and using it:
20
- #
21
+ # @example Instantiating a color scheme, applying it to HighLine,
22
+ # and using it:
21
23
  # ft = HighLine::ColorScheme.new do |cs|
22
24
  # cs[:headline] = [ :bold, :yellow, :on_black ]
23
25
  # cs[:horizontal_line] = [ :bold, :white ]
@@ -46,63 +48,78 @@ class HighLine
46
48
  # converted to <tt>:symbols</tt> and values are converted to HighLine
47
49
  # constants.
48
50
  #
49
- def initialize( h = nil )
50
- @scheme = Hash.new
51
- load_from_hash(h) unless h.nil?
51
+ # @param h [Hash]
52
+ def initialize(h = nil)
53
+ @scheme = {}
54
+ load_from_hash(h) if h
52
55
  yield self if block_given?
53
56
  end
54
57
 
55
58
  # Load multiple colors from key/value pairs.
56
- def load_from_hash( h )
59
+ # @param h [Hash]
60
+ def load_from_hash(h)
57
61
  h.each_pair do |color_tag, constants|
58
62
  self[color_tag] = constants
59
63
  end
60
64
  end
61
65
 
62
66
  # Does this color scheme include the given tag name?
63
- def include?( color_tag )
67
+ # @param color_tag [#to_sym]
68
+ # @return [Boolean]
69
+ def include?(color_tag)
64
70
  @scheme.keys.include?(to_symbol(color_tag))
65
71
  end
66
72
 
67
73
  # Allow the scheme to be accessed like a Hash.
68
- def []( color_tag )
74
+ # @param color_tag [#to_sym]
75
+ # @return [Style]
76
+ def [](color_tag)
69
77
  @scheme[to_symbol(color_tag)]
70
78
  end
71
79
 
72
80
  # Retrieve the original form of the scheme
73
- def definition( color_tag )
81
+ # @param color_tag [#to_sym]
82
+ def definition(color_tag)
74
83
  style = @scheme[to_symbol(color_tag)]
75
84
  style && style.list
76
85
  end
77
86
 
78
87
  # Retrieve the keys in the scheme
88
+ # @return [Array] of keys
79
89
  def keys
80
90
  @scheme.keys
81
91
  end
82
92
 
83
93
  # Allow the scheme to be set like a Hash.
84
- def []=( color_tag, constants )
85
- @scheme[to_symbol(color_tag)] = HighLine::Style.new(:name=>color_tag.to_s.downcase.to_sym,
86
- :list=>constants, :no_index=>true)
94
+ # @param color_tag [#to_sym]
95
+ # @param constants [Array<Symbol>] Array of Style symbols
96
+ def []=(color_tag, constants)
97
+ @scheme[to_symbol(color_tag)] =
98
+ HighLine::Style.new(name: color_tag.to_s.downcase.to_sym,
99
+ list: constants,
100
+ no_index: true)
87
101
  end
88
102
 
89
103
  # Retrieve the color scheme hash (in original definition format)
104
+ # @return [Hash] scheme as Hash. It may be reused in a new ColorScheme.
90
105
  def to_hash
91
- @scheme.inject({}) { |hsh, pair| key, value = pair; hsh[key] = value.list; hsh }
106
+ @scheme.each_with_object({}) do |pair, hsh|
107
+ key, value = pair
108
+ hsh[key] = value.list
109
+ end
92
110
  end
93
111
 
94
-
95
112
  private
96
113
 
97
114
  # Return a normalized representation of a color name.
98
- def to_symbol( t )
115
+ def to_symbol(t)
99
116
  t.to_s.downcase
100
117
  end
101
118
 
102
119
  # Return a normalized representation of a color setting.
103
- def to_constant( v )
120
+ def to_constant(v)
104
121
  v = v.to_s if v.is_a?(Symbol)
105
- if v.is_a?(::String) then
122
+ if v.is_a?(::String)
106
123
  HighLine.const_get(v.upcase)
107
124
  else
108
125
  v
@@ -112,23 +129,23 @@ class HighLine
112
129
 
113
130
  # A sample ColorScheme.
114
131
  class SampleColorScheme < ColorScheme
132
+ SAMPLE_SCHEME = {
133
+ critical: [:yellow, :on_red],
134
+ error: [:bold, :red],
135
+ warning: [:bold, :yellow],
136
+ notice: [:bold, :magenta],
137
+ info: [:bold, :cyan],
138
+ debug: [:bold, :green],
139
+ row_even: [:cyan],
140
+ row_odd: [:magenta]
141
+ }.freeze
115
142
  #
116
143
  # Builds the sample scheme with settings for <tt>:critical</tt>,
117
144
  # <tt>:error</tt>, <tt>:warning</tt>, <tt>:notice</tt>, <tt>:info</tt>,
118
145
  # <tt>:debug</tt>, <tt>:row_even</tt>, and <tt>:row_odd</tt> colors.
119
146
  #
120
- def initialize( h = nil )
121
- scheme = {
122
- :critical => [ :yellow, :on_red ],
123
- :error => [ :bold, :red ],
124
- :warning => [ :bold, :yellow ],
125
- :notice => [ :bold, :magenta ],
126
- :info => [ :bold, :cyan ],
127
- :debug => [ :bold, :green ],
128
- :row_even => [ :cyan ],
129
- :row_odd => [ :magenta ]
130
- }
131
- super(scheme)
147
+ def initialize(_h = nil)
148
+ super(SAMPLE_SCHEME)
132
149
  end
133
150
  end
134
151
  end
@@ -1,16 +1,23 @@
1
+ # coding: utf-8
2
+
1
3
  unless STDIN.respond_to? :getbyte
4
+ # HighLine adds #getbyte alias to #getc when #getbyte is not available.
2
5
  class IO
3
- alias_method :getbyte, :getc
6
+ # alias to #getc when #getbyte is not available
7
+ alias getbyte getc
4
8
  end
5
9
 
10
+ # HighLine adds #getbyte alias to #getc when #getbyte is not available.
6
11
  class StringIO
7
- alias_method :getbyte, :getc
12
+ # alias to #getc when #getbyte is not available
13
+ alias getbyte getc
8
14
  end
9
15
  end
10
16
 
11
17
  unless "".respond_to? :each_line
12
- # Not a perfect translation, but sufficient for our needs.
18
+ # HighLine adds #each_line alias to #each when each_line is not available.
13
19
  class String
14
- alias_method :each_line, :each
20
+ # alias to #each when each_line is not available.
21
+ alias each_line each
15
22
  end
16
23
  end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ class HighLine
4
+ # Internal HighLine errors.
5
+ module CustomErrors
6
+ # An error that responds to :explanation_key
7
+ class ExplainableError < StandardError
8
+ # Explanation key as Symbol or nil. Used to
9
+ # select the proper error message to be displayed.
10
+ # @return [nil, Symbol] explanation key to get the
11
+ # proper error message.
12
+ def explanation_key
13
+ nil
14
+ end
15
+ end
16
+
17
+ # Bare Question error
18
+ class QuestionError < ExplainableError
19
+ # (see ExplainableError#explanation_key)
20
+ def explanation_key
21
+ nil
22
+ end
23
+ end
24
+
25
+ # Invalid Question error
26
+ class NotValidQuestionError < ExplainableError
27
+ # (see ExplainableError#explanation_key)
28
+ def explanation_key
29
+ :not_valid
30
+ end
31
+ end
32
+
33
+ # Out of Range Question error
34
+ class NotInRangeQuestionError < ExplainableError
35
+ # (see ExplainableError#explanation_key)
36
+ def explanation_key
37
+ :not_in_range
38
+ end
39
+ end
40
+
41
+ # Unconfirmed Question error
42
+ class NoConfirmationQuestionError < ExplainableError
43
+ # (see ExplainableError#explanation_key)
44
+ def explanation_key
45
+ nil
46
+ end
47
+ end
48
+
49
+ # Unavailable auto complete error
50
+ class NoAutoCompleteMatch < ExplainableError
51
+ # (see ExplainableError#explanation_key)
52
+ def explanation_key
53
+ :no_completion
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  # import.rb
2
4
  #
3
5
  # Created by James Edward Gray II on 2005-04-26.
@@ -8,22 +10,23 @@
8
10
  require "highline"
9
11
  require "forwardable"
10
12
 
11
- $terminal = HighLine.new
12
-
13
13
  #
14
14
  # <tt>require "highline/import"</tt> adds shortcut methods to Kernel, making
15
- # agree(), ask(), choose() and say() globally available. This is handy for
16
- # quick and dirty input and output. These methods use the HighLine object in
17
- # the global variable <tt>$terminal</tt>, which is initialized to used
18
- # <tt>$stdin</tt> and <tt>$stdout</tt> (you are free to change this).
19
- # Otherwise, these methods are identical to their HighLine counterparts, see that
20
- # class for detailed explanations.
15
+ # {HighLine#agree}, {HighLine#ask}, {HighLine#choose} and {HighLine#say}
16
+ # globally available. This is handy for
17
+ # quick and dirty input and output. These methods use HighLine.default_instance
18
+ # which is initialized to use <tt>$stdin</tt> and <tt>$stdout</tt> (you are free
19
+ # to change this).
20
+ # Otherwise, these methods are identical to their {HighLine} counterparts,
21
+ # see that class for detailed explanations.
21
22
  #
22
23
  module Kernel
23
24
  extend Forwardable
24
- def_delegators :$terminal, :agree, :ask, :choose, :say
25
+ def_instance_delegators :HighLine, :agree, :ask, :choose, :say
25
26
  end
26
27
 
28
+ # When requiring 'highline/import' HighLine adds {#or_ask} to Object so
29
+ # it is globally available.
27
30
  class Object
28
31
  #
29
32
  # Tries this object as a _first_answer_ for a HighLine::Question. See that
@@ -31,11 +34,15 @@ class Object
31
34
  #
32
35
  # *Warning*: This Object will be passed to String() before set.
33
36
  #
34
- def or_ask( *args, &details )
37
+ # @param args [Array<#to_s>]
38
+ # @param details [lambda] block to be called with the question
39
+ # instance as argument.
40
+ # @return [String] answer
41
+ def or_ask(*args, &details)
35
42
  ask(*args) do |question|
36
- question.first_answer = String(self) unless nil?
43
+ question.first_answer = String(self)
37
44
 
38
- details.call(question) unless details.nil?
45
+ yield(question) if details
39
46
  end
40
47
  end
41
48
  end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ require "stringio"
4
+ require "tempfile"
5
+
6
+ #
7
+ # On tests, we try to simulate input output with
8
+ # StringIO, Tempfile and File objects.
9
+ #
10
+ # For this to be accomplished, we have to do some
11
+ # tweaking so that they respond adequately to the
12
+ # called methods during tests.
13
+ #
14
+
15
+ module IOConsoleCompatible
16
+ def getch
17
+ getc
18
+ end
19
+
20
+ attr_accessor :echo
21
+
22
+ def winsize
23
+ [24, 80]
24
+ end
25
+ end
26
+
27
+ class Tempfile
28
+ include IOConsoleCompatible
29
+ end
30
+
31
+ class File
32
+ include IOConsoleCompatible
33
+ end
34
+
35
+ class StringIO
36
+ include IOConsoleCompatible
37
+ end
@@ -0,0 +1,177 @@
1
+ # coding: utf-8
2
+
3
+ class HighLine
4
+ # List class with some convenience methods like {#col_down}.
5
+ class List
6
+ # Original given *items* argument.
7
+ # It's frozen at initialization time and
8
+ # all later transformations will happen on {#list}.
9
+ # @return [Array]
10
+ attr_reader :items
11
+
12
+ # Number of columns for each list row.
13
+ # @return [Integer]
14
+ attr_reader :cols
15
+
16
+ # Columns turn into rows in transpose mode.
17
+ # @return [Boolean]
18
+ #
19
+ # @example A two columns array like this:
20
+ # [ [ "a", "b" ],
21
+ # [ "c", "d" ],
22
+ # [ "e", "f" ],
23
+ # [ "g", "h" ],
24
+ # [ "i", "j" ] ]
25
+ #
26
+ # @example When in transpose mode will be like this:
27
+ # [ [ "a", "c", "e", "g", "i" ],
28
+ # [ "b", "d", "f", "h", "j" ] ]
29
+ #
30
+ # @see #col_down_mode
31
+
32
+ attr_reader :transpose_mode
33
+
34
+ # Content are distributed first by column in col down mode.
35
+ # @return [Boolean]
36
+ #
37
+ # @example A two columns array like this:
38
+ # [ [ "a", "b" ],
39
+ # [ "c", "d" ],
40
+ # [ "e", "f" ],
41
+ # [ "g", "h" ],
42
+ # [ "i", "j" ] ]
43
+ #
44
+ # @example In col down mode will be like this:
45
+ # [ [ "a", "f"],
46
+ # [ "b", "g"],
47
+ # [ "c", "h"],
48
+ # [ "d", "i"],
49
+ # [ "e", "j"] ]
50
+ #
51
+ # @see #transpose_mode
52
+
53
+ attr_reader :col_down_mode
54
+
55
+ # @param items [#to_a] an array of items to compose the list.
56
+ # @param options [Hash] a hash of options to tailor the list.
57
+ # @option options [Boolean] :transpose (false) set {#transpose_mode}.
58
+ # @option options [Boolean] :col_down (false) set {#col_down_mode}.
59
+ # @option options [Integer] :cols (1) set {#cols}.
60
+
61
+ def initialize(items, options = {})
62
+ @items = items.to_a.dup.freeze
63
+ @transpose_mode = options.fetch(:transpose) { false }
64
+ @col_down_mode = options.fetch(:col_down) { false }
65
+ @cols = options.fetch(:cols) { 1 }
66
+ build
67
+ end
68
+
69
+ # Transpose the (already sliced by rows) list,
70
+ # turning its rows into columns.
71
+ # @return [self]
72
+ def transpose
73
+ first_row = @list[0]
74
+ other_rows = @list[1..-1]
75
+ @list = first_row.zip(*other_rows)
76
+ self
77
+ end
78
+
79
+ # Slice the list by rows and transpose it.
80
+ # @return [self]
81
+ def col_down
82
+ slice_by_rows
83
+ transpose
84
+ self
85
+ end
86
+
87
+ # Slice the list by rows. The row count is calculated
88
+ # indirectly based on the {#cols} param and the items count.
89
+ # @return [self]
90
+ def slice_by_rows
91
+ @list = items_sliced_by_rows
92
+ self
93
+ end
94
+
95
+ # Slice the list by cols based on the {#cols} param.
96
+ # @return [self]
97
+ def slice_by_cols
98
+ @list = items_sliced_by_cols
99
+ self
100
+ end
101
+
102
+ # Set the cols number.
103
+ # @return [self]
104
+ def cols=(cols)
105
+ @cols = cols
106
+ build
107
+ end
108
+
109
+ # Returns an Array representation of the list
110
+ # in its current state.
111
+ # @return [Array] @list.dup
112
+ def list
113
+ @list.dup
114
+ end
115
+
116
+ # (see #list)
117
+ def to_a
118
+ list
119
+ end
120
+
121
+ # Stringfies the list in its current state.
122
+ # It joins each individual _cell_ with the current
123
+ # {#row_join_string} between them.
124
+ # It joins each individual row with a
125
+ # newline character. So the returned String is
126
+ # suitable to be directly outputed
127
+ # to the screen, preserving row/columns divisions.
128
+ # @return [String]
129
+ def to_s
130
+ list.map { |row| stringfy(row) }.join
131
+ end
132
+
133
+ # The String that will be used to join each
134
+ # cell of the list and stringfying it.
135
+ # @return [String] defaults to " " (space)
136
+ def row_join_string
137
+ @row_join_string ||= " "
138
+ end
139
+
140
+ # Set the {#row_join_string}.
141
+ # @see #row_join_string
142
+ attr_writer :row_join_string
143
+
144
+ # Returns the row join string size.
145
+ # Useful for calculating the actual size of
146
+ # rendered list.
147
+ # @return [Integer]
148
+ def row_join_str_size
149
+ row_join_string.size
150
+ end
151
+
152
+ private
153
+
154
+ def build
155
+ slice_by_cols
156
+ transpose if transpose_mode
157
+ col_down if col_down_mode
158
+ self
159
+ end
160
+
161
+ def items_sliced_by_cols
162
+ items.each_slice(cols).to_a
163
+ end
164
+
165
+ def items_sliced_by_rows
166
+ items.each_slice(row_count).to_a
167
+ end
168
+
169
+ def row_count
170
+ (items.count / cols.to_f).ceil
171
+ end
172
+
173
+ def stringfy(row)
174
+ row.compact.join(row_join_string) + "\n"
175
+ end
176
+ end
177
+ end