highline 1.7.10 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
data/test/tc_import.rb DELETED
@@ -1,52 +0,0 @@
1
- # tc_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 "test/unit"
9
-
10
- require "highline/import"
11
- require "stringio"
12
-
13
- class TestImport < Test::Unit::TestCase
14
- def test_import
15
- assert_respond_to(self, :agree)
16
- assert_respond_to(self, :ask)
17
- assert_respond_to(self, :choose)
18
- assert_respond_to(self, :say)
19
- end
20
-
21
- def test_or_ask
22
- old_terminal = $terminal
23
-
24
- input = StringIO.new
25
- output = StringIO.new
26
- $terminal = HighLine.new(input, output)
27
-
28
- input << "10\n"
29
- input.rewind
30
-
31
- assert_equal(10, nil.or_ask("How much? ", Integer))
32
-
33
- input.rewind
34
-
35
- assert_equal(20, "20".or_ask("How much? ", Integer))
36
- assert_equal(20, 20.or_ask("How much? ", Integer))
37
-
38
- assert_equal(10, 20.or_ask("How much? ", Integer) { |q| q.in = 1..10 })
39
- ensure
40
- $terminal = old_terminal
41
- end
42
-
43
- def test_redirection
44
- old_terminal = $terminal
45
-
46
- $terminal = HighLine.new(nil, (output = StringIO.new))
47
- say("Testing...")
48
- assert_equal("Testing...\n", output.string)
49
- ensure
50
- $terminal = old_terminal
51
- end
52
- end
data/test/tc_menu.rb DELETED
@@ -1,439 +0,0 @@
1
- # coding: utf-8
2
- # tc_menu.rb
3
- #
4
- # Created by Gregory Thomas Brown on 2005-05-10.
5
- # Copyright 2005. All rights reserved.
6
- #
7
- # This is Free Software. See LICENSE and COPYING for details.
8
-
9
- require "test/unit"
10
-
11
- require "highline"
12
- require "stringio"
13
-
14
- class TestMenu < Test::Unit::TestCase
15
- def setup
16
- @input = StringIO.new
17
- @output = StringIO.new
18
- @terminal = HighLine.new(@input, @output)
19
- end
20
-
21
- def test_choices
22
- @input << "2\n"
23
- @input.rewind
24
-
25
- output = @terminal.choose do |menu|
26
- menu.choices("Sample1", "Sample2", "Sample3")
27
- end
28
- assert_equal("Sample2", output)
29
- end
30
-
31
- def test_flow
32
- @input << "Sample1\n"
33
- @input.rewind
34
-
35
- @terminal.choose do |menu|
36
- # Default: menu.flow = :rows
37
-
38
- menu.choice "Sample1"
39
- menu.choice "Sample2"
40
- menu.choice "Sample3"
41
- end
42
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
43
-
44
- @output.truncate(@output.rewind)
45
- @input.rewind
46
-
47
- @terminal.choose do |menu|
48
- menu.flow = :columns_across
49
-
50
- menu.choice "Sample1"
51
- menu.choice "Sample2"
52
- menu.choice "Sample3"
53
- end
54
- assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string)
55
-
56
- @output.truncate(@output.rewind)
57
- @input.rewind
58
-
59
- @terminal.choose do |menu|
60
- menu.flow = :inline
61
- menu.index = :none
62
-
63
- menu.choice "Sample1"
64
- menu.choice "Sample2"
65
- menu.choice "Sample3"
66
- end
67
- assert_equal("Sample1, Sample2 or Sample3? ", @output.string)
68
- end
69
-
70
- def test_unicode_flow
71
- @input << "1\n"
72
- @input.rewind
73
-
74
- @terminal.choose do |menu|
75
- # Default: menu.flow = :rows
76
- menu.choice "Unicode right single quotation mark: ’"
77
- end
78
- assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, { :undef => :replace }), @output.string)
79
- end
80
-
81
- def test_help
82
- @input << "help\nhelp load\nhelp rules\nhelp missing\n"
83
- @input.rewind
84
-
85
- 4.times do
86
- @terminal.choose do |menu|
87
- menu.shell = true
88
-
89
- menu.choice(:load, "Load a file.")
90
- menu.choice(:save, "Save data in file.")
91
- menu.choice(:quit, "Exit program.")
92
-
93
- menu.help("rules", "The rules of this system are as follows...")
94
- end
95
- end
96
- assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " +
97
- "This command will display helpful messages about " +
98
- "functionality, like this one. To see the help for a " +
99
- "specific topic enter:\n" +
100
- "\thelp [TOPIC]\n" +
101
- "Try asking for help on any of the following:\n" +
102
- "\nload quit rules save \n" +
103
- "1. load\n2. save\n3. quit\n4. help\n? " +
104
- "= load\n\n" +
105
- "Load a file.\n" +
106
- "1. load\n2. save\n3. quit\n4. help\n? " +
107
- "= rules\n\n" +
108
- "The rules of this system are as follows...\n" +
109
- "1. load\n2. save\n3. quit\n4. help\n? " +
110
- "= missing\n\n" +
111
- "There's no help for that topic.\n", @output.string )
112
- end
113
-
114
- def test_index
115
- @input << "Sample1\n"
116
- @input.rewind
117
-
118
- @terminal.choose do |menu|
119
- # Default: menu.index = :number
120
-
121
- menu.choice "Sample1"
122
- menu.choice "Sample2"
123
- menu.choice "Sample3"
124
- end
125
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
126
-
127
- @output.truncate(@output.rewind)
128
- @input.rewind
129
-
130
- @terminal.choose do |menu|
131
- menu.index = :letter
132
- menu.index_suffix = ") "
133
-
134
- menu.choice "Sample1"
135
- menu.choice "Sample2"
136
- menu.choice "Sample3"
137
- end
138
- assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string)
139
-
140
- @output.truncate(@output.rewind)
141
- @input.rewind
142
-
143
- @terminal.choose do |menu|
144
- menu.index = :none
145
-
146
- menu.choice "Sample1"
147
- menu.choice "Sample2"
148
- menu.choice "Sample3"
149
- end
150
- assert_equal("Sample1\nSample2\nSample3\n? ", @output.string)
151
-
152
- @output.truncate(@output.rewind)
153
- @input.rewind
154
-
155
- @terminal.choose do |menu|
156
- menu.index = "*"
157
-
158
- menu.choice "Sample1"
159
- menu.choice "Sample2"
160
- menu.choice "Sample3"
161
- end
162
- assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string)
163
- end
164
-
165
- def test_layouts
166
- @input << "save\n"
167
- @input.rewind
168
-
169
- @terminal.choose(:load, :save, :quit) # Default: layout = :list
170
- assert_equal("1. load\n2. save\n3. quit\n? ", @output.string)
171
-
172
- @input.rewind
173
- @output.truncate(@output.rewind)
174
-
175
- @terminal.choose(:load, :save, :quit) do |menu|
176
- menu.header = "File Menu"
177
- end
178
- assert_equal( "File Menu:\n" +
179
- "1. load\n2. save\n3. quit\n? ", @output.string )
180
-
181
- @input.rewind
182
- @output.truncate(@output.rewind)
183
-
184
- @terminal.choose(:load, :save, :quit) do |menu|
185
- menu.layout = :one_line
186
- menu.header = "File Menu"
187
- menu.prompt = "Operation? "
188
- end
189
- assert_equal( "File Menu: Operation? " +
190
- "(load, save or quit) ", @output.string )
191
-
192
- @input.rewind
193
- @output.truncate(@output.rewind)
194
-
195
- @terminal.choose(:load, :save, :quit) do |menu|
196
- menu.layout = :menu_only
197
- end
198
- assert_equal("load, save or quit? ", @output.string)
199
-
200
- @input.rewind
201
- @output.truncate(@output.rewind)
202
-
203
- @terminal.choose(:load, :save, :quit) do |menu|
204
- menu.layout = '<%= list(@menu) %>File Menu: '
205
- end
206
- assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string)
207
- end
208
-
209
- def test_list_option
210
- @input << "l\n"
211
- @input.rewind
212
-
213
- @terminal.choose(:load, :save, :quit) do |menu|
214
- menu.layout = :menu_only
215
- menu.list_option = ", or "
216
- end
217
- assert_equal("load, save, or quit? ", @output.string)
218
- end
219
-
220
- def test_nil_on_handled
221
- @input << "3\n3\n2\n"
222
- @input.rewind
223
-
224
- # Shows that by default proc results are returned.
225
- output = @terminal.choose do |menu|
226
- menu.choice "Sample1" do "output1" end
227
- menu.choice "Sample2" do "output2" end
228
- menu.choice "Sample3" do "output3" end
229
- end
230
- assert_equal("output3", output)
231
-
232
- #
233
- # Shows that they can be replaced with +nil+ by setting
234
- # _nil_on_handled to +true+.
235
- #
236
- output = @terminal.choose do |menu|
237
- menu.nil_on_handled = true
238
- menu.choice "Sample1" do "output1" end
239
- menu.choice "Sample2" do "output2" end
240
- menu.choice "Sample3" do "output3" end
241
- end
242
- assert_equal(nil, output)
243
-
244
- # Shows that a menu item without a proc will be returned no matter what.
245
- output = @terminal.choose do |menu|
246
- menu.choice "Sample1"
247
- menu.choice "Sample2"
248
- menu.choice "Sample3"
249
- end
250
- assert_equal("Sample2", output)
251
- end
252
-
253
- def test_passed_command
254
- @input << "q\n"
255
- @input.rewind
256
-
257
- selected = nil
258
- @terminal.choose do |menu|
259
- menu.choices(:load, :save, :quit) { |command| selected = command }
260
- end
261
- assert_equal(:quit, selected)
262
- end
263
-
264
- def test_question_options
265
- @input << "save\n"
266
- @input.rewind
267
-
268
- answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
269
- menu.case = :capitalize
270
- end
271
- assert_equal(:Save, answer)
272
-
273
- @input.rewind
274
-
275
- answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
276
- menu.case = :capitalize
277
- menu.character = :getc
278
- end
279
- assert_equal(:Save, answer)
280
- assert_equal(?a, @input.getc)
281
- end
282
-
283
- def test_select_by
284
- @input << "Sample1\n2\n"
285
- @input.rewind
286
-
287
- selected = @terminal.choose do |menu|
288
- menu.choice "Sample1"
289
- menu.choice "Sample2"
290
- menu.choice "Sample3"
291
- end
292
- assert_equal("Sample1", selected)
293
-
294
- @input.rewind
295
-
296
- selected = @terminal.choose do |menu|
297
- menu.select_by = :index
298
-
299
- menu.choice "Sample1"
300
- menu.choice "Sample2"
301
- menu.choice "Sample3"
302
- end
303
- assert_equal("Sample2", selected)
304
-
305
- @input.rewind
306
-
307
- selected = @terminal.choose do |menu|
308
- menu.select_by = :name
309
-
310
- menu.choice "Sample1"
311
- menu.choice "Sample2"
312
- menu.choice "Sample3"
313
- end
314
- assert_equal("Sample1", selected)
315
- end
316
-
317
- def test_hidden
318
- @input << "Hidden\n4\n"
319
- @input.rewind
320
-
321
- selected = @terminal.choose do |menu|
322
- menu.choice "Sample1"
323
- menu.choice "Sample2"
324
- menu.choice "Sample3"
325
- menu.hidden "Hidden!"
326
- end
327
- assert_equal("Hidden!", selected)
328
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
329
-
330
- @input.rewind
331
-
332
- selected = @terminal.choose do |menu|
333
- menu.select_by = :index
334
-
335
- menu.choice "Sample1"
336
- menu.choice "Sample2"
337
- menu.choice "Sample3"
338
- menu.hidden "Hidden!"
339
- end
340
- assert_equal("Hidden!", selected)
341
-
342
- @input.rewind
343
-
344
- selected = @terminal.choose do |menu|
345
- menu.select_by = :name
346
-
347
- menu.choice "Sample1"
348
- menu.choice "Sample2"
349
- menu.choice "Sample3"
350
- menu.hidden "Hidden!"
351
- end
352
- assert_equal("Hidden!", selected)
353
-
354
- @input.rewind
355
- end
356
-
357
- def test_select_by_letter
358
- @input << "b\n"
359
- @input.rewind
360
-
361
- selected = @terminal.choose do |menu|
362
- menu.index = :letter
363
- menu.choice :save
364
- menu.choice :load
365
- menu.choice :quit
366
- end
367
- assert_equal(:load, selected)
368
- end
369
-
370
- def test_shell
371
- @input << "save --some-option my_file.txt\n"
372
- @input.rewind
373
-
374
- selected = nil
375
- options = nil
376
- answer = @terminal.choose do |menu|
377
- menu.choices(:load, :quit)
378
- menu.choice(:save) do |command, details|
379
- selected = command
380
- options = details
381
-
382
- "Saved!"
383
- end
384
- menu.shell = true
385
- end
386
- assert_equal("Saved!", answer)
387
- assert_equal(:save, selected)
388
- assert_equal("--some-option my_file.txt", options)
389
- end
390
-
391
- def test_simple_menu_shortcut
392
- @input << "3\n"
393
- @input.rewind
394
-
395
- selected = @terminal.choose(:save, :load, :quit)
396
- assert_equal(:quit, selected)
397
- end
398
-
399
- def test_symbols
400
- @input << "3\n"
401
- @input.rewind
402
-
403
- selected = @terminal.choose do |menu|
404
- menu.choices(:save, :load, :quit)
405
- end
406
- assert_equal(:quit, selected)
407
- end
408
-
409
- def test_paged_print_infinite_loop_bug
410
- @terminal.page_at = 5
411
- # Will page twice, so start with two new lines
412
- @input << "\n\n3\n"
413
- @input.rewind
414
-
415
- # Sadly this goes into an infinite loop without the fix to page_print
416
- selected = @terminal.choose(* 1..10)
417
- assert_equal(selected, 3)
418
- end
419
-
420
-
421
- def test_cancel_paging
422
- # Tests that paging can be cancelled halfway through
423
- @terminal.page_at = 5
424
- # Will page twice, so stop after first page and make choice 3
425
- @input << "q3\n"
426
- @input.rewind
427
-
428
- selected = @terminal.choose(* 1..10)
429
- assert_equal(selected, 3)
430
-
431
- # Make sure paging message appeared
432
- assert( @output.string.index('press enter/return to continue or q to stop'),
433
- "Paging message did not appear." )
434
-
435
- # Make sure it only appeared once
436
- assert( @output.string !~ /q to stop.*q to stop/m,
437
- "Paging message appeared more than once." )
438
- end
439
- end
data/test/tc_simulator.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'test/unit'
2
- require 'highline/import'
3
- require 'highline/simulate'
4
-
5
- class SimulatorTest < Test::Unit::TestCase
6
- def setup
7
- input = StringIO.new
8
- output = StringIO.new
9
- $terminal = HighLine.new(input, output)
10
- end
11
-
12
- def test_simulator
13
- HighLine::Simulate.with('Bugs Bunny', '18') do
14
- name = ask('What is your name?')
15
-
16
- assert_equal 'Bugs Bunny', name
17
-
18
- age = ask('What is your age?')
19
-
20
- assert_equal '18', age
21
- end
22
- end
23
-
24
- def test_simulate_with_echo_and_frozen_strings
25
- HighLine::Simulate.with('the password'.freeze) do
26
- password = ask('What is your password?') do |q|
27
- q.echo = '*'
28
- end
29
-
30
- assert_equal 'the password', password
31
- end
32
- end
33
- end
@@ -1,33 +0,0 @@
1
- # tc_string_extension.rb
2
- #
3
- # Created by Richard LeBer 2011-06-27
4
- #
5
- # This is Free Software. See LICENSE and COPYING for details.
6
-
7
- require "test/unit"
8
-
9
- require "highline"
10
- require "stringio"
11
- require "string_methods"
12
-
13
- class TestStringExtension < Test::Unit::TestCase
14
- def setup
15
- HighLine.colorize_strings
16
- @string = "string"
17
- end
18
-
19
- include StringMethods
20
-
21
- def test_Highline_String_is_yaml_serializable
22
- require 'yaml'
23
- unless Gem::Version.new(YAML::VERSION) < Gem::Version.new("2.0.2")
24
- highline_string = HighLine::String.new("Yaml didn't messed with HighLine::String")
25
- yaml_highline_string = highline_string.to_yaml
26
- yaml_loaded_string = YAML.load(yaml_highline_string)
27
-
28
- assert_equal "Yaml didn't messed with HighLine::String", yaml_loaded_string
29
- assert_equal highline_string, yaml_loaded_string
30
- assert_instance_of HighLine::String, yaml_loaded_string
31
- end
32
- end
33
- end
@@ -1,38 +0,0 @@
1
- # tc_highline_string.rb
2
- #
3
- # Created by Richard LeBer 2011-06-27
4
- #
5
- # This is Free Software. See LICENSE and COPYING for details.
6
-
7
- require "test/unit"
8
-
9
- require "highline"
10
- require "stringio"
11
- require "string_methods"
12
-
13
- class TestHighLineString < Test::Unit::TestCase
14
- def setup
15
- @string = HighLine::String.new("string")
16
- end
17
-
18
- def test_string_class
19
- # Basic constructor
20
- assert_equal HighLine::String, @string.class
21
- assert_equal "string", @string
22
-
23
- # Alternative constructor method
24
- new_string = HighLine::String("string")
25
- assert_equal HighLine::String, new_string.class
26
- assert_equal @string, new_string
27
-
28
- # String methods work
29
- assert_equal 6, @string.size
30
- assert_equal "STRING", @string.upcase
31
- end
32
-
33
- include StringMethods
34
-
35
- def test_string_class_is_unchanged
36
- assert_raise(::NoMethodError) { "string".color(:blue) }
37
- end
38
- end