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