highline-sgonyea 1.6.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +2 -0
  2. data/AUTHORS +3 -0
  3. data/CHANGELOG +308 -0
  4. data/COPYING +340 -0
  5. data/INSTALL +55 -0
  6. data/LICENSE +7 -0
  7. data/README.rdoc +63 -0
  8. data/Rakefile +50 -0
  9. data/TODO +6 -0
  10. data/examples/ansi_colors.rb +36 -0
  11. data/examples/asking_for_arrays.rb +16 -0
  12. data/examples/basic_usage.rb +73 -0
  13. data/examples/color_scheme.rb +30 -0
  14. data/examples/limit.rb +10 -0
  15. data/examples/menus.rb +63 -0
  16. data/examples/overwrite.rb +17 -0
  17. data/examples/page_and_wrap.rb +320 -0
  18. data/examples/password.rb +5 -0
  19. data/examples/repeat_entry.rb +19 -0
  20. data/examples/trapping_eof.rb +20 -0
  21. data/examples/using_readline.rb +15 -0
  22. data/highline.gemspec +36 -0
  23. data/lib/highline.rb +1000 -0
  24. data/lib/highline/color_scheme.rb +134 -0
  25. data/lib/highline/compatibility.rb +16 -0
  26. data/lib/highline/import.rb +41 -0
  27. data/lib/highline/menu.rb +398 -0
  28. data/lib/highline/question.rb +472 -0
  29. data/lib/highline/simulate.rb +48 -0
  30. data/lib/highline/string_extensions.rb +131 -0
  31. data/lib/highline/style.rb +181 -0
  32. data/lib/highline/system_extensions.rb +186 -0
  33. data/setup.rb +1360 -0
  34. data/site/.cvsignore +1 -0
  35. data/site/highline.css +65 -0
  36. data/site/images/logo.png +0 -0
  37. data/site/index.html +58 -0
  38. data/test/string_methods.rb +32 -0
  39. data/test/tc_color_scheme.rb +96 -0
  40. data/test/tc_highline.rb +1027 -0
  41. data/test/tc_import.rb +52 -0
  42. data/test/tc_menu.rb +427 -0
  43. data/test/tc_string_extension.rb +20 -0
  44. data/test/tc_string_highline.rb +38 -0
  45. data/test/tc_style.rb +567 -0
  46. data/test/ts_all.rb +16 -0
  47. metadata +118 -0
