tty-prompt 0.20.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/README.md +29 -4
  4. data/lib/tty/prompt.rb +3 -3
  5. data/lib/tty/prompt/multi_list.rb +33 -10
  6. data/lib/tty/prompt/version.rb +1 -1
  7. metadata +11 -175
  8. data/Rakefile +0 -8
  9. data/examples/ask.rb +0 -7
  10. data/examples/ask_blank.rb +0 -9
  11. data/examples/ask_multiline.rb +0 -7
  12. data/examples/ask_valid.rb +0 -12
  13. data/examples/collect.rb +0 -21
  14. data/examples/echo.rb +0 -11
  15. data/examples/enum_select.rb +0 -7
  16. data/examples/enum_select_disabled.rb +0 -16
  17. data/examples/enum_select_paged.rb +0 -9
  18. data/examples/enum_select_wrapped.rb +0 -15
  19. data/examples/expand.rb +0 -29
  20. data/examples/expand_auto.rb +0 -29
  21. data/examples/in.rb +0 -9
  22. data/examples/inputs.rb +0 -10
  23. data/examples/key_events.rb +0 -15
  24. data/examples/keypress.rb +0 -9
  25. data/examples/mask.rb +0 -13
  26. data/examples/multi_select.rb +0 -8
  27. data/examples/multi_select_disabled.rb +0 -17
  28. data/examples/multi_select_disabled_paged.rb +0 -22
  29. data/examples/multi_select_paged.rb +0 -9
  30. data/examples/multi_select_wrapped.rb +0 -15
  31. data/examples/multiline.rb +0 -9
  32. data/examples/pause.rb +0 -9
  33. data/examples/select.rb +0 -24
  34. data/examples/select_disabled.rb +0 -18
  35. data/examples/select_disabled_paged.rb +0 -22
  36. data/examples/select_enum.rb +0 -8
  37. data/examples/select_filtered.rb +0 -11
  38. data/examples/select_paginated.rb +0 -11
  39. data/examples/select_wrapped.rb +0 -15
  40. data/examples/slider.rb +0 -6
  41. data/examples/validation.rb +0 -9
  42. data/examples/yes_no.rb +0 -7
  43. data/spec/spec_helper.rb +0 -61
  44. data/spec/unit/ask_spec.rb +0 -173
  45. data/spec/unit/block_paginator_spec.rb +0 -84
  46. data/spec/unit/choice/eql_spec.rb +0 -22
  47. data/spec/unit/choice/from_spec.rb +0 -130
  48. data/spec/unit/choices/add_spec.rb +0 -12
  49. data/spec/unit/choices/each_spec.rb +0 -13
  50. data/spec/unit/choices/find_by_spec.rb +0 -10
  51. data/spec/unit/choices/new_spec.rb +0 -10
  52. data/spec/unit/choices/pluck_spec.rb +0 -9
  53. data/spec/unit/collect_spec.rb +0 -96
  54. data/spec/unit/converters/convert_bool_spec.rb +0 -58
  55. data/spec/unit/converters/convert_char_spec.rb +0 -11
  56. data/spec/unit/converters/convert_custom_spec.rb +0 -14
  57. data/spec/unit/converters/convert_date_spec.rb +0 -34
  58. data/spec/unit/converters/convert_file_spec.rb +0 -18
  59. data/spec/unit/converters/convert_number_spec.rb +0 -39
  60. data/spec/unit/converters/convert_path_spec.rb +0 -15
  61. data/spec/unit/converters/convert_range_spec.rb +0 -22
  62. data/spec/unit/converters/convert_regex_spec.rb +0 -12
  63. data/spec/unit/converters/convert_string_spec.rb +0 -21
  64. data/spec/unit/converters/on_error_spec.rb +0 -9
  65. data/spec/unit/distance/distance_spec.rb +0 -73
  66. data/spec/unit/enum_select_spec.rb +0 -518
  67. data/spec/unit/error_spec.rb +0 -20
  68. data/spec/unit/evaluator_spec.rb +0 -67
  69. data/spec/unit/expand_spec.rb +0 -290
  70. data/spec/unit/keypress_spec.rb +0 -66
  71. data/spec/unit/mask_spec.rb +0 -140
  72. data/spec/unit/multi_select_spec.rb +0 -741
  73. data/spec/unit/multiline_spec.rb +0 -77
  74. data/spec/unit/new_spec.rb +0 -20
  75. data/spec/unit/ok_spec.rb +0 -10
  76. data/spec/unit/paginator_spec.rb +0 -92
  77. data/spec/unit/question/checks_spec.rb +0 -97
  78. data/spec/unit/question/default_spec.rb +0 -31
  79. data/spec/unit/question/echo_spec.rb +0 -38
  80. data/spec/unit/question/in_spec.rb +0 -115
  81. data/spec/unit/question/initialize_spec.rb +0 -12
  82. data/spec/unit/question/modifier/apply_to_spec.rb +0 -24
  83. data/spec/unit/question/modifier/letter_case_spec.rb +0 -41
  84. data/spec/unit/question/modifier/whitespace_spec.rb +0 -51
  85. data/spec/unit/question/modify_spec.rb +0 -41
  86. data/spec/unit/question/required_spec.rb +0 -92
  87. data/spec/unit/question/validate_spec.rb +0 -115
  88. data/spec/unit/question/validation/call_spec.rb +0 -31
  89. data/spec/unit/question/validation/coerce_spec.rb +0 -30
  90. data/spec/unit/result_spec.rb +0 -40
  91. data/spec/unit/say_spec.rb +0 -67
  92. data/spec/unit/select_spec.rb +0 -942
  93. data/spec/unit/slider_spec.rb +0 -142
  94. data/spec/unit/statement/initialize_spec.rb +0 -15
  95. data/spec/unit/subscribe_spec.rb +0 -22
  96. data/spec/unit/suggest_spec.rb +0 -28
  97. data/spec/unit/timer_spec.rb +0 -29
  98. data/spec/unit/warn_spec.rb +0 -21
  99. data/spec/unit/yes_no_spec.rb +0 -285
  100. data/tasks/console.rake +0 -11
  101. data/tasks/coverage.rake +0 -11
  102. data/tasks/spec.rake +0 -29
  103. data/tty-prompt.gemspec +0 -40
