File: multi_select_spec.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (908 lines) | stat: -rw-r--r-- 36,782 bytes parent folder | download
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# frozen_string_literal: true

RSpec.describe TTY::Prompt do
  let(:symbols) { TTY::Prompt::Symbols.symbols }
  let(:up_down) { "#{symbols[:arrow_up]}/#{symbols[:arrow_down]}" }
  let(:left_right) { "#{symbols[:arrow_left]}/#{symbols[:arrow_right]}" }
  let(:left_key) { "\e[D" }
  let(:right_key) { "\e[C" }

  subject(:prompt) { TTY::Prompt::Test.new }

  def output_helper(prompt, choices, active, selected, options = {})
    hint = options[:hint]
    init = options.fetch(:init, false)
    enum = options[:enum]

    out = []
    out << "\e[?25l" if init
    out << prompt << " "
    out << "(min. #{options[:min]}) " if options[:min]
    out << "(max. #{options[:max]}) " if options[:max]
    out << selected.join(", ")
    out << " " if (init || hint) && !selected.empty?
    out << "\e[90m(#{hint})\e[0m" if hint
    out << "\n"
    out << choices.map.with_index do |choice, i|
      name = choice.is_a?(Hash) ? choice[:name] : choice
      disabled = choice.is_a?(Hash) ? choice[:disabled] : false
      num = (i + 1).to_s + enum if enum

      prefix = name == active ? "#{symbols[:marker]} " : "  "
      prefix += if disabled
                  "\e[31m#{symbols[:cross]}\e[0m #{num}#{name} #{disabled}"
                elsif selected.include?(name)
                  "\e[32m#{symbols[:radio_on]}\e[0m #{num}#{name}"
                else
                  "#{symbols[:radio_off]} #{num}#{name}"
                end
      prefix
    end.join("\n")
    out << "\e[2K\e[1G\e[1A" * choices.count
    out << "\e[2K\e[1G"
    out.join
  end

  def exit_message(prompt, choices)
    out = []
    out << "#{prompt} "
    out << "\e[32m#{choices.join(', ')}\e[0m" unless choices.empty?
    out << "\n\e[?25h"
    out.join
  end

  # Ensure a wide prompt on CI
  before { allow(TTY::Screen).to receive(:width).and_return(200) }

  it "selects nothing when return pressed immediately" do
    choices = %i[vodka beer wine whisky bourbon]
    prompt.input << "\r"
    prompt.input.rewind

    expect(prompt.multi_select("Select drinks?", choices)).to eq([])

    expected_output =
      output_helper("Select drinks?", choices, :vodka, [], init: true,
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
      exit_message("Select drinks?", [])

    expect(prompt.output.string).to eq(expected_output)
  end

  it "selects item when space pressed" do
    choices = %w[vodka beer wine whisky bourbon]
    prompt.input << " \r"
    prompt.input.rewind
    expect(prompt.multi_select("Select drinks?", choices)).to eq(["vodka"])

    expected_output =
      output_helper("Select drinks?", choices, "vodka", [], init: true,
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
      output_helper("Select drinks?", choices, "vodka", ["vodka"]) +
      exit_message("Select drinks?", %w[vodka])

    expect(prompt.output.string).to eq(expected_output)
  end

  it "selects item when space pressed but doesn't echo item if echo: false" do
    choices = %w[vodka beer wine whisky bourbon]
    prompt.input << " \r"
    prompt.input.rewind
    expect(prompt.multi_select("Select drinks?", choices, echo: false)).to eq(["vodka"])

    expected_output = [
      "\e[?25lSelect drinks? \e[90m(Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)\e[0m\n",
      "#{symbols[:marker]} #{symbols[:radio_off]} vodka\n",
      "  #{symbols[:radio_off]} beer\n",
      "  #{symbols[:radio_off]} wine\n",
      "  #{symbols[:radio_off]} whisky\n",
      "  #{symbols[:radio_off]} bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? \n",
      "#{symbols[:marker]} \e[32m#{symbols[:radio_on]}\e[0m vodka\n",
      "  #{symbols[:radio_off]} beer\n",
      "  #{symbols[:radio_off]} wine\n",
      "  #{symbols[:radio_off]} whisky\n",
      "  #{symbols[:radio_off]} bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? \n\e[?25h"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "sets choice custom values" do
    choices = {vodka: 1, beer: 2, wine: 3, whisky: 4, bourbon: 5}
    prompt.input << " \r"
    prompt.input.rewind

    expect(prompt.multi_select("Select drinks?", choices)).to eq([1])

    expected_output =
      output_helper("Select drinks?", choices.keys, :vodka, [], init: true,
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
      output_helper("Select drinks?", choices.keys, :vodka, [:vodka]) +
      exit_message("Select drinks?", %w[vodka])

    expect(prompt.output.string).to eq(expected_output)
  end

  it "sets choice name and value through DSL" do
    prompt.input << " \r"
    prompt.input.rewind
    value = prompt.multi_select("Select drinks?") do |menu|
              menu.symbols marker: ">", radio_off: "-", radio_on: "="
              menu.enum ")"

              menu.choice :vodka, {score: 1}
              menu.choice :beer, 2
              menu.choice :wine, 3
              menu.choices whisky: 4, bourbon: 5
            end
    expect(value).to eq([{score: 1}])
    expect(prompt.output.string).to eq([
      "\e[?25lSelect drinks? \e[90m(Press #{up_down} arrow or 1-5 number to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)\e[0m\n",
      "> - 1) vodka\n",
      "  - 2) beer\n",
      "  - 3) wine\n",
      "  - 4) whisky\n",
      "  - 5) bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? vodka\n",
      "> \e[32m=\e[0m 1) vodka\n",
      "  - 2) beer\n",
      "  - 3) wine\n",
      "  - 4) whisky\n",
      "  - 5) bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? \e[32mvodka\e[0m\n\e[?25h"
    ].join)
  end

  it "sets default options through DSL syntax" do
    prompt.input << "\r"
    prompt.input.rewind
    value = prompt.multi_select("Select drinks?") do |menu|
              menu.default 2, 5

              menu.choice :vodka,   {score: 10}
              menu.choice :beer,    {score: 20}
              menu.choice :wine,    {score: 30}
              menu.choice :whisky,  {score: 40}
              menu.choice :bourbon, {score: 50}
            end
    expect(value).to match_array([{score: 20}, {score: 50}])
    expect(prompt.output.string).to eq([
      "\e[?25lSelect drinks? beer, bourbon \e[90m(Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)\e[0m\n",
      "  #{symbols[:radio_off]} vodka\n",
      "  \e[32m#{symbols[:radio_on]}\e[0m beer\n",
      "  #{symbols[:radio_off]} wine\n",
      "  #{symbols[:radio_off]} whisky\n",
      "#{symbols[:marker]} \e[32m#{symbols[:radio_on]}\e[0m bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? \e[32mbeer, bourbon\e[0m\n\e[?25h"
    ].join)
  end

  it "sets choice value to nil through DSL" do
    choices = [
      {name: "none", value: nil},
      {name: "vodka", value: 1},
      {name: "beer", value: 1},
      {name: "wine", value: 1}
    ]
    prompt.input << "\r"
    prompt.input.rewind
    value = prompt.multi_select("Select drinks?", default: 1) do |menu|
              menu.choice :none,  nil
              menu.choice :vodka, {score: 10}
              menu.choice :beer,  {score: 20}
              menu.choice :wine,  {score: 30}
            end
    expect(value).to match_array([nil])

    expected_output =
      output_helper("Select drinks?", choices, "none", %w[none], init: true,
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select " \
              "(all|rev) and Enter to finish") +
      exit_message("Select drinks?", %w[none])

    expect(prompt.output.string).to eq(expected_output)
  end

  it "sets default options through hash syntax" do
    prompt.input << "\r"
    prompt.input.rewind
    value = prompt.multi_select("Select drinks?", default: [2, 5]) do |menu|
              menu.choice :vodka,   {score: 10}
              menu.choice :beer,    {score: 20}
              menu.choice :wine,    {score: 30}
              menu.choice :whisky,  {score: 40}
              menu.choice :bourbon, {score: 50}
            end
    expect(value).to match_array([{score: 20}, {score: 50}])
  end

  it "sets default choices using names" do
    prompt.input << "\r"
    prompt.input.rewind
    value = prompt.multi_select("Select drinks?", default: [:beer, "whisky"]) do |menu|
              menu.choice :vodka,   {score: 10}
              menu.choice :beer,    {score: 20}
              menu.choice :wine,    {score: 30}
              menu.choice :whisky,  {score: 40}
              menu.choice :bourbon, {score: 50}
            end
    expect(value).to match_array([{score: 20}, {score: 40}])
  end

  it "raises error for defaults out of range" do
    prompt.input << "\r"
    prompt.input.rewind
    expect {
      prompt.multi_select("Select drinks?", default: [2, 6]) do |menu|
        menu.choice :vodka,   {score: 10}
        menu.choice :beer,    {score: 20}
        menu.choice :wine,    {score: 30}
        menu.choice :whisky,  {score: 40}
        menu.choice :bourbon, {score: 50}
      end
    }.to raise_error(TTY::Prompt::ConfigurationError,
                     /default index `6` out of range \(1 - 5\)/)
  end

  it "sets prompt prefix" do
    prompt = TTY::Prompt::Test.new(prefix: "[?] ")
    choices = %w[vodka beer wine whisky bourbon]
    prompt.input << "\r"
    prompt.input.rewind
    expect(prompt.multi_select("Select drinks?", choices)).to eq([])
    expect(prompt.output.string).to eq([
      "\e[?25l[?] Select drinks? \e[90m(Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)\e[0m\n",
      "#{symbols[:marker]} #{symbols[:radio_off]} vodka\n",
      "  #{symbols[:radio_off]} beer\n",
      "  #{symbols[:radio_off]} wine\n",
      "  #{symbols[:radio_off]} whisky\n",
      "  #{symbols[:radio_off]} bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "[?] Select drinks? \n\e[?25h"
    ].join)
  end

  it "changes selected item color & marker" do
    choices = %w[vodka beer wine whisky bourbon]
    prompt.input << "\r"
    prompt.input.rewind
    options = {default: [1], active_color: :blue, symbols: {marker: ">"}}
    expect(prompt.multi_select("Select drinks?", choices, options)).to eq(["vodka"])
    expect(prompt.output.string).to eq([
      "\e[?25lSelect drinks? vodka \e[90m(Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)\e[0m\n",
      "> \e[34m#{symbols[:radio_on]}\e[0m vodka\n",
      "  #{symbols[:radio_off]} beer\n",
      "  #{symbols[:radio_off]} wine\n",
      "  #{symbols[:radio_off]} whisky\n",
      "  #{symbols[:radio_off]} bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? \e[34mvodka\e[0m\n\e[?25h"
    ].join)
  end

  it "changes help text and color" do
    choices = %w[vodka beer wine whisky bourbon]
    prompt.input << "\r"
    prompt.input.rewind
    options = {help: "(Bash keyboard)", help_color: :cyan}
    answer = prompt.multi_select("Select drinks?", choices, options)

    expect(answer).to eq([])
    expected_output = [
      "\e[?25lSelect drinks? \e[36m(Bash keyboard)\e[0m\n",
      "#{symbols[:marker]} #{symbols[:radio_off]} vodka\n",
      "  #{symbols[:radio_off]} beer\n",
      "  #{symbols[:radio_off]} wine\n",
      "  #{symbols[:radio_off]} whisky\n",
      "  #{symbols[:radio_off]} bourbon",
      "\e[2K\e[1G\e[1A" * 5, "\e[2K\e[1G",
      "Select drinks? \n\e[?25h"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "sets prompt to quiet mode" do
    prompt = TTY::Prompt::Test.new(quiet: true)
    choices = %w[vodka beer wine whisky bourbon]
    prompt.input << "\r"
    prompt.input.rewind

    expect(prompt.multi_select("Select drinks?", choices)).to eq([])

    expected_output =
      output_helper("Select drinks?", choices, "vodka", [], init: true,
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
      "\e[?25h"
    expect(prompt.output.string).to eq(expected_output)
  end

  it "changes to always show help" do
    choices = %w[vodka beer wine whisky bourbon]
    prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
    prompt.input << "j" << "j" << " " << "\r"
    prompt.input.rewind

    answer = prompt.multi_select("Select drinks?", choices, show_help: :always)
    expect(answer).to eq(%w[wine])

    expected_output =
      output_helper("Select drinks?", choices, "vodka", [], init: true,
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select " \
              "(all|rev) and Enter to finish") +
      output_helper("Select drinks?", choices, "beer", [],
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select " \
              "(all|rev) and Enter to finish") +
      output_helper("Select drinks?", choices, "wine", [],
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select " \
              "(all|rev) and Enter to finish") +
      output_helper("Select drinks?", choices, "wine", %w[wine],
        hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select " \
              "(all|rev) and Enter to finish") +
      exit_message("Select drinks?", %w[wine])

    expect(prompt.output.string).to eq(expected_output)
  end

  it "changes to never show help" do
    choices = %w[vodka beer wine whisky bourbon]
    prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
    prompt.input << "j" << "j" << " " << "\r"
    prompt.input.rewind

    answer = prompt.multi_select("Select drinks?", choices, show_help: :never)
    expect(answer).to eq(%w[wine])

    expected_output =
      output_helper("Select drinks?", choices, "vodka", [], init: true) +
      output_helper("Select drinks?", choices, "beer", []) +
      output_helper("Select drinks?", choices, "wine", []) +
      output_helper("Select drinks?", choices, "wine", %w[wine]) +
      exit_message("Select drinks?", %w[wine])

    expect(prompt.output.string).to eq(expected_output)
  end

  context "when paginated" do
    it "paginates long selections" do
      choices = %w[A B C D E F G H]
      prompt.input << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What letter?", choices, per_page: 3, default: 4)
      expect(answer).to eq(["D"])

      expected_output =
        output_helper("What letter?", choices[3..5], "D", %w[D], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        exit_message("What letter?", %w[D])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "paginates choices as hash object" do
      choices = {A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8}
      prompt.input << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What letter?", choices, default: 4, per_page: 3)
      expect(answer).to eq([4])

      expected_output =
        output_helper("What letter?", choices.keys[3..5], :D, [:D], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        exit_message("What letter?", %w[D])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "paginates long selections through DSL" do
      choices = %w[A B C D E F G H]
      prompt.input << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What letter?") do |menu|
                menu.per_page 3
                menu.default 4
                menu.choices choices
              end
      expect(answer).to eq(["D"])

      expected_output =
        output_helper("What letter?", choices[3..5], "D", %w[D], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        exit_message("What letter?", %w[D])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "doesn't paginate short selections" do
      choices = %w[A B C D]
      prompt.input << "\r"
      prompt.input.rewind

      value = prompt.multi_select("What letter?", choices, per_page: 4, default: 1)
      expect(value).to eq(["A"])

      expected_output =
        output_helper("What letter?", choices, "A", %w[A], init: true,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        exit_message("What letter?", %w[A])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "navigates evenly paged output with right arrow until end of selection" do
      choices = ("1".."12").to_a
      prompt.on(:keypress) { |e| prompt.trigger(:keyright) if e.value == "l" }
      prompt.input << "l" << "l" << "l" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, per_page: 4)

      expect(answer).to eq(["9"])

      expected_output =
        output_helper("What number?", choices[0..3], "1", [], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[4..7], "5", []) +
        output_helper("What number?", choices[8..11], "9", []) +
        output_helper("What number?", choices[8..11], "9", []) +
        output_helper("What number?", choices[8..11], "9", ["9"]) +
        exit_message("What number?", %w[9])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "navigates unevenly paged output with right arrow until the end of selection" do
      choices = ("1".."10").to_a
      prompt.on(:keypress) { |e| prompt.trigger(:keyright) if e.value == "l" }
      prompt.input << "l" << "l" << "l" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, default: 4, per_page: 4)

      expect(answer).to eq(%w[4 10])

      expected_output =
        output_helper("What number?", choices[3..6], "4", ["4"], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[4..7], "8", ["4"]) +
        output_helper("What number?", choices[8..9], "10", ["4"]) +
        output_helper("What number?", choices[8..9], "10", ["4"]) +
        output_helper("What number?", choices[8..9], "10", %w[4 10]) +
        exit_message("What number?", %w[4 10])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "navigates left and right" do
      choices = ("1".."10").to_a
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyright) if e.value == "l"
        prompt.trigger(:keyleft) if e.value == "h"
      }
      prompt.input << "l" << "l" << "h" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, default: 2, per_page: 4)

      expect(answer).to eq(%w[2 6])

      expected_output =
        output_helper("What number?", choices[0..3], "2", ["2"], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[4..7], "6", ["2"]) +
        output_helper("What number?", choices[8..9], "10", ["2"]) +
        output_helper("What number?", choices[4..7], "6", ["2"]) +
        output_helper("What number?", choices[4..7], "6", %w[2 6]) +
        exit_message("What number?", %w[2 6])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "combines up/down navigation with left/right" do
      choices = ("1".."11").to_a
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyup)    if e.value == "k"
        prompt.trigger(:keydown)  if e.value == "j"
        prompt.trigger(:keyright) if e.value == "l"
        prompt.trigger(:keyleft)  if e.value == "h"
      }
      prompt.input << "j" << "l" << "k" << "k" << "h" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, default: 2, per_page: 4)

      expect(answer).to eq(%w[1 2])

      expected_output =
        output_helper("What number?", choices[0..3], "2", ["2"], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[0..3], "3", ["2"]) +
        output_helper("What number?", choices[4..7], "7", ["2"]) +
        output_helper("What number?", choices[4..7], "6", ["2"]) +
        output_helper("What number?", choices[3..6], "5", ["2"]) +
        output_helper("What number?", choices[0..3], "1", ["2"]) +
        output_helper("What number?", choices[0..3], "1", %w[1 2]) +
        exit_message("What number?", %w[1 2])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "selects all paged choices with ctrl+a" do
      choices = ("1".."12").to_a
      prompt.on(:keypress) { |e| prompt.trigger(:keyctrl_a) if e.value == "a" }
      prompt.input << "a" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, per_page: 4)

      expect(answer).to eq(choices - %w[1])

      expected_output =
        output_helper("What number?", choices[0..3], "1", [], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[0..3], "1", choices) +
        output_helper("What number?", choices[0..3], "1", choices - %w[1]) +
        exit_message("What number?", choices - %w[1])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "reverts selection accross pages with Ctrl+r" do
      choices = ("1".."12").to_a
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyctrl_r) if e.value == "r"
        prompt.trigger(:keydown) if e.value == "j"
      }
      prompt.input << " " << "j" << " " << "j" << " " << "r" << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, per_page: 4)

      expect(answer).to eq(("4".."12").to_a)

      expected_output =
        output_helper("What number?", choices[0..3], "1", [], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[0..3], "1", ["1"]) +
        output_helper("What number?", choices[0..3], "2", ["1"]) +
        output_helper("What number?", choices[0..3], "2", %w[1 2]) +
        output_helper("What number?", choices[0..3], "3", %w[1 2]) +
        output_helper("What number?", choices[0..3], "3", %w[1 2 3]) +
        output_helper("What number?", choices[0..3], "3", ("4".."12").to_a) +
        exit_message("What number?", ("4".."12").to_a)

      expect(prompt.output.string).to eq(expected_output)
    end
  end

  context "with :cycle" do
    it "doesn't cycle by default" do
      choices = %w[A B C]
      prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
      prompt.input << "j" << "j" << "j" << " " << "\r"
      prompt.input.rewind

      value = prompt.multi_select("What letter?", choices)
      expect(value).to eq(["C"])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What letter?", choices, "B", []) +
        output_helper("What letter?", choices, "C", []) +
        output_helper("What letter?", choices, "C", []) +
        output_helper("What letter?", choices, "C", ["C"]) +
        exit_message("What letter?", %w[C])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "cycles when configured to do so" do
      choices = %w[A B C]
      prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
      prompt.input << "j" << "j" << "j" << " " << "\r"
      prompt.input.rewind

      value = prompt.multi_select("What letter?", choices, cycle: true)
      expect(value).to eq(["A"])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What letter?", choices, "B", []) +
        output_helper("What letter?", choices, "C", []) +
        output_helper("What letter?", choices, "A", []) +
        output_helper("What letter?", choices, "A", ["A"]) +
        exit_message("What letter?", %w[A])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "cycles choices using left/right arrows" do
      choices = ("1".."10").to_a
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyright) if e.value == "l"
        prompt.trigger(:keyleft) if e.value == "h"
      }
      prompt.input << "l" << "l" << "l" << "h" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What number?", choices, default: 2, per_page: 4, cycle: true)

      expect(answer).to eq(%w[2 10])

      expected_output =
        output_helper("What number?", choices[0..3], "2", %w[2], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What number?", choices[4..7], "6", %w[2]) +
        output_helper("What number?", choices[8..9], "10", %w[2]) +
        output_helper("What number?", choices[0..3], "2", %w[2]) +
        output_helper("What number?", choices[8..9], "10", %w[2]) +
        output_helper("What number?", choices[8..9], "10", %w[2 10]) +
        exit_message("What number?", %w[2 10])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "cycles filtered choices left and right" do
      numbers = ("1".."10").to_a
      choices = numbers.map { |n| "a#{n}" } + numbers.map { |n| "b#{n}" }
      prompt.input << "b" << right_key << right_key << right_key
      prompt.input << left_key << left_key << left_key << " \r"
      prompt.input.rewind

      answer = prompt.multi_select("What room?", choices, default: 2, per_page: 4,
                                                          filter: true, cycle: true)

      expect(answer).to eq(%w[a2 b2])

      expected_output =
        output_helper("What room?", choices[0..3], "a2", %w[a2], init: true,
          hint: "Press #{up_down}/#{left_right} arrow to move, Space/Ctrl+A|R to select (all|rev), Enter to finish and letters to filter") +
        output_helper("What room?", choices[10..13], "b1", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[14..17], "b5", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[18..20], "b9", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[10..13], "b1", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[18..20], "b10", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[14..17], "b6", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[10..13], "b2", %w[a2], hint: "Filter: \"b\"") +
        output_helper("What room?", choices[10..13], "b2", %w[a2 b2], hint: "Filter: \"b\"") +
        exit_message("What room?", %w[a2 b2])

      expect(prompt.output.string).to eq(expected_output)
    end
  end

  context "with filter" do
    it "doesn't lose the selection when switching between filters" do
      choices = %w[Tiny Medium Large Huge]
      prompt.on(:keypress) { |e| prompt.trigger(:keydelete) if e.value == "\r" }
      prompt.input << " "         # select `Tiny`
      prompt.input << "a" << " "  # match and select `Large`
      prompt.input << "\u007F"    # backspace (shows all)
      prompt.input << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What size?", choices, filter: true, show_help: :always)
      expect(answer).to eql(%w[Tiny Large])

      expected_output =
        output_helper("What size?", %w[Tiny Medium Large Huge], "Tiny", %w[], init: true,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev), Enter to finish and letters to filter") +
        output_helper("What size?", %w[Tiny Medium Large Huge], "Tiny", %w[Tiny],
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev), Enter to finish and letters to filter") +
        output_helper("What size?", %w[Large], "Large", %w[Tiny], hint: "Filter: \"a\"") +
        output_helper("What size?", %w[Large], "Large", %w[Tiny Large], hint: "Filter: \"a\"") +
        output_helper("What size?", %w[Tiny Medium Large Huge], "Tiny", %w[Tiny Large],
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev), Enter to finish and letters to filter") +
        exit_message("What size?", %w[Tiny Large])

      expect(prompt.output.string).to eql(expected_output)
    end
  end

  context "with :disabled" do
    it "fails when default item is also disabled" do
      choices = [
        {name: "vodka", disabled: true},
        "beer", "wine", "whisky", "bourbon"
      ]
      expect {
        prompt.multi_select("Select drinks?", choices, default: 1)
      }.to raise_error(TTY::Prompt::ConfigurationError,
                       "default index `1` matches disabled choice")
    end

    it "adjusts active index to match first non-disabled choice" do
      choices = [
        {name: "vodka", disabled: true},
        "beer", "wine", "whisky", "bourbon"
      ]
      prompt.input << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("Select drinks?", choices)
      expect(answer).to eq(["beer"])
    end

    it "omits disabled choice when nagivating menu" do
      choices = [
        {name: "A"},
        {name: "B", disabled: "(out)"},
        {name: "C", disabled: "(out)"},
        {name: "D"},
        {name: "E"}
      ]
      prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == "j" }
      prompt.input << "j" << " " << "j" << " " << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What letter?", choices)
      expect(answer).to eq(%w[D E])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What letter?", choices, "D", []) +
        output_helper("What letter?", choices, "D", %w[D]) +
        output_helper("What letter?", choices, "E", %w[D]) +
        output_helper("What letter?", choices, "E", %w[D E]) +
        exit_message("What letter?", %w[D E])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "omits disabled choice when number key is pressed" do
      choices = [
        {name: "vodka", value: 1},
        {name: "beer", value: 1, disabled: true},
        {name: "wine", value: 1},
        {name: "whisky", value: 1, disabled: true},
        {name: "bourbon", value: 1}
      ]
      prompt.input << "2" << " \r"
      prompt.input.rewind
      answer = prompt.multi_select("Select drinks?") do |menu|
                menu.enum ")"

                menu.choice :vodka, 1
                menu.choice :beer, 2, disabled: true
                menu.choice :wine, 3
                menu.choice :whisky, 4, disabled: true
                menu.choice :bourbon, 5
              end
      expect(answer).to eq([1])

      expected_output =
        output_helper("Select drinks?", choices, "vodka", [], init: true, enum: ") ",
          hint: "Press #{up_down} arrow or 1-5 number to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("Select drinks?", choices, "vodka", [], enum: ") ") +
        output_helper("Select drinks?", choices, "vodka", %w[vodka], enum: ") ") +
        exit_message("Select drinks?", %w[vodka])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "selects all non-disabled choices when ctrl+a is pressed" do
      choices = [
        {name: "A"},
        {name: "B", disabled: "(out)"},
        {name: "C", disabled: "(out)"},
        {name: "D"},
        {name: "E"}
      ]
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyctrl_a) if e.value == "a"
        prompt.trigger(:keyctrl_r) if e.value == "r"
      }
      prompt.input << "a" << "r" << "a" << "\r"
      prompt.input.rewind

      answer = prompt.multi_select("What letter?", choices)
      expect(answer).to eq(%w[A D E])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What letter?", choices, "A", %w[A D E]) +
        output_helper("What letter?", choices, "A", %w[]) +
        output_helper("What letter?", choices, "A", %w[A D E]) +
        exit_message("What letter?", %w[A D E])

      expect(prompt.output.string).to eq(expected_output)
    end
  end

  context "with :min" do
    it "requires number of choices" do
      choices = %w[A B C]
      prompt.on(:keypress) { |e|
        prompt.trigger(:keydown) if e.value == "j"
      }
      prompt.input << " " << "\r" << "j" << " " << "\r"
      prompt.input.rewind

      value = prompt.multi_select("What letter?", choices, min: 2, per_page: 100)
      expect(value).to eq(%w[A B])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true, min: 2,
          hint: "Press #{up_down} arrow to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish") +
        output_helper("What letter?", choices, "A", %w[A], min: 2) +
        output_helper("What letter?", choices, "A", %w[A], min: 2) +
        output_helper("What letter?", choices, "B", %w[A], min: 2) +
        output_helper("What letter?", choices, "B", %w[A B], min: 2) +
        exit_message("What letter?", %w[A B])

      expect(prompt.output.string).to eq(expected_output)
    end
  end

  context "with :max" do
    it "limits number of choices" do
      choices = %w[A B C]
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyup)   if e.value == "k"
        prompt.trigger(:keydown) if e.value == "j"
      }
      prompt.input << " " << "j" << " " << "j" << " " << "k" << " " << "j" << " " << "\r"
      prompt.input.rewind

      value = prompt.multi_select("What letter?", choices, max: 2, per_page: 100)
      expect(value).to eq(%w[A C])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true, max: 2,
          hint: "Press #{up_down} arrow to move, Space to select and Enter to finish") +
        output_helper("What letter?", choices, "A", %w[A], max: 2) +
        output_helper("What letter?", choices, "B", %w[A], max: 2) +
        output_helper("What letter?", choices, "B", %w[A B], max: 2) +
        output_helper("What letter?", choices, "C", %w[A B], max: 2) +
        output_helper("What letter?", choices, "C", %w[A B], max: 2) +
        output_helper("What letter?", choices, "B", %w[A B], max: 2) +
        output_helper("What letter?", choices, "B", %w[A], max: 2) +
        output_helper("What letter?", choices, "C", %w[A], max: 2) +
        output_helper("What letter?", choices, "C", %w[A C], max: 2) +
        exit_message("What letter?", %w[A C])

      expect(prompt.output.string).to eq(expected_output)
    end

    it "disables Ctrl+a/Ctrl+r selection when :max option is specified" do
      choices = %w[A B C D E F G]
      prompt.on(:keypress) { |e|
        prompt.trigger(:keyctrl_a) if e.value == "a"
        prompt.trigger(:keyctrl_r) if e.value == "r"
        prompt.trigger(:keydown) if e.value == "j"
      }
      prompt.input << "a" << "j" << " " << "r" << "j" << " " << "\r"
      prompt.input.rewind

      value = prompt.multi_select("What letter?", choices, max: 2, per_page: 100)
      expect(value).to eq(%w[B C])

      expected_output =
        output_helper("What letter?", choices, "A", [], init: true, max: 2,
          hint: "Press #{up_down} arrow to move, Space to select and Enter to finish") +
        output_helper("What letter?", choices, "A", %w[], max: 2) +
        output_helper("What letter?", choices, "B", %w[], max: 2) +
        output_helper("What letter?", choices, "B", %w[B], max: 2) +
        output_helper("What letter?", choices, "B", %w[B], max: 2) +
        output_helper("What letter?", choices, "C", %w[B], max: 2) +
        output_helper("What letter?", choices, "C", %w[B C], max: 2) +
        exit_message("What letter?", %w[B C])

      expect(prompt.output.string).to eq(expected_output)
    end
  end
end