television 0.15.5

A very fast, portable and hackable fuzzy finder for the terminal
Documentation
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
//! Tests for CLI UI/layout options: --layout, --input-header, --ui-scale, --no-remote, --no-status-bar.
//!
//! These tests verify Television's user interface customization capabilities,
//! ensuring users can adapt the layout and appearance to their preferences and needs.

use television::tui::TESTING_ENV_VAR;

use super::super::common::*;

/// Tests that --layout landscape arranges panels side-by-side.
#[test]
fn test_layout_landscape() {
    let mut tester = PtyTester::new();

    // This sets the interface to use side-by-side panel arrangement
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--layout",
        "landscape",
    ]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the TUI started successfully in landscape layout
    tester.assert_tui_frame_contains(
        "╭───────────────────────── files ──────────────────────────╮╭─",
    ); // Should be in landscape layout

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --layout portrait arranges panels vertically stacked.
#[test]
fn test_layout_portrait() {
    let mut tester = PtyTester::new();

    // This sets the interface to use vertically stacked panel arrangement
    let cmd = tv_local_config_and_cable_with_args(&[
        "files", "--layout", "portrait",
    ]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the TUI started successfully in portrait layout (preview below results)
    tester.assert_tui_frame_contains("╭─────────────────────────────────────────────────────── files ────────────────────────────────────────────────────────╮");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --layout portrait arranges panels vertically stacked.
// FIXME: this should be in a separate module that tests TUI interactions
#[test]
fn test_toggle_layout() {
    let mut tester = PtyTester::new();

    // This sets the interface to use vertically stacked panel arrangement
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--layout",
        "portrait",
        "--keybindings",
        "ctrl-l='toggle_layout'",
    ]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the TUI started successfully in portrait layout
    tester.assert_tui_frame_contains("╭─────────────────────────────────────────────────────── files ────────────────────────────────────────────────────────╮");

    // Toggle to landscape layout
    tester.send(&ctrl('l'));

    // Verify the TUI switched to landscape layout
    tester.assert_tui_frame_contains(
        "╭───────────────────────── files ──────────────────────────╮╭─",
    );

    // Toggle back to portrait layout
    tester.send(&ctrl('l'));

    // Verify the TUI switched back to portrait layout
    tester.assert_tui_frame_contains("╭─────────────────────────────────────────────────────── files ────────────────────────────────────────────────────────╮");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --input-header customizes the text above the search input in Channel Mode.
#[test]
fn test_input_header_in_channel_mode() {
    let mut tester = PtyTester::new();

    // This overrides the channel's default input header with custom text
    let mut cmd = tv_local_config_and_cable_with_args(&["files"]);
    cmd.args(["--input-header", "UNIQUE16CHARID"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the custom input header is displayed
    tester.assert_tui_frame_contains("UNIQUE16CHARID");
    tester.assert_tui_frame_contains("CHANNEL  files");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --input-header works in Ad-hoc Mode.
#[test]
fn test_input_header_in_adhoc_mode() {
    let mut tester = PtyTester::new();

    // This provides a custom input header for an ad-hoc channel
    let mut cmd =
        tv_local_config_and_cable_with_args(&["--source-command", "ls"]);
    cmd.args(["--input-header", "UNIQUE16CHARID"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the custom input header is displayed
    tester.assert_tui_frame_contains("UNIQUE16CHARID");
    tester.assert_tui_frame_contains("CHANNEL  Custom");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --input-prompt customizes the prompt symbol in Channel Mode.
#[test]
fn test_input_prompt_in_channel_mode() {
    let mut tester = PtyTester::new();

    // This overrides the channel's default input prompt with custom symbol
    let mut cmd = tv_local_config_and_cable_with_args(&["files"]);
    cmd.args(["--input-prompt", ""]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the custom input prompt is displayed
    tester.assert_tui_frame_contains("");
    tester.assert_tui_frame_contains("CHANNEL  files");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --input-prompt works in Ad-hoc Mode.
#[test]
fn test_input_prompt_in_adhoc_mode() {
    let mut tester = PtyTester::new();

    // This provides a custom input prompt for an ad-hoc channel
    let mut cmd =
        tv_local_config_and_cable_with_args(&["--source-command", "ls"]);
    cmd.args(["--input-prompt", ""]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the custom input prompt is displayed
    tester.assert_tui_frame_contains("");
    tester.assert_tui_frame_contains("CHANNEL  Custom");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that the default input prompt "> " is used when no custom prompt is specified.
#[test]
fn test_default_input_prompt() {
    let mut tester = PtyTester::new();

    // This uses the default input prompt
    let cmd = tv_local_config_and_cable_with_args(&["files"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the default input prompt is displayed
    tester.assert_tui_frame_contains("> ");
    tester.assert_tui_frame_contains("CHANNEL  files");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --ui-scale adjusts the overall interface size.
#[test]
fn test_ui_scale() {
    let mut tester = PtyTester::new();

    // This scales the entire interface to 80% of normal size
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--ui-scale", "80"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the interface is scaled (smaller panels visible in output)
    tester.assert_tui_frame_contains(
        "╭─────────────────── files ────────────────────╮╭─",
    );

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --no-remote hides the remote control panel.
#[test]
fn test_no_remote_hides_remote_panel() {
    let mut tester = PtyTester::new();

    // This disables the remote control panel display
    let cmd = tv_local_config_and_cable_with_args(&["files", "--no-remote"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the remote control panel is hidden
    tester.assert_not_tui_frame_contains("── Channels ──");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --hide-status-bar starts the interface with the status bar hidden.
#[test]
fn test_hide_status_bar_flag_hides_status_bar() {
    let mut tester = PtyTester::new();

    // Start with the files channel and hide the status bar
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--hide-status-bar"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the status bar is hidden
    tester.assert_not_tui_frame_contains("CHANNEL  files");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --show-remote starts the interface with the remote control panel visible.
#[test]
fn test_show_remote_flag_shows_remote_panel() {
    let mut tester = PtyTester::new();

    // Start with the files channel and show the remote control panel
    let cmd = tv_local_config_and_cable_with_args(&["files", "--show-remote"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the remote control panel is visible (channel indicators)
    tester.assert_tui_frame_contains("(1) (2) (3)");

    // Send Ctrl+C to remote control
    tester.send(&ctrl('c'));

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --hide-remote prevents the remote control panel from showing at startup.
#[test]
fn test_hide_remote_flag_hides_remote_panel() {
    let mut tester = PtyTester::new();

    // Start with the files channel and hide the remote control panel
    let cmd = tv_local_config_and_cable_with_args(&["files", "--hide-remote"]);
    let mut child = tester.spawn_command_tui(cmd);

    tester.assert_not_tui_frame_contains("(1) (2) (3)");
    tester.assert_not_tui_frame_contains("── Channels ──");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --hide-remote conflicts with --no-remote.
#[test]
fn test_hide_remote_conflicts_with_no_remote() {
    let mut tester = PtyTester::new();

    // This should fail because the flags are mutually exclusive
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--hide-remote",
        "--no-remote",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

/// Tests that --hide-remote and --show-remote cannot be used together.
#[test]
fn test_hide_and_show_remote_conflict_errors() {
    let mut tester = PtyTester::new();

    // This should fail because the flags are mutually exclusive
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--hide-remote",
        "--show-remote",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

/// Tests that --no-help-panel disables the help panel entirely.
#[test]
fn test_no_help_panel_disables_help_panel() {
    let mut tester = PtyTester::new();

    // This disables the help panel entirely
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--no-help-panel"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Send Ctrl+H to try to open help panel (should not work)
    tester.send(&ctrl('h'));

    // Verify help panel is not displayed
    tester.assert_not_tui_frame_contains("───── Help ─────");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --hide-help-panel starts the interface with the help panel hidden.
#[test]
fn test_hide_help_panel_starts_with_help_hidden() {
    let mut tester = PtyTester::new();

    // Start with the files channel and hide the help panel
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--hide-help-panel"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Send Ctrl+H to open help panel (should still work since it's just hidden)
    tester.send(&ctrl('h'));

    // Verify help panel can be toggled visible
    tester.assert_tui_frame_contains("───── Help ─────");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --show-help-panel ensures the help panel is visible.
#[test]
fn test_show_help_panel_starts_with_help_visible() {
    let mut tester = PtyTester::new();

    // Start with the files channel and force the help panel visible
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--show-help-panel"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify help panel is initially visible
    tester.assert_tui_frame_contains("───── Help ─────");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --hide-help-panel conflicts with --no-help-panel.
#[test]
fn test_hide_help_panel_conflicts_with_no_help_panel() {
    let mut tester = PtyTester::new();

    // This should fail because the flags are mutually exclusive
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--hide-help-panel",
        "--no-help-panel",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

/// Tests that --hide-help-panel and --show-help-panel cannot be used together.
#[test]
fn test_hide_and_show_help_panel_conflict_errors() {
    let mut tester = PtyTester::new();

    // This should fail because the flags are mutually exclusive
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--hide-help-panel",
        "--show-help-panel",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

/// Tests that --no-help-panel conflicts with --show-help-panel.
#[test]
fn test_no_help_panel_conflicts_with_show_help_panel() {
    let mut tester = PtyTester::new();

    // This should fail because the flags are mutually exclusive
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--no-help-panel",
        "--show-help-panel",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

#[test]
fn test_tui_with_height_and_width() {
    let mut tester = PtyTester::new();

    // Test TUI with height 20 and width 80
    let mut cmd = tv_local_config_and_cable_with_args(&[
        "files", "--height", "20", "--width", "80",
    ]);
    cmd.env(TESTING_ENV_VAR, "1");
    let mut child = tester.spawn_command_tui(cmd);

    // Check that 'files' appears in the frame content
    tester.assert_tui_frame_contains("CHANNEL  files");

    // Validate frame dimensions (20 rows × 80 columns)
    let frame = tester.get_tui_frame();
    let non_empty_lines: Vec<&str> =
        frame.lines().filter(|l| !l.trim().is_empty()).collect();
    assert_eq!(
        non_empty_lines.len(),
        20,
        "Expected 20 rows, got {}",
        non_empty_lines.len()
    );
    let max_width = non_empty_lines
        .iter()
        .map(|l| l.chars().count())
        .max()
        .unwrap_or(0);
    assert_eq!(max_width, 80, "Expected 80 columns, got {}", max_width);

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --no-preview disables the preview panel entirely.
#[test]
fn test_no_preview_disables_preview_panel() {
    let mut tester = PtyTester::new();

    // This disables the preview panel entirely
    let cmd = tv_local_config_and_cable_with_args(&["files", "--no-preview"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Try to toggle preview - it shouldn't work since it's disabled entirely
    tester.send("o"); // Toggle preview key

    // Verify no preview elements are shown (no scrollbar, no panel frame)
    tester.assert_tui_frame_contains_none(&["───╮╭───", "Show Preview"]);

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --show-preview starts the interface with the preview panel visible.
#[test]
fn test_show_preview_starts_with_preview_visible() {
    let mut tester = PtyTester::new();

    // Start with the files channel and force the preview panel visible
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--show-preview"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify preview panel is initially visible (landscape layout shows side-by-side panels)
    tester.assert_tui_frame_contains("───╮╭───");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --no-status-bar disables the status bar entirely.
#[test]
fn test_no_status_bar_disables_status_bar() {
    let mut tester = PtyTester::new();

    // This disables the status bar entirely
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--no-status-bar"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify the status bar is not shown
    tester.assert_not_tui_frame_contains("CHANNEL  files");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --show-status-bar starts the interface with the status bar visible.
#[test]
fn test_show_status_bar_starts_with_status_visible() {
    let mut tester = PtyTester::new();

    // Start with the files channel and force the status bar visible
    let cmd =
        tv_local_config_and_cable_with_args(&["files", "--show-status-bar"]);
    let mut child = tester.spawn_command_tui(cmd);

    // Verify status bar is initially visible
    tester.assert_tui_frame_contains("CHANNEL  files");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --hide-preview-scrollbar hides the preview panel scrollbar.
#[test]
fn test_hide_preview_scrollbar_hides_scrollbar() {
    let mut tester = PtyTester::new();

    // This hides the preview scrollbar while keeping the preview panel functional
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--hide-preview-scrollbar",
    ]);
    let mut child = tester.spawn_command_tui(cmd);

    // The preview panel should still be visible but without scrollbar indicators
    tester.assert_tui_frame_contains("───╮╭───");
    tester.assert_not_tui_frame_contains("");

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}

/// Tests that --no-preview conflicts with preview-related flags.
#[test]
fn test_no_preview_conflicts_with_preview_flags() {
    let mut tester = PtyTester::new();

    // This should fail because --no-preview conflicts with --preview-command
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--no-preview",
        "--preview-command",
        "cat {}",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

/// Tests that --no-status-bar conflicts with status-bar-related flags.
#[test]
fn test_no_status_bar_conflicts_with_status_bar_flags() {
    let mut tester = PtyTester::new();

    // This should fail because --no-status-bar conflicts with --show-status-bar
    let cmd = tv_local_config_and_cable_with_args(&[
        "files",
        "--no-status-bar",
        "--show-status-bar",
    ]);
    tester.spawn_command(cmd);

    // CLI should exit with error message, not show TUI
    tester.assert_raw_output_contains("cannot be used with");
}

#[test]
// FIXME: needs https://github.com/crossterm-rs/crossterm/pull/957
#[ignore = "needs https://github.com/crossterm-rs/crossterm/pull/957"]
fn test_tui_with_height_only() {
    let mut tester = PtyTester::new();

    // Test TUI with only height specified
    let mut cmd =
        tv_local_config_and_cable_with_args(&["files", "--height", "15"]);
    cmd.env(TESTING_ENV_VAR, "1");
    let mut child = tester.spawn_command_tui(cmd);

    // Check that 'files' appears in the frame content
    tester.assert_tui_frame_contains("CHANNEL  files");

    // Validate frame height (15 rows)
    let frame = tester.get_tui_frame();
    let non_empty_lines: Vec<&str> =
        frame.lines().filter(|l| !l.trim().is_empty()).collect();
    assert_eq!(
        non_empty_lines.len(),
        15,
        "Expected 15 rows, got {}",
        non_empty_lines.len()
    );

    // Send Ctrl+C to exit
    tester.send(&ctrl('c'));
    tester.assert_exit_ok(&mut child, DEFAULT_DELAY);
}