@@ -1,142 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt, '#slider' do
4
-
5
- subject(:prompt) { TTY::TestPrompt.new }
6
-
7
- let(:symbols) { TTY::Prompt::Symbols.symbols }
8
-
9
- it "specifies ranges & step" do
10
- prompt.input << "\r"
11
- prompt.input.rewind
12
- expect(prompt.slider('What size?', min: 32, max: 54, step: 2)).to eq(44)
13
- expect(prompt.output.string).to eq([
14
- "\e[?25lWhat size? ",
15
- symbols[:line] * 6,
16
- "\e[32m#{symbols[:bullet]}\e[0m",
17
- "#{symbols[:line] * 5} 44",
18
- "\n\e[90m(Use arrow keys, press Enter to select)\e[0m",
19
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
20
- "What size? \e[32m44\e[0m\n\e[?25h"
21
- ].join)
22
- end
23
-
24
- it "specifies default value" do
25
- prompt.input << "\r"
26
- prompt.input.rewind
27
- expect(prompt.slider('What size?', min: 32, max: 54, step: 2, default: 38)).to eq(38)
28
- expect(prompt.output.string).to eq([
29
- "\e[?25lWhat size? ",
30
- symbols[:line] * 3,
31
- "\e[32m#{symbols[:bullet]}\e[0m",
32
- "#{symbols[:line] * 8} 38",
33
- "\n\e[90m(Use arrow keys, press Enter to select)\e[0m",
34
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
35
- "What size? \e[32m38\e[0m\n\e[?25h"
36
- ].join)
37
- end
38
-
39
- it "specifies range through DSL" do
40
- prompt.input << "\r"
41
- prompt.input.rewind
42
- value = prompt.slider('What size?') do |range|
43
- range.default 6
44
- range.min 0
45
- range.max 20
46
- range.step 2
47
- range.format "|:slider| %d%%"
48
- end
49
- expect(value).to eq(6)
50
- expect(prompt.output.string).to eq([
51
- "\e[?25lWhat size? ",
52
- symbols[:pipe] + symbols[:line] * 3,
53
- "\e[32m#{symbols[:bullet]}\e[0m",
54
- "#{symbols[:line] * 7 + symbols[:pipe]} 6%",
55
- "\n\e[90m(Use arrow keys, press Enter to select)\e[0m",
56
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
57
- "What size? \e[32m6\e[0m\n\e[?25h"
58
- ].join)
59
- end
60
-
61
- it "changes display colors" do
62
- prompt.input << "\r"
63
- prompt.input.rewind
64
- options = {active_color: :red, help_color: :cyan}
65
- expect(prompt.slider('What size?', options)).to eq(5)
66
- expect(prompt.output.string).to eq([
67
- "\e[?25lWhat size? ",
68
- symbols[:line] * 5,
69
- "\e[31m#{symbols[:bullet]}\e[0m",
70
- "#{symbols[:line] * 5} 5",
71
- "\n\e[36m(Use arrow keys, press Enter to select)\e[0m",
72
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
73
- "What size? \e[31m5\e[0m\n\e[?25h"
74
- ].join)
75
- end
76
-
77
- it "doesn't allow values outside of range" do
78
- prompt.input << "l\r"
79
- prompt.input.rewind
80
- prompt.on(:keypress) do |event|
81
- if event.value = 'l'
82
- prompt.trigger(:keyright)
83
- end
84
- end
85
- res = prompt.slider('What size?', min: 0, max: 10, step: 1, default: 10)
86
- expect(res).to eq(10)
87
- expect(prompt.output.string).to eq([
88
- "\e[?25lWhat size? ",
89
- symbols[:line] * 10,
90
- "\e[32m#{symbols[:bullet]}\e[0m 10",
91
- "\n\e[90m(Use arrow keys, press Enter to select)\e[0m",
92
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
93
- "What size? ",
94
- symbols[:line] * 10,
95
- "\e[32m#{symbols[:bullet]}\e[0m 10",
96
- "\e[2K\e[1G",
97
- "What size? \e[32m10\e[0m\n\e[?25h"
98
- ].join)
99
- end
100
-
101
- it "changes all display symbols" do
102
- prompt = TTY::TestPrompt.new(symbols: {
103
- bullet: 'x',
104
- line: '_'
105
- })
106
- prompt.input << "\r"
107
- prompt.input.rewind
108
-
109
- expect(prompt.slider('What size?', min: 32, max: 54, step: 2)).to eq(44)
110
-
111
- expect(prompt.output.string).to eq([
112
- "\e[?25lWhat size? ",
113
- '_' * 6,
114
- "\e[32mx\e[0m",
115
- "#{'_' * 5} 44",
116
- "\n\e[90m(Use arrow keys, press Enter to select)\e[0m",
117
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
118
- "What size? \e[32m44\e[0m\n\e[?25h"
119
- ].join)
120
- end
121
-
122
- it "changes all display symbols per instance" do
123
- prompt = TTY::TestPrompt.new
124
- prompt.input << "\r"
125
- prompt.input.rewind
126
-
127
- answer = prompt.slider('What size?', min: 32, max: 54, step: 2) do |range|
128
- range.symbols bullet: 'x', line: '_'
129
- end
130
-
131
- expect(answer).to eq(44)
132
- expect(prompt.output.string).to eq([
133
- "\e[?25lWhat size? ",
134
- '_' * 6,
135
- "\e[32mx\e[0m",
136
- "#{'_' * 5} 44",
137
- "\n\e[90m(Use arrow keys, press Enter to select)\e[0m",
138
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
139
- "What size? \e[32m44\e[0m\n\e[?25h"
140
- ].join)
141
- end
142
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt::Statement, '#new' do
4
- it "forces newline after the prompt message" do
5
- prompt = TTY::TestPrompt.new
6
- statement = described_class.new(prompt)
7
- expect(statement.newline).to eq(true)
8
- end
9
-
10
- it "displays prompt message in color" do
11
- prompt = TTY::TestPrompt.new
12
- statement = described_class.new(prompt)
13
- expect(statement.color).to eq(false)
14
- end
15
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt, '#subscribe' do
4
- it "subscribes to key events only for the current prompt" do
5
- prompt = TTY::TestPrompt.new
6
- uuid = '14c3b412-e0c5-4ff5-9cd8-25ec3f18c702'
7
- prompt.input << "3\n#{uuid}\n"
8
- prompt.input.rewind
9
- keys = []
10
-
11
- prompt.on(:keypress) do |event|
12
- keys << :enter if event.key.name == :enter
13
- end
14
-
15
- letter = prompt.enum_select('Select something', ('A'..'Z').to_a)
16
- id = prompt.ask('Request ID?')
17
-
18
- expect(letter).to eq('C')
19
- expect(id).to eq(uuid)
20
- expect(keys).to eq([:enter, :enter])
21
- end
22
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt, '#suggest' do
4
- let(:possible) { %w(status stage stash commit branch blame) }
5
-
6
- subject(:prompt) { TTY::TestPrompt.new }
7
-
8
- it 'suggests few matches' do
9
- prompt.suggest('sta', possible)
10
- expect(prompt.output.string).
11
- to eql("Did you mean one of these?\n stage\n stash\n")
12
- end
13
-
14
- it 'suggests a single match for one character' do
15
- prompt.suggest('b', possible)
16
- expect(prompt.output.string).to eql("Did you mean this?\n blame\n")
17
- end
18
-
19
- it 'suggests a single match for two characters' do
20
- prompt.suggest('co', possible)
21
- expect(prompt.output.string).to eql("Did you mean this?\n commit\n")
22
- end
23
-
24
- it 'suggests with different text and indentation' do
25
- prompt.suggest('b', possible, indent: 4, single_text: 'Perhaps you meant?')
26
- expect(prompt.output.string).to eql("Perhaps you meant?\n blame\n")
27
- end
28
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt::Timer do
4
- it "times out loop execution" do
5
- timer = TTY::Prompt::Timer.new(0.03, 0.01)
6
- yielded = []
7
-
8
- timer.while_remaining do |remaining|
9
- expect(remaining).to be_within(0.1).of(timer.duration - yielded.size * 0.01)
10
- yielded << remaining
11
- sleep(0.01)
12
- end
13
- end
14
-
15
- it "registers a tick event" do
16
- timer = TTY::Prompt::Timer.new(0.03, 0.01)
17
- yielded = []
18
-
19
- timer.on_tick do |time|
20
- yielded << time
21
- end
22
-
23
- timer.while_remaining do
24
- # busy work
25
- end
26
-
27
- expect(yielded.size).to be >= 2
28
- end
29
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt, '#warn' do
4
-
5
- subject(:prompt) { TTY::TestPrompt.new }
6
-
7
- it 'displays one message' do
8
- prompt.warn "Careful young apprentice!"
9
- expect(prompt.output.string).to eql "\e[33mCareful young apprentice!\e[0m\n"
10
- end
11
-
12
- it 'displays many messages' do
13
- prompt.warn "Careful there!", "It's dangerous!"
14
- expect(prompt.output.string).to eql "\e[33mCareful there!\e[0m\n\e[33mIt's dangerous!\e[0m\n"
15
- end
16
-
17
- it 'displays message with option' do
18
- prompt.warn "Careful young apprentice!", newline: false
19
- expect(prompt.output.string).to eql "\e[33mCareful young apprentice!\e[0m"
20
- end
21
- end
@@ -1,285 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Prompt, 'confirmation' do
4
-
5
- subject(:prompt) { TTY::TestPrompt.new }
6
-
7
- context '#yes?' do
8
- it 'agrees with question' do
9
- prompt.input << 'yes'
10
- prompt.input.rewind
11
- expect(prompt.yes?("Are you a human?")).to eq(true)
12
- expect(prompt.output.string).to eq([
13
- "Are you a human? \e[90m(Y/n)\e[0m ",
14
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m y",
15
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m ye",
16
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m yes",
17
- "\e[1A\e[2K\e[1G",
18
- "Are you a human? \e[32mYes\e[0m\n"
19
- ].join)
20
- end
21
-
22
- it 'disagrees with question' do
23
- prompt.input << 'no'
24
- prompt.input.rewind
25
- expect(prompt.yes?("Are you a human?")).to eq(false)
26
- expect(prompt.output.string).to eq([
27
- "Are you a human? \e[90m(Y/n)\e[0m ",
28
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m n",
29
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m no",
30
- "\e[1A\e[2K\e[1G",
31
- "Are you a human? \e[32mno\e[0m\n"
32
- ].join)
33
- end
34
-
35
- it 'warns about invalid entry when using defaults' do
36
- prompt.input << "test"
37
- prompt.input.rewind
38
- prompt.yes?("Are you a human?")
39
- expect(prompt.output.string).to eq([
40
- "Are you a human? \e[90m(Y/n)\e[0m ",
41
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m t",
42
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m te",
43
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m tes",
44
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m test",
45
- "\e[31m>>\e[0m Invalid input.\e[1A",
46
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m ",
47
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
48
- "Are you a human? \e[32mYes\e[0m\n"
49
- ].join)
50
- end
51
-
52
- it 'assumes default true' do
53
- prompt.input << "\r"
54
- prompt.input.rewind
55
- expect(prompt.yes?("Are you a human?")).to eq(true)
56
- expect(prompt.output.string).to eq([
57
- "Are you a human? \e[90m(Y/n)\e[0m ",
58
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m \n",
59
- "\e[1A\e[2K\e[1G",
60
- "Are you a human? \e[32mYes\e[0m\n"
61
- ].join)
62
- end
63
-
64
- it 'changes default' do
65
- prompt.input << "\n"
66
- prompt.input.rewind
67
- expect(prompt.yes?("Are you a human?", default: false)).to eq(false)
68
- expect(prompt.output.string).to eq([
69
- "Are you a human? \e[90m(y/N)\e[0m ",
70
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
71
- "\e[1A\e[2K\e[1G",
72
- "Are you a human? \e[32mNo\e[0m\n"
73
- ].join)
74
- end
75
-
76
- it "defaults suffix and converter" do
77
- prompt.input << "Nope\n"
78
- prompt.input.rewind
79
- result = prompt.yes?("Are you a human?") do |q|
80
- q.positive 'Yup'
81
- q.negative 'nope'
82
- end
83
- expect(result).to eq(false)
84
- expect(prompt.output.string).to eq([
85
- "Are you a human? \e[90m(Yup/nope)\e[0m ",
86
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m N",
87
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m No",
88
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nop",
89
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope",
90
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope\n",
91
- "\e[1A\e[2K\e[1G",
92
- "Are you a human? \e[32mnope\e[0m\n"
93
- ].join)
94
- end
95
-
96
- it "defaults positive and negative" do
97
- prompt.input << "Nope\n"
98
- prompt.input.rewind
99
- result = prompt.yes?("Are you a human?") do |q|
100
- q.suffix 'Yup/nope'
101
- end
102
- expect(result).to eq(false)
103
- expect(prompt.output.string).to eq([
104
- "Are you a human? \e[90m(Yup/nope)\e[0m ",
105
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m N",
106
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m No",
107
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nop",
108
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope",
109
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope\n",
110
- "\e[1A\e[2K\e[1G",
111
- "Are you a human? \e[32mnope\e[0m\n"
112
- ].join)
113
- end
114
-
115
- it "accepts regex conflicting characters as suffix" do
116
- prompt.input << "]\n"
117
- prompt.input.rewind
118
- result = prompt.yes?("Are you a human? [ as yes and ] as no") do |q|
119
- q.suffix "[/]"
120
- end
121
- expect(result).to eq(false)
122
- expect(prompt.output.string).to eq([
123
- "Are you a human? [ as yes and ] as no \e[90m([/])\e[0m ",
124
- "\e[2K\e[1GAre you a human? [ as yes and ] as no \e[90m([/])\e[0m ]",
125
- "\e[2K\e[1GAre you a human? [ as yes and ] as no \e[90m([/])\e[0m ]\n",
126
- "\e[1A\e[2K\e[1G",
127
- "Are you a human? [ as yes and ] as no \e[32m]\e[0m\n"
128
- ].join)
129
- end
130
-
131
- it "customizes question through options" do
132
- prompt.input << "\r"
133
- prompt.input.rewind
134
- result = prompt.yes?("Are you a human?", suffix: 'Agree/Disagree',
135
- positive: 'Agree', negative: 'Disagree')
136
- expect(result).to eq(true)
137
- expect(prompt.output.string).to eq([
138
- "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
139
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m \n",
140
- "\e[1A\e[2K\e[1G",
141
- "Are you a human? \e[32mAgree\e[0m\n"
142
- ].join)
143
- end
144
-
145
- it "customizes question through DSL" do
146
- prompt.input << "disagree\r"
147
- prompt.input.rewind
148
- conversion = proc { |input| !input.match(/^agree$/i).nil? }
149
- result = prompt.yes?("Are you a human?") do |q|
150
- q.suffix 'Agree/Disagree'
151
- q.positive 'Agree'
152
- q.negative 'Disagree'
153
- q.convert conversion
154
- end
155
- expect(result).to eq(false)
156
- expect(prompt.output.string).to eq([
157
- "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
158
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m d",
159
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m di",
160
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m dis",
161
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disa",
162
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disag",
163
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagr",
164
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagre",
165
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagree",
166
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagree\n",
167
- "\e[1A\e[2K\e[1G",
168
- "Are you a human? \e[32mDisagree\e[0m\n"
169
- ].join)
170
- end
171
- end
172
-
173
- context '#no?' do
174
- it 'agrees with question' do
175
- prompt.input << 'no'
176
- prompt.input.rewind
177
- expect(prompt.no?("Are you a human?")).to eq(true)
178
- expect(prompt.output.string).to eq([
179
- "Are you a human? \e[90m(y/N)\e[0m ",
180
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m n",
181
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m no",
182
- "\e[1A\e[2K\e[1G",
183
- "Are you a human? \e[32mNo\e[0m\n"
184
- ].join)
185
- end
186
-
187
- it 'disagrees with question' do
188
- prompt.input << 'yes'
189
- prompt.input.rewind
190
- expect(prompt.no?("Are you a human?")).to eq(false)
191
- expect(prompt.output.string).to eq([
192
- "Are you a human? \e[90m(y/N)\e[0m ",
193
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m y",
194
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m ye",
195
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m yes",
196
- "\e[1A\e[2K\e[1G",
197
- "Are you a human? \e[32myes\e[0m\n"
198
- ].join)
199
- end
200
-
201
- it 'warns about invalid entry when using defaults' do
202
- prompt.input << "test"
203
- prompt.input.rewind
204
- prompt.no?("Are you a human?")
205
- expect(prompt.output.string).to eq([
206
- "Are you a human? \e[90m(y/N)\e[0m ",
207
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m t",
208
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m te",
209
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m tes",
210
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m test",
211
- "\e[31m>>\e[0m Invalid input.\e[1A",
212
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m ",
213
- "\e[2K\e[1G\e[1A\e[2K\e[1G",
214
- "Are you a human? \e[32mNo\e[0m\n"
215
- ].join)
216
- end
217
-
218
- it 'assumes default false' do
219
- prompt.input << "\r"
220
- prompt.input.rewind
221
- expect(prompt.no?("Are you a human?")).to eq(true)
222
- expect(prompt.output.string).to eq([
223
- "Are you a human? \e[90m(y/N)\e[0m ",
224
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
225
- "\e[1A\e[2K\e[1G",
226
- "Are you a human? \e[32mNo\e[0m\n"
227
- ].join)
228
- end
229
-
230
- it 'changes default' do
231
- prompt.input << "\r"
232
- prompt.input.rewind
233
- expect(prompt.no?("Are you a human?", default: true)).to eq(false)
234
- expect(prompt.output.string).to eq([
235
- "Are you a human? \e[90m(Y/n)\e[0m ",
236
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m \n",
237
- "\e[1A\e[2K\e[1G",
238
- "Are you a human? \e[32mYes\e[0m\n"
239
- ].join)
240
- end
241
-
242
- it "defaults suffix and converter" do
243
- prompt.input << "Yup\n"
244
- prompt.input.rewind
245
- result = prompt.no?("Are you a human?") do |q|
246
- q.positive 'yup'
247
- q.negative 'Nope'
248
- end
249
- expect(result).to eq(false)
250
- expect(prompt.output.string).to eq([
251
- "Are you a human? \e[90m(yup/Nope)\e[0m ",
252
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Y",
253
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yu",
254
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yup",
255
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yup\n",
256
- "\e[1A\e[2K\e[1G",
257
- "Are you a human? \e[32myup\e[0m\n"
258
- ].join)
259
- end
260
-
261
- it "customizes question through DSL" do
262
- prompt.input << "agree\r"
263
- prompt.input.rewind
264
- conversion = proc { |input| !input.match(/^agree$/i).nil? }
265
- result = prompt.no?("Are you a human?") do |q|
266
- q.suffix 'Agree/Disagree'
267
- q.positive 'Agree'
268
- q.negative 'Disagree'
269
- q.convert conversion
270
- end
271
- expect(result).to eq(false)
272
- expect(prompt.output.string).to eq([
273
- "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
274
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m a",
275
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m ag",
276
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agr",
277
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agre",
278
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agree",
279
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agree\n",
280
- "\e[1A\e[2K\e[1G",
281
- "Are you a human? \e[32mAgree\e[0m\n"
282
- ].join)
283
- end
284
- end
285
- end