tty-prompt 0.19.0 → 0.23.1

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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +81 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +485 -233
  5. data/lib/tty-prompt.rb +1 -2
  6. data/lib/tty/prompt.rb +159 -147
  7. data/lib/tty/prompt/answers_collector.rb +5 -5
  8. data/lib/tty/prompt/block_paginator.rb +1 -1
  9. data/lib/tty/prompt/choice.rb +31 -13
  10. data/lib/tty/prompt/choices.rb +30 -12
  11. data/lib/tty/prompt/confirm_question.rb +42 -16
  12. data/lib/tty/prompt/const.rb +17 -0
  13. data/lib/tty/prompt/converter_dsl.rb +6 -7
  14. data/lib/tty/prompt/converter_registry.rb +31 -26
  15. data/lib/tty/prompt/converters.rb +139 -32
  16. data/lib/tty/prompt/enum_list.rb +58 -25
  17. data/lib/tty/prompt/errors.rb +31 -0
  18. data/lib/tty/prompt/evaluator.rb +2 -2
  19. data/lib/tty/prompt/expander.rb +27 -15
  20. data/lib/tty/prompt/keypress.rb +5 -3
  21. data/lib/tty/prompt/list.rb +101 -38
  22. data/lib/tty/prompt/mask_question.rb +9 -5
  23. data/lib/tty/prompt/multi_list.rb +108 -29
  24. data/lib/tty/prompt/multiline.rb +9 -7
  25. data/lib/tty/prompt/paginator.rb +1 -1
  26. data/lib/tty/prompt/question.rb +67 -36
  27. data/lib/tty/prompt/question/checks.rb +20 -2
  28. data/lib/tty/prompt/question/modifier.rb +4 -2
  29. data/lib/tty/prompt/question/validation.rb +3 -3
  30. data/lib/tty/prompt/selected_choices.rb +77 -0
  31. data/lib/tty/prompt/slider.rb +110 -23
  32. data/lib/tty/prompt/statement.rb +3 -3
  33. data/lib/tty/prompt/suggestion.rb +7 -6
  34. data/lib/tty/prompt/symbols.rb +58 -58
  35. data/lib/tty/prompt/test.rb +36 -0
  36. data/lib/tty/prompt/utils.rb +1 -3
  37. data/lib/tty/prompt/version.rb +1 -1
  38. metadata +27 -196
  39. data/Rakefile +0 -8
  40. data/examples/ask.rb +0 -7
  41. data/examples/ask_blank.rb +0 -9
  42. data/examples/ask_valid.rb +0 -12
  43. data/examples/collect.rb +0 -21
  44. data/examples/echo.rb +0 -11
  45. data/examples/enum_select.rb +0 -7
  46. data/examples/enum_select_disabled.rb +0 -16
  47. data/examples/enum_select_paged.rb +0 -9
  48. data/examples/enum_select_wrapped.rb +0 -15
  49. data/examples/expand.rb +0 -29
  50. data/examples/expand_auto.rb +0 -29
  51. data/examples/in.rb +0 -9
  52. data/examples/inputs.rb +0 -10
  53. data/examples/key_events.rb +0 -15
  54. data/examples/keypress.rb +0 -9
  55. data/examples/mask.rb +0 -13
  56. data/examples/multi_select.rb +0 -8
  57. data/examples/multi_select_disabled.rb +0 -17
  58. data/examples/multi_select_disabled_paged.rb +0 -22
  59. data/examples/multi_select_paged.rb +0 -9
  60. data/examples/multi_select_wrapped.rb +0 -15
  61. data/examples/multiline.rb +0 -9
  62. data/examples/pause.rb +0 -9
  63. data/examples/select.rb +0 -24
  64. data/examples/select_disabled.rb +0 -18
  65. data/examples/select_disabled_paged.rb +0 -22
  66. data/examples/select_enum.rb +0 -8
  67. data/examples/select_filtered.rb +0 -11
  68. data/examples/select_paginated.rb +0 -11
  69. data/examples/select_wrapped.rb +0 -15
  70. data/examples/slider.rb +0 -6
  71. data/examples/validation.rb +0 -9
  72. data/examples/yes_no.rb +0 -7
  73. data/lib/tty/prompt/messages.rb +0 -49
  74. data/lib/tty/test_prompt.rb +0 -20
  75. data/spec/spec_helper.rb +0 -61
  76. data/spec/unit/ask_spec.rb +0 -173
  77. data/spec/unit/block_paginator_spec.rb +0 -84
  78. data/spec/unit/choice/eql_spec.rb +0 -22
  79. data/spec/unit/choice/from_spec.rb +0 -112
  80. data/spec/unit/choices/add_spec.rb +0 -12
  81. data/spec/unit/choices/each_spec.rb +0 -13
  82. data/spec/unit/choices/find_by_spec.rb +0 -10
  83. data/spec/unit/choices/new_spec.rb +0 -10
  84. data/spec/unit/choices/pluck_spec.rb +0 -9
  85. data/spec/unit/collect_spec.rb +0 -96
  86. data/spec/unit/converters/convert_bool_spec.rb +0 -58
  87. data/spec/unit/converters/convert_char_spec.rb +0 -11
  88. data/spec/unit/converters/convert_custom_spec.rb +0 -14
  89. data/spec/unit/converters/convert_date_spec.rb +0 -34
  90. data/spec/unit/converters/convert_file_spec.rb +0 -18
  91. data/spec/unit/converters/convert_number_spec.rb +0 -39
  92. data/spec/unit/converters/convert_path_spec.rb +0 -15
  93. data/spec/unit/converters/convert_range_spec.rb +0 -22
  94. data/spec/unit/converters/convert_regex_spec.rb +0 -12
  95. data/spec/unit/converters/convert_string_spec.rb +0 -21
  96. data/spec/unit/converters/on_error_spec.rb +0 -9
  97. data/spec/unit/distance/distance_spec.rb +0 -73
  98. data/spec/unit/enum_select_spec.rb +0 -518
  99. data/spec/unit/error_spec.rb +0 -20
  100. data/spec/unit/evaluator_spec.rb +0 -67
  101. data/spec/unit/expand_spec.rb +0 -290
  102. data/spec/unit/keypress_spec.rb +0 -66
  103. data/spec/unit/mask_spec.rb +0 -140
  104. data/spec/unit/multi_select_spec.rb +0 -741
  105. data/spec/unit/multiline_spec.rb +0 -77
  106. data/spec/unit/new_spec.rb +0 -20
  107. data/spec/unit/ok_spec.rb +0 -10
  108. data/spec/unit/paginator_spec.rb +0 -92
  109. data/spec/unit/question/checks_spec.rb +0 -97
  110. data/spec/unit/question/default_spec.rb +0 -31
  111. data/spec/unit/question/echo_spec.rb +0 -38
  112. data/spec/unit/question/in_spec.rb +0 -115
  113. data/spec/unit/question/initialize_spec.rb +0 -12
  114. data/spec/unit/question/modifier/apply_to_spec.rb +0 -24
  115. data/spec/unit/question/modifier/letter_case_spec.rb +0 -41
  116. data/spec/unit/question/modifier/whitespace_spec.rb +0 -51
  117. data/spec/unit/question/modify_spec.rb +0 -41
  118. data/spec/unit/question/required_spec.rb +0 -92
  119. data/spec/unit/question/validate_spec.rb +0 -115
  120. data/spec/unit/question/validation/call_spec.rb +0 -31
  121. data/spec/unit/question/validation/coerce_spec.rb +0 -30
  122. data/spec/unit/result_spec.rb +0 -40
  123. data/spec/unit/say_spec.rb +0 -67
  124. data/spec/unit/select_spec.rb +0 -942
  125. data/spec/unit/slider_spec.rb +0 -142
  126. data/spec/unit/statement/initialize_spec.rb +0 -15
  127. data/spec/unit/subscribe_spec.rb +0 -22
  128. data/spec/unit/suggest_spec.rb +0 -28
  129. data/spec/unit/timer_spec.rb +0 -29
  130. data/spec/unit/warn_spec.rb +0 -21
  131. data/spec/unit/yes_no_spec.rb +0 -251
  132. data/tasks/console.rake +0 -11
  133. data/tasks/coverage.rake +0 -11
  134. data/tasks/spec.rake +0 -29
  135. data/tty-prompt.gemspec +0 -31
