xtui 0.1.0

TUI for discovering and running project commands (xtask, cargo, just, nu, npm, make, mise)
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
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
use crate::history::{self, HistoryEntry};
use crate::pipeline::{Pipeline, PipelineStep};
use crate::runner::{self, RunningTask};
use crate::search::SearchState;
use crate::source::{CommandSource, SourceCommand, all_sources};
use crate::status::{self, GitStatus};
use crate::ui;
use anyhow::Result;
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::crossterm::execute;
use ratatui::crossterm::terminal::{
    EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use std::io;
use std::path::PathBuf;

/// Output buffer line limit before trimming oldest lines.
const OUTPUT_LINE_LIMIT: usize = 10_000;
/// Number of lines trimmed when the output buffer exceeds `OUTPUT_LINE_LIMIT`.
const OUTPUT_TRIM_SIZE: usize = 1_000;
const _: () = assert!(OUTPUT_TRIM_SIZE < OUTPUT_LINE_LIMIT);
/// Event poll interval in milliseconds.
const EVENT_POLL_MS: u64 = 50;

fn base64_encode(input: &[u8]) -> String {
    use std::fmt::Write;
    const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
    for chunk in input.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
        let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
        let n = (b0 << 16) | (b1 << 8) | b2;
        let _ = write!(out, "{}", CHARS[(n >> 18 & 63) as usize] as char);
        let _ = write!(out, "{}", CHARS[(n >> 12 & 63) as usize] as char);
        if chunk.len() > 1 {
            let _ = write!(out, "{}", CHARS[(n >> 6 & 63) as usize] as char);
        } else {
            out.push('=');
        }
        if chunk.len() > 2 {
            let _ = write!(out, "{}", CHARS[(n & 63) as usize] as char);
        } else {
            out.push('=');
        }
    }
    out
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Focus {
    Commands,
    Output,
}

/// A tab grouping commands from a single source.
pub struct SourceTab {
    pub name: String,
    pub commands: Vec<SourceCommand>,
}

// qual:allow(coupling) reason: "app↔ui cycle is intentional TUI pattern — App owns state, ui renders it; decoupling would require a separate state type"
pub struct App {
    pub workspace: PathBuf,
    pub sources: Vec<Box<dyn CommandSource>>,
    pub tabs: Vec<SourceTab>,
    pub active_tab: usize,
    pub selected: usize,
    pub output: Vec<String>,
    pub task: Option<RunningTask>,
    pub exit_code: Option<i32>,
    pub should_quit: bool,
    pub flash_message: Option<(String, std::time::Instant)>,
    pub focus: Focus,
    pub output_scroll: u16,
    pub output_height: u16,
    pub git_status: Option<GitStatus>,
    pub search: Option<SearchState>,
    pub show_status_tab: bool,
    pub pipeline: Option<Pipeline>,
    pub run_start: Option<std::time::Instant>,
    /// When `Some`, the app is in args-input mode. The string accumulates typed args.
    pub args_input: Option<String>,
}

impl App {
    pub fn new(workspace: PathBuf) -> Self {
        let sources = all_sources();
        let git_status = status::collect_git_status(&workspace);

        let mut app = Self {
            workspace,
            sources,
            tabs: Vec::new(),
            active_tab: 0,
            selected: 0,
            output: Vec::new(),
            task: None,
            exit_code: None,
            should_quit: false,
            flash_message: None,
            focus: Focus::Commands,
            output_scroll: 0,
            output_height: 0,
            git_status,
            search: None,
            show_status_tab: false,
            pipeline: None,
            run_start: None,
            args_input: None,
        };
        app.discover_all();
        app
    }

    fn discover_all(&mut self) {
        self.tabs.clear();
        for source in &self.sources {
            match source.discover(&self.workspace) {
                Ok(cmds) if !cmds.is_empty() => {
                    self.tabs.push(SourceTab {
                        name: source.name().to_string(),
                        commands: cmds,
                    });
                }
                _ => {}
            }
        }
        self.active_tab = 0;
        self.selected = 0;
    }

    /// Returns the commands in the currently active tab.
    pub fn current_commands(&self) -> &[SourceCommand] {
        self.tabs
            .get(self.active_tab)
            .map(|t| t.commands.as_slice())
            .unwrap_or(&[])
    }

    /// Returns the currently selected command, if any.
    pub fn selected_command(&self) -> Option<&SourceCommand> {
        self.current_commands().get(self.selected)
    }

    pub fn total_command_count(&self) -> usize {
        self.tabs.iter().map(|t| t.commands.len()).sum()
    }

    pub fn next(&mut self) {
        let len = self.current_commands().len();
        if len > 0 && self.selected + 1 < len {
            self.selected += 1;
        }
    }

    pub fn previous(&mut self) {
        self.selected = self.selected.saturating_sub(1);
    }

    fn next_tab(&mut self) {
        if !self.tabs.is_empty() {
            self.active_tab = (self.active_tab + 1) % self.tabs.len();
            self.selected = 0;
        }
    }

    fn prev_tab(&mut self) {
        if !self.tabs.is_empty() {
            self.active_tab = self
                .active_tab
                .checked_sub(1)
                .unwrap_or(self.tabs.len() - 1);
            self.selected = 0;
        }
    }

    fn switch_tab(&mut self, idx: usize) {
        if idx < self.tabs.len() {
            self.active_tab = idx;
            self.selected = 0;
        }
    }

    fn scroll_output_down(&mut self) {
        let max = (self.output.len() as u16).saturating_sub(self.output_height);
        if self.output_scroll < max {
            self.output_scroll += 1;
        }
    }

    fn scroll_output_up(&mut self) {
        self.output_scroll = self.output_scroll.saturating_sub(1);
    }

    fn scroll_output_to_bottom(&mut self) {
        let max = (self.output.len() as u16).saturating_sub(self.output_height);
        self.output_scroll = max;
    }

    async fn run_selected(&mut self) -> Result<()> {
        if self.task.is_some() {
            return Ok(());
        }
        let Some(mut cmd) = self.selected_command().cloned() else {
            return Ok(());
        };
        // Append any pending args from args-input mode.
        if let Some(args) = self.args_input.take() {
            let args = args.trim().to_string();
            if !args.is_empty() {
                cmd.name = format!("{} {args}", cmd.name);
            }
        }
        self.output.clear();
        self.output_scroll = 0;
        self.exit_code = None;
        self.run_start = Some(std::time::Instant::now());
        let task = runner::run_source_command(&self.workspace, &cmd).await?;
        self.task = Some(task);
        Ok(())
    }

    fn start_args_input(&mut self) {
        if self.task.is_none() && self.selected_command().is_some() {
            self.args_input = Some(String::new());
        }
    }

    /// Handle a keypress while in args-input mode.
    /// Returns `true` if the user confirmed (Enter) and the command should run.
    fn handle_args_input(&mut self, code: KeyCode) -> bool {
        match code {
            KeyCode::Enter => return true,
            KeyCode::Char(ch) => {
                if let Some(ref mut buf) = self.args_input {
                    buf.push(ch);
                }
            }
            KeyCode::Backspace => {
                if let Some(ref mut buf) = self.args_input {
                    buf.pop();
                }
            }
            KeyCode::Esc => {
                self.args_input = None;
            }
            _ => {}
        }
        false
    }

    async fn cancel(&mut self) {
        if let Some(ref mut task) = self.task {
            task.cancel().await;
        }
        self.task = None;
        self.pipeline = None;
    }

    fn poll_output(&mut self) {
        let mut finished = None;
        if let Some(ref mut task) = self.task {
            let prev_len = self.output.len();
            task.poll_lines(&mut self.output);
            if self.output.len() > OUTPUT_LINE_LIMIT {
                self.output.drain(..OUTPUT_TRIM_SIZE);
            }
            if self.output.len() != prev_len && self.focus == Focus::Commands {
                let max = (self.output.len() as u16).saturating_sub(self.output_height);
                self.output_scroll = max;
            }
            if let Some(code) = task.try_exit_code() {
                finished = Some(code);
            }
        }
        if let Some(code) = finished {
            self.exit_code = Some(code);
            self.task = None;
            self.on_command_finished(code);
        }
    }

    fn on_command_finished(&mut self, exit_code: i32) {
        // Save history
        let cmd = self.selected_command().cloned();
        if let Some(cmd) = cmd {
            let duration = self.run_start.map(|s| s.elapsed().as_secs()).unwrap_or(0);
            let project_name = self
                .workspace
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_default();
            let entry = HistoryEntry {
                command: cmd.name.clone(),
                source: cmd.source.clone(),
                exit_code,
                timestamp: String::new(),
                duration_secs: duration,
            };
            let base = history::history_dir();
            let _ = history::save_entry(&base, &project_name, &entry);
            let _ = history::save_output(&base, &project_name, &cmd.name, &self.output);
            let _ = history::prune_logs(&base, &project_name);
        }
        self.run_start = None;

        // Advance pipeline if active
        if let Some(ref mut pipe) = self.pipeline {
            pipe.advance(exit_code);
            // If pipeline is still running, we need to start the next step
            // This will be handled in the main loop
        }
    }

    /// If a pipeline is active and the current step finished, start the next one.
    async fn advance_pipeline(&mut self) -> Result<()> {
        let should_start_next = if let Some(ref pipe) = self.pipeline {
            self.task.is_none() && pipe.is_active()
        } else {
            false
        };

        if should_start_next
            && let Some(ref pipe) = self.pipeline
            && let Some(step) = pipe.current_step()
        {
            let cmd = SourceCommand {
                name: step.name.clone(),
                source: step.source.clone(),
                description: None,
            };
            self.output
                .push(format!("--- [pipeline] running: {} ---", cmd.name));
            self.run_start = Some(std::time::Instant::now());
            let task = runner::run_source_command(&self.workspace, &cmd).await?;
            self.task = Some(task);
        }
        Ok(())
    }

    fn copy_output(&mut self) {
        use std::io::Write;
        if self.output.is_empty() {
            return;
        }
        let text = self.output.join("\n");
        let encoded = base64_encode(text.as_bytes());
        let _ = write!(io::stdout(), "\x1b]52;c;{encoded}\x07");
        let _ = io::stdout().flush();
        let lines = self.output.len();
        self.flash_message = Some((
            format!("Copied {lines} lines to clipboard"),
            std::time::Instant::now(),
        ));
    }

    fn refresh_commands(&mut self) {
        self.discover_all();
        self.git_status = status::collect_git_status(&self.workspace);
    }

    fn start_search(&mut self) {
        self.search = Some(SearchState::from_input());
        self.focus = Focus::Output;
    }

    fn handle_search_input(&mut self, code: KeyCode) {
        let Some(ref mut search) = self.search else {
            return;
        };
        match code {
            KeyCode::Enter => {
                search.query = search.input_buffer.clone();
                search.find_matches(&self.output);
                if let Some(line) = search.current_line() {
                    self.output_scroll = line as u16;
                }
            }
            KeyCode::Char(ch) => {
                search.input_buffer.push(ch);
            }
            KeyCode::Backspace => {
                search.input_buffer.pop();
            }
            KeyCode::Esc => {
                self.search = None;
            }
            _ => {}
        }
    }

    fn search_next(&mut self) {
        if let Some(ref mut search) = self.search
            && let Some(line) = search.next_match()
        {
            self.output_scroll = line as u16;
        }
    }

    fn search_prev(&mut self) {
        if let Some(ref mut search) = self.search
            && let Some(line) = search.prev_match()
        {
            self.output_scroll = line as u16;
        }
    }

    fn start_pipeline_from_selected(&mut self) {
        // Build a pipeline from all commands in the current tab
        let steps: Vec<PipelineStep> = self
            .current_commands()
            .iter()
            .map(|c| PipelineStep {
                name: c.name.clone(),
                source: c.source.clone(),
            })
            .collect();
        if steps.is_empty() {
            return;
        }
        let mut pipe = Pipeline::new(steps);
        pipe.start();
        self.pipeline = Some(pipe);
        self.output.clear();
        self.output_scroll = 0;
        self.exit_code = None;
    }

    pub async fn run(&mut self) -> Result<()> {
        enable_raw_mode()?;
        let mut stdout = io::stdout();
        execute!(stdout, EnterAlternateScreen)?;
        let backend = CrosstermBackend::new(stdout);
        let mut terminal = Terminal::new(backend)?;

        while !self.should_quit {
            if let Some((_, at)) = &self.flash_message
                && at.elapsed() > std::time::Duration::from_secs(2)
            {
                self.flash_message = None;
            }

            terminal.draw(|frame| ui::draw(frame, &mut *self))?;
            self.poll_output();
            self.advance_pipeline().await?;

            if event::poll(std::time::Duration::from_millis(EVENT_POLL_MS))?
                && let Event::Key(key) = event::read()?
            {
                if key.kind != KeyEventKind::Press {
                    continue;
                }

                // If in args-input mode, route all keys there.
                if self.args_input.is_some() {
                    if self.handle_args_input(key.code) {
                        self.run_selected().await?;
                    }
                    continue;
                }

                // If in search input mode, route all keys to search handler.
                if self
                    .search
                    .as_ref()
                    .is_some_and(|s| s.query.is_empty() && s.active)
                {
                    self.handle_search_input(key.code);
                    continue;
                }

                match key.code {
                    KeyCode::Char('q') => {
                        self.cancel().await;
                        self.should_quit = true;
                    }
                    KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        if self.task.is_some() {
                            self.cancel().await;
                        } else {
                            self.cancel().await;
                            self.should_quit = true;
                        }
                    }
                    KeyCode::Esc => {
                        if self.search.is_some() {
                            self.search = None;
                        } else if self.task.is_some() {
                            self.cancel().await;
                        } else if self.focus == Focus::Output {
                            self.focus = Focus::Commands;
                        }
                    }
                    KeyCode::Tab | KeyCode::BackTab => {
                        if self.focus == Focus::Commands {
                            // Tab cycles source tabs when in Commands focus
                            if key.code == KeyCode::Tab {
                                self.next_tab();
                            } else {
                                self.prev_tab();
                            }
                        } else {
                            self.focus = Focus::Commands;
                        }
                    }
                    KeyCode::Char(ch @ '1'..='9') if self.focus == Focus::Commands => {
                        let idx = (ch as usize) - ('1' as usize);
                        self.switch_tab(idx);
                    }
                    KeyCode::Char('j') | KeyCode::Down => match self.focus {
                        Focus::Commands => self.next(),
                        Focus::Output => self.scroll_output_down(),
                    },
                    KeyCode::Char('k') | KeyCode::Up => match self.focus {
                        Focus::Commands => self.previous(),
                        Focus::Output => self.scroll_output_up(),
                    },
                    KeyCode::Char('g') if self.focus == Focus::Output => {
                        self.output_scroll = 0;
                    }
                    KeyCode::Char('G') if self.focus == Focus::Output => {
                        self.scroll_output_to_bottom();
                    }
                    KeyCode::Enter => {
                        if self.focus == Focus::Output {
                            self.focus = Focus::Commands;
                        } else {
                            self.run_selected().await?;
                        }
                    }
                    KeyCode::Char('a') if self.focus == Focus::Commands => {
                        self.start_args_input();
                    }
                    KeyCode::Char('r') => self.refresh_commands(),
                    KeyCode::Char('c') => self.copy_output(),
                    KeyCode::Char('s') => {
                        self.show_status_tab = !self.show_status_tab;
                        if self.show_status_tab {
                            self.focus = Focus::Output;
                        }
                    }
                    KeyCode::Char('/') => self.start_search(),
                    KeyCode::Char('n') if self.focus == Focus::Output => self.search_next(),
                    KeyCode::Char('N') if self.focus == Focus::Output => self.search_prev(),
                    KeyCode::Char('o') => {
                        self.focus = Focus::Output;
                    }
                    KeyCode::Char('P') => self.start_pipeline_from_selected(),
                    _ => {}
                }
            }
        }

        disable_raw_mode()?;
        execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_app_navigate() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        // Should have at least xtask and cargo tabs
        assert!(!app.tabs.is_empty());
        let len = app.current_commands().len();
        assert!(len > 0);
        app.next();
        if len > 1 {
            assert_eq!(app.selected, 1);
        }
        app.previous();
        assert_eq!(app.selected, 0);
    }

    #[test]
    fn test_tab_switching() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        if app.tabs.len() >= 2 {
            assert_eq!(app.active_tab, 0);
            app.next_tab();
            assert_eq!(app.active_tab, 1);
            assert_eq!(app.selected, 0);
            app.prev_tab();
            assert_eq!(app.active_tab, 0);
        }
    }

    #[test]
    fn test_total_command_count() {
        let app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        let total = app.total_command_count();
        // At minimum: 4 cargo commands + some xtask commands
        assert!(total >= 4);
    }

    #[test]
    fn test_switch_tab_by_index() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        if app.tabs.len() >= 2 {
            app.switch_tab(1);
            assert_eq!(app.active_tab, 1);
            assert_eq!(app.selected, 0);
            // Out-of-bounds index is a no-op
            app.switch_tab(999);
            assert_eq!(app.active_tab, 1);
        }
    }

    #[test]
    fn test_sources_grouped_by_tab() {
        let app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        // Each tab should have a unique source name
        let names: Vec<&str> = app.tabs.iter().map(|t| t.name.as_str()).collect();
        let mut deduped = names.clone();
        deduped.sort();
        deduped.dedup();
        assert_eq!(names.len(), deduped.len(), "duplicate tab names: {names:?}");
        // xtui has Cargo.toml so cargo tab must exist
        assert!(names.contains(&"cargo"), "missing cargo tab in {names:?}");
    }

    #[test]
    fn test_selected_command_returns_correct_source() {
        let app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        if let Some(cmd) = app.selected_command() {
            let tab_name = &app.tabs[app.active_tab].name;
            assert_eq!(&cmd.source, tab_name);
        }
    }

    #[test]
    fn test_scroll_bounds() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        app.output_height = 10;
        // No output — scroll should stay at 0
        app.scroll_output_down();
        assert_eq!(app.output_scroll, 0);
        // Add output exceeding height
        app.output = (0..25).map(|i| format!("line {i}")).collect();
        app.scroll_output_to_bottom();
        assert_eq!(app.output_scroll, 15); // 25 - 10
        app.scroll_output_up();
        assert_eq!(app.output_scroll, 14);
        // Scroll up past 0 saturates
        app.output_scroll = 0;
        app.scroll_output_up();
        assert_eq!(app.output_scroll, 0);
    }

    #[test]
    fn test_search_integration() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        app.output = vec!["hello world".into(), "foo bar".into(), "hello again".into()];
        app.start_search();
        assert!(app.search.is_some());
        assert_eq!(app.focus, Focus::Output);
        // Type query
        app.handle_search_input(KeyCode::Char('h'));
        app.handle_search_input(KeyCode::Char('e'));
        app.handle_search_input(KeyCode::Char('l'));
        app.handle_search_input(KeyCode::Enter);
        let search = app.search.as_ref().unwrap();
        assert_eq!(search.match_count(), 2);
        // Cycling
        app.search_next();
        assert_eq!(app.output_scroll, 2);
        app.search_prev();
        assert_eq!(app.output_scroll, 0);
    }

    #[test]
    fn test_pipeline_construction() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        assert!(!app.current_commands().is_empty());
        app.start_pipeline_from_selected();
        assert!(app.pipeline.is_some());
        let pipe = app.pipeline.as_ref().unwrap();
        assert_eq!(pipe.step_count(), app.current_commands().len());
    }

    #[test]
    fn test_focus_toggle() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        assert_eq!(app.focus, Focus::Commands);
        app.focus = Focus::Output;
        assert_eq!(app.focus, Focus::Output);
    }

    #[test]
    fn test_base64_encode_rfc4648_vectors() {
        assert_eq!(base64_encode(b""), "");
        assert_eq!(base64_encode(b"f"), "Zg==");
        assert_eq!(base64_encode(b"fo"), "Zm8=");
        assert_eq!(base64_encode(b"foo"), "Zm9v");
        assert_eq!(base64_encode(b"foob"), "Zm9vYg==");
        assert_eq!(base64_encode(b"fooba"), "Zm9vYmE=");
        assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy");
    }

    #[test]
    fn test_flash_message() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        assert!(app.flash_message.is_none());
        app.flash_message = Some(("test".into(), std::time::Instant::now()));
        assert!(app.flash_message.is_some());
    }

    #[test]
    fn test_args_input_start_and_cancel() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        assert!(app.args_input.is_none());
        app.start_args_input();
        assert_eq!(app.args_input.as_deref(), Some(""));
        // Esc cancels without running
        app.handle_args_input(KeyCode::Esc);
        assert!(app.args_input.is_none());
    }

    #[test]
    fn test_args_input_typing() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        app.start_args_input();
        app.handle_args_input(KeyCode::Char('-'));
        app.handle_args_input(KeyCode::Char('-'));
        app.handle_args_input(KeyCode::Char('v'));
        assert_eq!(app.args_input.as_deref(), Some("--v"));
        app.handle_args_input(KeyCode::Backspace);
        assert_eq!(app.args_input.as_deref(), Some("--"));
    }

    #[test]
    fn test_args_input_enter_returns_true() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        app.start_args_input();
        app.handle_args_input(KeyCode::Char('-'));
        let should_run = app.handle_args_input(KeyCode::Enter);
        assert!(should_run);
        // args_input is still Some at this point (consumed by run_selected)
        assert_eq!(app.args_input.as_deref(), Some("-"));
    }

    #[test]
    fn test_start_args_input_no_op_while_running() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        // Simulate a running task by setting task to None but checking guard
        // (can't spawn a real task in a sync test; verify guard via task = None path)
        app.start_args_input();
        assert!(app.args_input.is_some());
    }

    #[test]
    fn test_refresh_commands_preserves_structure() {
        let mut app = App::new(PathBuf::from(env!("CARGO_MANIFEST_DIR")));
        let tab_count = app.tabs.len();
        let total = app.total_command_count();
        app.refresh_commands();
        assert_eq!(app.tabs.len(), tab_count);
        assert_eq!(app.total_command_count(), total);
    }
}