1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
# frozen_string_literal: true
RSpec.describe TTY::Prompt, "#slider" do
subject(:prompt) { TTY::Prompt::Test.new }
let(:symbols) { TTY::Prompt::Symbols.symbols }
let(:left_right) { "#{symbols[:arrow_left]}/#{symbols[:arrow_right]}" }
def output_helper(prompt, choices, active, init: false, hint: false)
index = choices.index(active)
out = []
out << "\e[?25l" if init
out << prompt << " "
out << symbols[:line] * index
out << "\e[32m#{symbols[:bullet]}\e[0m"
out << symbols[:line] * (choices.size - index - 1)
out << " " << active
out << "\n\e[90m(#{hint})\e[0m" if hint
out << "\e[2K\e[1G"
out << "\e[1A\e[2K\e[1G" if hint
out.join
end
def exit_message(prompt, choice)
"#{prompt} \e[32m#{choice}\e[0m\n\e[?25h"
end
it "specifies ranges & step" do
choices = (32..54).step(2).to_a
prompt.input << "\r"
prompt.input.rewind
expect(prompt.slider("What size?", min: 32, max: 54, step: 2)).to eq(44)
expect(prompt.output.string).to eq([
output_helper("What size?", choices, 44, init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
exit_message("What size?", 44)
].join)
end
it "specifies default value" do
choices = (32..54).step(2).to_a
prompt.input << "\r"
prompt.input.rewind
expect(prompt.slider("What size?", min: 32, max: 54, step: 2, default: 38)).to eq(38)
expect(prompt.output.string).to eq([
output_helper("What size?", choices, 38, init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
exit_message("What size?", 38)
].join)
end
it "specifies range through DSL" do
prompt.input << "\r"
prompt.input.rewind
value = prompt.slider("What size?") do |range|
range.help "(Move with arrows)"
range.default 6
range.min 0
range.max 20
range.step 2
range.format "|:slider| %d%%"
end
expect(value).to eq(6)
expect(prompt.output.string).to eq([
"\e[?25lWhat size? ",
symbols[:pipe] + symbols[:line] * 3,
"\e[32m#{symbols[:bullet]}\e[0m",
"#{symbols[:line] * 7 + symbols[:pipe]} 6%",
"\n\e[90m(Move with arrows)\e[0m",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"What size? \e[32m6\e[0m\n\e[?25h"
].join)
end
it "formats via proc" do
prompt.input << "\r"
prompt.input.rewind
value = prompt.slider("What size?") do |range|
range.default 6
range.max 20
range.step 2
range.format ->(slider, value) { "|#{slider}| %d%%" % value }
end
expect(value).to eq(6)
expect(prompt.output.string).to eq([
"\e[?25lWhat size? ",
symbols[:pipe] + symbols[:line] * 3,
"\e[32m#{symbols[:bullet]}\e[0m",
"#{symbols[:line] * 7 + symbols[:pipe]} 6%",
"\n\e[90m(Use #{left_right} arrow keys, press Enter to select)\e[0m",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"What size? \e[32m6\e[0m\n\e[?25h"
].join)
end
it "changes display colors" do
prompt.input << "\r"
prompt.input.rewind
options = {active_color: :red, help_color: :cyan}
expect(prompt.slider("What size?", **options)).to eq(5)
expect(prompt.output.string).to eq([
"\e[?25lWhat size? ",
symbols[:line] * 5,
"\e[31m#{symbols[:bullet]}\e[0m",
"#{symbols[:line] * 5} 5",
"\n\e[36m(Use #{left_right} arrow keys, press Enter to select)\e[0m",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"What size? \e[31m5\e[0m\n\e[?25h"
].join)
end
it "doesn't allow values outside of range" do
choices = (0..10).to_a
prompt.input << "l\r"
prompt.input.rewind
prompt.on(:keypress) do |event|
if event.value = "l"
prompt.trigger(:keyright)
end
end
res = prompt.slider("What size?", min: 0, max: 10, step: 1, default: 10)
expect(res).to eq(10)
expect(prompt.output.string).to eq([
output_helper("What size?", choices, 10, init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What size?", choices, 10),
exit_message("What size?", 10)
].join)
end
it "changes all display symbols" do
prompt = TTY::Prompt::Test.new(symbols: {
bullet: "x",
line: "_"
})
prompt.input << "\r"
prompt.input.rewind
expect(prompt.slider("What size?", min: 32, max: 54, step: 2)).to eq(44)
expect(prompt.output.string).to eq([
"\e[?25lWhat size? ",
"_" * 6,
"\e[32mx\e[0m",
"#{'_' * 5} 44",
"\n\e[90m(Use #{left_right} arrow keys, press Enter to select)\e[0m",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"What size? \e[32m44\e[0m\n\e[?25h"
].join)
end
it "changes all display symbols per instance" do
prompt.input << "\r"
prompt.input.rewind
answer = prompt.slider("What size?", min: 32, max: 54, step: 2) do |range|
range.symbols bullet: "x", line: "_"
end
expect(answer).to eq(44)
expect(prompt.output.string).to eq([
"\e[?25lWhat size? ",
"_" * 6,
"\e[32mx\e[0m",
"#{'_' * 5} 44",
"\n\e[90m(Use #{left_right} arrow keys, press Enter to select)\e[0m",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"What size? \e[32m44\e[0m\n\e[?25h"
].join)
end
it "sets quiet mode" do
choices = (32..54).step(2).to_a
prompt.input << "\r"
prompt.input.rewind
expect(prompt.slider("What size?", min: 32, max: 54, step: 2, quiet: true)).to eq(44)
expect(prompt.output.string).to eq([
output_helper("What size?", choices, 44, init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
"\e[?25h"
].join)
end
it "specifies quiet mode through DSL" do
prompt.input << "\r"
prompt.input.rewind
value = prompt.slider("What size?") do |slider|
slider.quiet true
slider.default 6
slider.min 0
slider.max 20
slider.step 2
slider.format "|:slider| %d%%"
end
expect(value).to eq(6)
expect(prompt.output.string).to eq([
"\e[?25lWhat size? ",
symbols[:pipe] + symbols[:line] * 3,
"\e[32m#{symbols[:bullet]}\e[0m",
"#{symbols[:line] * 7 + symbols[:pipe]} 6%",
"\n\e[90m(Use #{left_right} arrow keys, press Enter to select)\e[0m",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"\e[?25h"
].join)
end
it "changes to always show help" do
choices = (0..10).to_a
prompt.on(:keypress) do |event|
prompt.trigger(:keyright) if event.value == "l"
end
prompt.input << "l" << "l" << "\r"
prompt.input.rewind
res = prompt.slider("What size?", min: 0, max: 10, step: 1,
default: 0, show_help: :always)
expect(res).to eq(2)
expected_output = [
output_helper("What size?", choices, 0, init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What size?", choices, 1,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What size?", choices, 2,
hint: "Use #{left_right} arrow keys, press Enter to select"),
exit_message("What size?", 2)
].join
expect(prompt.output.string).to eq(expected_output)
end
it "changes to never show help" do
choices = (0..10).to_a
prompt.on(:keypress) do |event|
prompt.trigger(:keyright) if event.value == "l"
end
prompt.input << "l" << "l" << "\r"
prompt.input.rewind
res = prompt.slider("What size?", min: 0, max: 10, step: 1) do |range|
range.default 0
range.show_help :never
end
expect(res).to eq(2)
expected_output = [
output_helper("What size?", choices, 0, init: true),
output_helper("What size?", choices, 1),
output_helper("What size?", choices, 2),
exit_message("What size?", 2)
].join
expect(prompt.output.string).to eq(expected_output)
end
it "specifies choices instead of calculated range" do
choices = %w[a b c d e f g]
prompt.on(:keypress) do |event|
prompt.trigger(:keyright) if event.value == "l"
end
prompt.input << "l" << "l" << "\r"
prompt.input.rewind
res = prompt.slider("What letter?", choices) do |range|
range.default "b"
end
expect(res).to eq("d")
expected_output = [
output_helper("What letter?", choices, "b", init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What letter?", choices, "c"),
output_helper("What letter?", choices, "d"),
exit_message("What letter?", "d")
].join
expect(prompt.output.string).to eq(expected_output)
end
it "specifies choices through DSL" do
choices = %w[a b c d e f g]
prompt.on(:keypress) do |event|
prompt.trigger(:keyleft) if event.value == "l"
end
prompt.input << "l" << "l" << "\r"
prompt.input.rewind
res = prompt.slider("What letter?") do |range|
range.default "c"
range.choices choices
end
expect(res).to eq("a")
expected_output = [
output_helper("What letter?", choices, "c", init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What letter?", choices, "b"),
output_helper("What letter?", choices, "a"),
exit_message("What letter?", "a")
].join
expect(prompt.output.string).to eq(expected_output)
end
it "specifies choices through DSL" do
choices = %w[a b c d e f g]
prompt.on(:keypress) do |event|
prompt.trigger(:keyleft) if event.value == "l"
end
prompt.input << "l" << "l" << "\r"
prompt.input.rewind
res = prompt.slider("What letter?") do |range|
range.default "c"
range.choice "a"
range.choice "b"
range.choice "c"
range.choice "d"
range.choice "e"
range.choice "f"
range.choice "g"
end
expect(res).to eq("a")
expected_output = [
output_helper("What letter?", choices, "c", init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What letter?", choices, "b"),
output_helper("What letter?", choices, "a"),
exit_message("What letter?", "a")
].join
expect(prompt.output.string).to eq(expected_output)
end
it "mixes choices as values and via DSL and keeps ordering" do
choices = %w[a b c d e f g]
prompt.on(:keypress) do |event|
prompt.trigger(:keyleft) if event.value == "l"
end
prompt.input << "l" << "l" << "\r"
prompt.input.rewind
res = prompt.slider("What letter?", %w[a b c d]) do |range|
range.default "c"
range.choice "e"
range.choice "f"
range.choice "g"
end
expect(res).to eq("a")
expected_output = [
output_helper("What letter?", choices, "c", init: true,
hint: "Use #{left_right} arrow keys, press Enter to select"),
output_helper("What letter?", choices, "b"),
output_helper("What letter?", choices, "a"),
exit_message("What letter?", "a")
].join
expect(prompt.output.string).to eq(expected_output)
end
it "sets default choice by name" do
prompt.input << "\r"
prompt.input.rewind
res = prompt.slider("What letter?") do |range|
range.default "a"
range.choice "a", 1
range.choice "b", 2
range.choice "c", 3
end
expect(res).to eq(1)
end
it "sets default choice by index number" do
prompt.input << "\r"
prompt.input.rewind
res = prompt.slider("What letter?") do |range|
range.default 3
range.choice "a", 1
range.choice "b", 2
range.choice "c", 3
end
expect(res).to eq(3)
end
it "sets choice value to proc and executes it" do
prompt.input << "\r"
prompt.input.rewind
res = prompt.slider("What letter?") do |range|
range.choice "a", 1
range.choice "b" do "NOT THE BEEEEEEEES!" end
range.choice "c", 3
end
expect(res).to eq("NOT THE BEEEEEEEES!")
end
end
|