@@ -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,251 +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 'assumes default true' do
36
- prompt.input << "\r"
37
- prompt.input.rewind
38
- expect(prompt.yes?("Are you a human?")).to eq(true)
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 \n",
42
- "\e[1A\e[2K\e[1G",
43
- "Are you a human? \e[32mYes\e[0m\n"
44
- ].join)
45
- end
46
-
47
- it 'changes default' do
48
- prompt.input << "\n"
49
- prompt.input.rewind
50
- expect(prompt.yes?("Are you a human?", default: false)).to eq(false)
51
- expect(prompt.output.string).to eq([
52
- "Are you a human? \e[90m(y/N)\e[0m ",
53
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
54
- "\e[1A\e[2K\e[1G",
55
- "Are you a human? \e[32mNo\e[0m\n"
56
- ].join)
57
- end
58
-
59
- it "defaults suffix and converter" do
60
- prompt.input << "Nope\n"
61
- prompt.input.rewind
62
- result = prompt.yes?("Are you a human?") do |q|
63
- q.positive 'Yup'
64
- q.negative 'nope'
65
- end
66
- expect(result).to eq(false)
67
- expect(prompt.output.string).to eq([
68
- "Are you a human? \e[90m(Yup/nope)\e[0m ",
69
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m N",
70
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m No",
71
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nop",
72
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope",
73
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope\n",
74
- "\e[1A\e[2K\e[1G",
75
- "Are you a human? \e[32mnope\e[0m\n"
76
- ].join)
77
- end
78
-
79
- it "defaults positive and negative" do
80
- prompt.input << "Nope\n"
81
- prompt.input.rewind
82
- result = prompt.yes?("Are you a human?") do |q|
83
- q.suffix 'Yup/nope'
84
- end
85
- expect(result).to eq(false)
86
- expect(prompt.output.string).to eq([
87
- "Are you a human? \e[90m(Yup/nope)\e[0m ",
88
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m N",
89
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m No",
90
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nop",
91
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope",
92
- "\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope\n",
93
- "\e[1A\e[2K\e[1G",
94
- "Are you a human? \e[32mnope\e[0m\n"
95
- ].join)
96
- end
97
-
98
- it "accepts regex conflicting characters as suffix" do
99
- prompt.input << "]\n"
100
- prompt.input.rewind
101
- result = prompt.yes?("Are you a human? [ as yes and ] as no") do |q|
102
- q.suffix "[/]"
103
- end
104
- expect(result).to eq(false)
105
- expect(prompt.output.string).to eq([
106
- "Are you a human? [ as yes and ] as no \e[90m([/])\e[0m ",
107
- "\e[2K\e[1GAre you a human? [ as yes and ] as no \e[90m([/])\e[0m ]",
108
- "\e[2K\e[1GAre you a human? [ as yes and ] as no \e[90m([/])\e[0m ]\n",
109
- "\e[1A\e[2K\e[1G",
110
- "Are you a human? [ as yes and ] as no \e[32m]\e[0m\n"
111
- ].join)
112
- end
113
-
114
- it "customizes question through options" do
115
- prompt.input << "\r"
116
- prompt.input.rewind
117
- result = prompt.yes?("Are you a human?", suffix: 'Agree/Disagree',
118
- positive: 'Agree', negative: 'Disagree')
119
- expect(result).to eq(true)
120
- expect(prompt.output.string).to eq([
121
- "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
122
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m \n",
123
- "\e[1A\e[2K\e[1G",
124
- "Are you a human? \e[32mAgree\e[0m\n"
125
- ].join)
126
- end
127
-
128
- it "customizes question through DSL" do
129
- prompt.input << "disagree\r"
130
- prompt.input.rewind
131
- conversion = proc { |input| !input.match(/^agree$/i).nil? }
132
- result = prompt.yes?("Are you a human?") do |q|
133
- q.suffix 'Agree/Disagree'
134
- q.positive 'Agree'
135
- q.negative 'Disagree'
136
- q.convert conversion
137
- end
138
- expect(result).to eq(false)
139
- expect(prompt.output.string).to eq([
140
- "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
141
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m d",
142
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m di",
143
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m dis",
144
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disa",
145
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disag",
146
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagr",
147
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagre",
148
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagree",
149
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagree\n",
150
- "\e[1A\e[2K\e[1G",
151
- "Are you a human? \e[32mDisagree\e[0m\n"
152
- ].join)
153
- end
154
- end
155
-
156
- context '#no?' do
157
- it 'agrees with question' do
158
- prompt.input << 'no'
159
- prompt.input.rewind
160
- expect(prompt.no?("Are you a human?")).to eq(true)
161
- expect(prompt.output.string).to eq([
162
- "Are you a human? \e[90m(y/N)\e[0m ",
163
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m n",
164
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m no",
165
- "\e[1A\e[2K\e[1G",
166
- "Are you a human? \e[32mNo\e[0m\n"
167
- ].join)
168
- end
169
-
170
- it 'disagrees with question' do
171
- prompt.input << 'yes'
172
- prompt.input.rewind
173
- expect(prompt.no?("Are you a human?")).to eq(false)
174
- expect(prompt.output.string).to eq([
175
- "Are you a human? \e[90m(y/N)\e[0m ",
176
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m y",
177
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m ye",
178
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m yes",
179
- "\e[1A\e[2K\e[1G",
180
- "Are you a human? \e[32myes\e[0m\n"
181
- ].join)
182
- end
183
-
184
- it 'assumes default false' do
185
- prompt.input << "\r"
186
- prompt.input.rewind
187
- expect(prompt.no?("Are you a human?")).to eq(true)
188
- expect(prompt.output.string).to eq([
189
- "Are you a human? \e[90m(y/N)\e[0m ",
190
- "\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
191
- "\e[1A\e[2K\e[1G",
192
- "Are you a human? \e[32mNo\e[0m\n"
193
- ].join)
194
- end
195
-
196
- it 'changes default' do
197
- prompt.input << "\r"
198
- prompt.input.rewind
199
- expect(prompt.no?("Are you a human?", default: true)).to eq(false)
200
- expect(prompt.output.string).to eq([
201
- "Are you a human? \e[90m(Y/n)\e[0m ",
202
- "\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m \n",
203
- "\e[1A\e[2K\e[1G",
204
- "Are you a human? \e[32mYes\e[0m\n"
205
- ].join)
206
- end
207
-
208
- it "defaults suffix and converter" do
209
- prompt.input << "Yup\n"
210
- prompt.input.rewind
211
- result = prompt.no?("Are you a human?") do |q|
212
- q.positive 'yup'
213
- q.negative 'Nope'
214
- end
215
- expect(result).to eq(false)
216
- expect(prompt.output.string).to eq([
217
- "Are you a human? \e[90m(yup/Nope)\e[0m ",
218
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Y",
219
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yu",
220
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yup",
221
- "\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yup\n",
222
- "\e[1A\e[2K\e[1G",
223
- "Are you a human? \e[32myup\e[0m\n"
224
- ].join)
225
- end
226
-
227
- it "customizes question through DSL" do
228
- prompt.input << "agree\r"
229
- prompt.input.rewind
230
- conversion = proc { |input| !input.match(/^agree$/i).nil? }
231
- result = prompt.no?("Are you a human?") do |q|
232
- q.suffix 'Agree/Disagree'
233
- q.positive 'Agree'
234
- q.negative 'Disagree'
235
- q.convert conversion
236
- end
237
- expect(result).to eq(false)
238
- expect(prompt.output.string).to eq([
239
- "Are you a human? \e[90m(Agree/Disagree)\e[0m ",
240
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m a",
241
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m ag",
242
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agr",
243
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agre",
244
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agree",
245
- "\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agree\n",
246
- "\e[1A\e[2K\e[1G",
247
- "Are you a human? \e[32mAgree\e[0m\n"
248
- ].join)
249
- end
250
- end
251
- end