@@ -0,0 +1,134 @@
1
+ # color_scheme.rb
2
+ #
3
+ # Created by Jeremy Hinegardner on 2007-01-24
4
+ # Copyright 2007. All rights reserved
5
+ #
6
+ # This is Free Software. See LICENSE and COPYING for details
7
+
8
+ class HighLine
9
+ #
10
+ # 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
12
+ # has a <tt>:warning</tt> color then the following could be used:
13
+ #
14
+ # colors("This is a warning", :warning)
15
+ #
16
+ # A ColorScheme contains named sets of HighLine color constants.
17
+ #
18
+ # Example: Instantiating a color scheme, applying it to HighLine,
19
+ # and using it:
20
+ #
21
+ # ft = HighLine::ColorScheme.new do |cs|
22
+ # cs[:headline] = [ :bold, :yellow, :on_black ]
23
+ # cs[:horizontal_line] = [ :bold, :white ]
24
+ # cs[:even_row] = [ :green ]
25
+ # cs[:odd_row] = [ :magenta ]
26
+ # end
27
+ #
28
+ # HighLine.color_scheme = ft
29
+ # say("<%= color('Headline', :headline) %>")
30
+ # say("<%= color('-'*20, :horizontal_line) %>")
31
+ # i = true
32
+ # ("A".."D").each do |row|
33
+ # if i then
34
+ # say("<%= color('#{row}', :even_row ) %>")
35
+ # else
36
+ # say("<%= color('#{row}', :odd_row) %>")
37
+ # end
38
+ # i = !i
39
+ # end
40
+ #
41
+ #
42
+ class ColorScheme
43
+ #
44
+ # Create an instance of HighLine::ColorScheme. The customization can
45
+ # happen as a passed in Hash or via the yielded block. Key's are
46
+ # converted to <tt>:symbols</tt> and values are converted to HighLine
47
+ # constants.
48
+ #
49
+ def initialize( h = nil )
50
+ @scheme = Hash.new
51
+ load_from_hash(h) unless h.nil?
52
+ yield self if block_given?
53
+ end
54
+
55
+ # Load multiple colors from key/value pairs.
56
+ def load_from_hash( h )
57
+ h.each_pair do |color_tag, constants|
58
+ self[color_tag] = constants
59
+ end
60
+ end
61
+
62
+ # Does this color scheme include the given tag name?
63
+ def include?( color_tag )
64
+ @scheme.keys.include?(to_symbol(color_tag))
65
+ end
66
+
67
+ # Allow the scheme to be accessed like a Hash.
68
+ def []( color_tag )
69
+ @scheme[to_symbol(color_tag)]
70
+ end
71
+
72
+ # Retrieve the original form of the scheme
73
+ def definition( color_tag )
74
+ style = @scheme[to_symbol(color_tag)]
75
+ style && style.list
76
+ end
77
+
78
+ # Retrieve the keys in the scheme
79
+ def keys
80
+ @scheme.keys
81
+ end
82
+
83
+ # 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)
87
+ end
88
+
89
+ # Retrieve the color scheme hash (in original definition format)
90
+ def to_hash
91
+ @scheme.inject({}) { |hsh, pair| key, value = pair; hsh[key] = value.list; hsh }
92
+ end
93
+
94
+
95
+ private
96
+
97
+ # Return a normalized representation of a color name.
98
+ def to_symbol( t )
99
+ t.to_s.downcase
100
+ end
101
+
102
+ # Return a normalized representation of a color setting.
103
+ def to_constant( v )
104
+ v = v.to_s if v.is_a?(Symbol)
105
+ if v.is_a?(::String) then
106
+ HighLine.const_get(v.upcase)
107
+ else
108
+ v
109
+ end
110
+ end
111
+ end
112
+
113
+ # A sample ColorScheme.
114
+ class SampleColorScheme < ColorScheme
115
+ #
116
+ # Builds the sample scheme with settings for <tt>:critical</tt>,
117
+ # <tt>:error</tt>, <tt>:warning</tt>, <tt>:notice</tt>, <tt>:info</tt>,
118
+ # <tt>:debug</tt>, <tt>:row_even</tt>, and <tt>:row_odd</tt> colors.
119
+ #
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)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,16 @@
1
+ unless STDIN.respond_to? :getbyte
2
+ class IO
3
+ alias_method :getbyte, :getc
4
+ end
5
+
6
+ class StringIO
7
+ alias_method :getbyte, :getc
8
+ end
9
+ end
10
+
11
+ unless "".respond_to? :each_line
12
+ # Not a perfect translation, but sufficient for our needs.
13
+ class String
14
+ alias_method :each_line, :each
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ # import.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-04-26.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+ #
6
+ # This is Free Software. See LICENSE and COPYING for details.
7
+
8
+ require "highline"
9
+ require "forwardable"
10
+
11
+ $terminal = HighLine.new
12
+
13
+ #
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.
21
+ #
22
+ module Kernel
23
+ extend Forwardable
24
+ def_delegators :$terminal, :agree, :ask, :choose, :say
25
+ end
26
+
27
+ class Object
28
+ #
29
+ # Tries this object as a _first_answer_ for a HighLine::Question. See that
30
+ # attribute for details.
31
+ #
32
+ # *Warning*: This Object will be passed to String() before set.
33
+ #
34
+ def or_ask( *args, &details )
35
+ ask(*args) do |question|
36
+ question.first_answer = String(self) unless nil?
37
+
38
+ details.call(question) unless details.nil?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,398 @@
1
+ # menu.rb
2
+ #
3
+ # Created by Gregory Thomas Brown on 2005-05-10.
4
+ # Copyright 2005. All rights reserved.
5
+ #
6
+ # This is Free Software. See LICENSE and COPYING for details.
7
+
8
+ require "highline/question"
9
+
10
+ class HighLine
11
+ #
12
+ # Menu objects encapsulate all the details of a call to HighLine.choose().
13
+ # Using the accessors and Menu.choice() and Menu.choices(), the block passed
14
+ # to HighLine.choose() can detail all aspects of menu display and control.
15
+ #
16
+ class Menu < Question
17
+ #
18
+ # Create an instance of HighLine::Menu. All customization is done
19
+ # through the passed block, which should call accessors and choice() and
20
+ # choices() as needed to define the Menu. Note that Menus are also
21
+ # Questions, so all that functionality is available to the block as
22
+ # well.
23
+ #
24
+ def initialize( )
25
+ #
26
+ # Initialize Question objects with ignored values, we'll
27
+ # adjust ours as needed.
28
+ #
29
+ super("Ignored", [ ], &nil) # avoiding passing the block along
30
+
31
+ @items = [ ]
32
+ @hidden_items = [ ]
33
+ @help = Hash.new("There's no help for that topic.")
34
+
35
+ @index = :number
36
+ @index_suffix = ". "
37
+ @select_by = :index_or_name
38
+ @flow = :rows
39
+ @list_option = nil
40
+ @header = nil
41
+ @prompt = "? "
42
+ @layout = :list
43
+ @shell = false
44
+ @nil_on_handled = false
45
+
46
+ # Override Questions responses, we'll set our own.
47
+ @responses = { }
48
+ # Context for action code.
49
+ @highline = nil
50
+
51
+ yield self if block_given?
52
+
53
+ init_help if @shell and not @help.empty?
54
+ end
55
+
56
+ #
57
+ # An _index_ to append to each menu item in display. See
58
+ # Menu.index=() for details.
59
+ #
60
+ attr_reader :index
61
+ #
62
+ # The String placed between an _index_ and a menu item. Defaults to
63
+ # ". ". Switches to " ", when _index_ is set to a String (like "-").
64
+ #
65
+ attr_accessor :index_suffix
66
+ #
67
+ # The _select_by_ attribute controls how the user is allowed to pick a
68
+ # menu item. The available choices are:
69
+ #
70
+ # <tt>:index</tt>:: The user is allowed to type the numerical
71
+ # or alphetical index for their selection.
72
+ # <tt>:index_or_name</tt>:: Allows both methods from the
73
+ # <tt>:index</tt> option and the
74
+ # <tt>:name</tt> option.
75
+ # <tt>:name</tt>:: Menu items are selected by typing a portion
76
+ # of the item name that will be
77
+ # auto-completed.
78
+ #
79
+ attr_accessor :select_by
80
+ #
81
+ # This attribute is passed directly on as the mode to HighLine.list() by
82
+ # all the preset layouts. See that method for appropriate settings.
83
+ #
84
+ attr_accessor :flow
85
+ #
86
+ # This setting is passed on as the third parameter to HighLine.list()
87
+ # by all the preset layouts. See that method for details of its
88
+ # effects. Defaults to +nil+.
89
+ #
90
+ attr_accessor :list_option
91
+ #
92
+ # Used by all the preset layouts to display title and/or introductory
93
+ # information, when set. Defaults to +nil+.
94
+ #
95
+ attr_accessor :header
96
+ #
97
+ # Used by all the preset layouts to ask the actual question to fetch a
98
+ # menu selection from the user. Defaults to "? ".
99
+ #
100
+ attr_accessor :prompt
101
+ #
102
+ # An ERb _layout_ to use when displaying this Menu object. See
103
+ # Menu.layout=() for details.
104
+ #
105
+ attr_reader :layout
106
+ #
107
+ # When set to +true+, responses are allowed to be an entire line of
108
+ # input, including details beyond the command itself. Only the first
109
+ # "word" of input will be matched against the menu choices, but both the
110
+ # command selected and the rest of the line will be passed to provided
111
+ # action blocks. Defaults to +false+.
112
+ #
113
+ attr_accessor :shell
114
+ #
115
+ # When +true+, any selected item handled by provided action code, will
116
+ # return +nil+, instead of the results to the action code. This may
117
+ # prove handy when dealing with mixed menus where only the names of
118
+ # items without any code (and +nil+, of course) will be returned.
119
+ # Defaults to +false+.
120
+ #
121
+ attr_accessor :nil_on_handled
122
+
123
+ #
124
+ # Adds _name_ to the list of available menu items. Menu items will be
125
+ # displayed in the order they are added.
126
+ #
127
+ # An optional _action_ can be associated with this name and if provided,
128
+ # it will be called if the item is selected. The result of the method
129
+ # will be returned, unless _nil_on_handled_ is set (when you would get
130
+ # +nil+ instead). In _shell_ mode, a provided block will be passed the
131
+ # command chosen and any details that followed the command. Otherwise,
132
+ # just the command is passed. The <tt>@highline</tt> variable is set to
133
+ # the current HighLine context before the action code is called and can
134
+ # thus be used for adding output and the like.
135
+ #
136
+ def choice( name, help = nil, &action )
137
+ @items << [name, action]
138
+
139
+ @help[name.to_s.downcase] = help unless help.nil?
140
+ update_responses # rebuild responses based on our settings
141
+ end
142
+
143
+ #
144
+ # A shortcut for multiple calls to the sister method choice(). <b>Be
145
+ # warned:</b> An _action_ set here will apply to *all* provided
146
+ # _names_. This is considered to be a feature, so you can easily
147
+ # hand-off interface processing to a different chunk of code.
148
+ #
149
+ def choices( *names, &action )
150
+ names.each { |n| choice(n, &action) }
151
+ end
152
+
153
+ # Identical to choice(), but the item will not be listed for the user.
154
+ def hidden( name, help = nil, &action )
155
+ @hidden_items << [name, action]
156
+
157
+ @help[name.to_s.downcase] = help unless help.nil?
158
+ end
159
+
160
+ #
161
+ # Sets the indexing style for this Menu object. Indexes are appended to
162
+ # menu items, when displayed in list form. The available settings are:
163
+ #
164
+ # <tt>:number</tt>:: Menu items will be indexed numerically, starting
165
+ # with 1. This is the default method of indexing.
166
+ # <tt>:letter</tt>:: Items will be indexed alphabetically, starting
167
+ # with a.
168
+ # <tt>:none</tt>:: No index will be appended to menu items.
169
+ # <i>any String</i>:: Will be used as the literal _index_.
170
+ #
171
+ # Setting the _index_ to <tt>:none</tt> a literal String, also adjusts
172
+ # _index_suffix_ to a single space and _select_by_ to <tt>:none</tt>.
173
+ # Because of this, you should make a habit of setting the _index_ first.
174
+ #
175
+ def index=( style )
176
+ @index = style
177
+
178
+ # Default settings.
179
+ if @index == :none or @index.is_a?(::String)
180
+ @index_suffix = " "
181
+ @select_by = :name
182
+ end
183
+ end
184
+
185
+ #
186
+ # Initializes the help system by adding a <tt>:help</tt> choice, some
187
+ # action code, and the default help listing.
188
+ #
189
+ def init_help( )
190
+ return if @items.include?(:help)
191
+
192
+ topics = @help.keys.sort
193
+ help_help = @help.include?("help") ? @help["help"] :
194
+ "This command will display helpful messages about " +
195
+ "functionality, like this one. To see the help for " +
196
+ "a specific topic enter:\n\thelp [TOPIC]\nTry asking " +
197
+ "for help on any of the following:\n\n" +
198
+ "<%= list(#{topics.inspect}, :columns_across) %>"
199
+ choice(:help, help_help) do |command, topic|
200
+ topic.strip!
201
+ topic.downcase!
202
+ if topic.empty?
203
+ @highline.say(@help["help"])
204
+ else
205
+ @highline.say("= #{topic}\n\n#{@help[topic]}")
206
+ end
207
+ end
208
+ end
209
+
210
+ #
211
+ # Used to set help for arbitrary topics. Use the topic <tt>"help"</tt>
212
+ # to override the default message.
213
+ #
214
+ def help( topic, help )
215
+ @help[topic] = help
216
+ end
217
+
218
+ #
219
+ # Setting a _layout_ with this method also adjusts some other attributes
220
+ # of the Menu object, to ideal defaults for the chosen _layout_. To
221
+ # account for that, you probably want to set a _layout_ first in your
222
+ # configuration block, if needed.
223
+ #
224
+ # Accepted settings for _layout_ are:
225
+ #
226
+ # <tt>:list</tt>:: The default _layout_. The _header_ if set
227
+ # will appear at the top on its own line with
228
+ # a trailing colon. Then the list of menu
229
+ # items will follow. Finally, the _prompt_
230
+ # will be used as the ask()-like question.
231
+ # <tt>:one_line</tt>:: A shorter _layout_ that fits on one line.
232
+ # The _header_ comes first followed by a
233
+ # colon and spaces, then the _prompt_ with menu
234
+ # items between trailing parenthesis.
235
+ # <tt>:menu_only</tt>:: Just the menu items, followed up by a likely
236
+ # short _prompt_.
237
+ # <i>any ERb String</i>:: Will be taken as the literal _layout_. This
238
+ # String can access <tt>@header</tt>,
239
+ # <tt>@menu</tt> and <tt>@prompt</tt>, but is
240
+ # otherwise evaluated in the typical HighLine
241
+ # context, to provide access to utilities like
242
+ # HighLine.list() primarily.
243
+ #
244
+ # If set to either <tt>:one_line</tt>, or <tt>:menu_only</tt>, _index_
245
+ # will default to <tt>:none</tt> and _flow_ will default to
246
+ # <tt>:inline</tt>.
247
+ #
248
+ def layout=( new_layout )
249
+ @layout = new_layout
250
+
251
+ # Default settings.
252
+ case @layout
253
+ when :one_line, :menu_only
254
+ self.index = :none
255
+ @flow = :inline
256
+ end
257
+ end
258
+
259
+ #
260
+ # This method returns all possible options for auto-completion, based
261
+ # on the settings of _index_ and _select_by_.
262
+ #
263
+ def options( )
264
+ # add in any hidden menu commands
265
+ @items.concat(@hidden_items)
266
+
267
+ by_index = if @index == :letter
268
+ l_index = "`"
269
+ @items.map { "#{l_index.succ!}" }
270
+ else
271
+ (1 .. @items.size).collect { |s| String(s) }
272
+ end
273
+ by_name = @items.collect { |c| c.first }
274
+
275
+ case @select_by
276
+ when :index then
277
+ by_index
278
+ when :name
279
+ by_name
280
+ else
281
+ by_index + by_name
282
+ end
283
+ ensure
284
+ # make sure the hidden items are removed, before we return
285
+ @items.slice!(@items.size - @hidden_items.size, @hidden_items.size)
286
+ end
287
+
288
+ #
289
+ # This method processes the auto-completed user selection, based on the
290
+ # rules for this Menu object. If an action was provided for the
291
+ # selection, it will be executed as described in Menu.choice().
292
+ #
293
+ def select( highline_context, selection, details = nil )
294
+ # add in any hidden menu commands
295
+ @items.concat(@hidden_items)
296
+
297
+ # Find the selected action.
298
+ name, action = if selection =~ /^\d+$/
299
+ @items[selection.to_i - 1]
300
+ else
301
+ l_index = "`"
302
+ index = @items.map { "#{l_index.succ!}" }.index(selection)
303
+ @items.find { |c| c.first == selection } or @items[index]
304
+ end
305
+
306
+ # Run or return it.
307
+ if not action.nil?
308
+ @highline = highline_context
309
+ if @shell
310
+ result = action.call(name, details)
311
+ else
312
+ result = action.call(name)
313
+ end
314
+ @nil_on_handled ? nil : result
315
+ elsif action.nil?
316
+ name
317
+ else
318
+ nil
319
+ end
320
+ ensure
321
+ # make sure the hidden items are removed, before we return
322
+ @items.slice!(@items.size - @hidden_items.size, @hidden_items.size)
323
+ end
324
+
325
+ #
326
+ # Allows Menu objects to pass as Arrays, for use with HighLine.list().
327
+ # This method returns all menu items to be displayed, complete with
328
+ # indexes.
329
+ #
330
+ def to_ary( )
331
+ case @index
332
+ when :number
333
+ @items.map { |c| "#{@items.index(c) + 1}#{@index_suffix}#{c.first}" }
334
+ when :letter
335
+ l_index = "`"
336
+ @items.map { |c| "#{l_index.succ!}#{@index_suffix}#{c.first}" }
337
+ when :none
338
+ @items.map { |c| "#{c.first}" }
339
+ else
340
+ @items.map { |c| "#{index}#{@index_suffix}#{c.first}" }
341
+ end
342
+ end
343
+
344
+ #
345
+ # Allows Menu to behave as a String, just like Question. Returns the
346
+ # _layout_ to be rendered, which is used by HighLine.say().
347
+ #
348
+ def to_str( )
349
+ case @layout
350
+ when :list
351
+ '<%= if @header.nil? then '' else "#{@header}:\n" end %>' +
352
+ "<%= list( @menu, #{@flow.inspect},
353
+ #{@list_option.inspect} ) %>" +
354
+ "<%= @prompt %>"
355
+ when :one_line
356
+ '<%= if @header.nil? then '' else "#{@header}: " end %>' +
357
+ "<%= @prompt %>" +
358
+ "(<%= list( @menu, #{@flow.inspect},
359
+ #{@list_option.inspect} ) %>)" +
360
+ "<%= @prompt[/\s*$/] %>"
361
+ when :menu_only
362
+ "<%= list( @menu, #{@flow.inspect},
363
+ #{@list_option.inspect} ) %><%= @prompt %>"
364
+ else
365
+ @layout
366
+ end
367
+ end
368
+
369
+ #
370
+ # This method will update the intelligent responses to account for
371
+ # Menu specific differences. This overrides the work done by
372
+ # Question.build_responses().
373
+ #
374
+ def update_responses( )
375
+ append_default unless default.nil?
376
+ @responses = @responses.merge(
377
+ :ambiguous_completion =>
378
+ "Ambiguous choice. " +
379
+ "Please choose one of #{options.inspect}.",
380
+ :ask_on_error =>
381
+ "? ",
382
+ :invalid_type =>
383
+ "You must enter a valid #{options}.",
384
+ :no_completion =>
385
+ "You must choose one of " +
386
+ "#{options.inspect}.",
387
+ :not_in_range =>
388
+ "Your answer isn't within the expected range " +
389
+ "(#{expected_range}).",
390
+ :mismatch =>
391
+ "Your entries didn't match.",
392
+ :not_valid =>
393
+ "Your answer isn't valid (must match " +
394
+ "#{@validate.inspect})."
395
+ )
396
+ end
397
+ end
398
+ end