cargo-port 0.1.3

A TUI for inspecting and managing Rust projects
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
use std::collections::HashSet;
use std::io;
use std::io::BufReader;
use std::io::Read;
use std::io::Stdout;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::Command;
use std::process::ExitCode;
use std::process::Stdio;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use std::time::Instant;

use crossterm::event::DisableFocusChange;
use crossterm::event::DisableMouseCapture;
use crossterm::event::EnableFocusChange;
use crossterm::event::EnableMouseCapture;
use crossterm::event::Event;
use crossterm::execute;
use crossterm::terminal::EnterAlternateScreen;
use crossterm::terminal::LeaveAlternateScreen;
use crossterm::terminal::disable_raw_mode;
use crossterm::terminal::enable_raw_mode;
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
#[cfg(not(unix))]
use sysinfo::Pid;
#[cfg(not(unix))]
use sysinfo::ProcessRefreshKind;
#[cfg(not(unix))]
use sysinfo::ProcessesToUpdate;
#[cfg(not(unix))]
use sysinfo::Signal;
#[cfg(not(unix))]
use sysinfo::System;
use terminal_colorsaurus::QueryOptions;
use terminal_colorsaurus::ThemeMode;
use tui_pane::Appearance;
use tui_pane::SLOW_FRAME_MS;
use tui_pane::TrackedItemKey;

use super::app::App;
use super::app::PendingClean;
use super::app::PollBackgroundStats;
use super::constants::CARGO_BENCH_FLAG;
use super::constants::CARGO_BENCH_SUBCOMMAND;
use super::constants::CARGO_CLEAN_SUBCOMMAND;
use super::constants::CARGO_COLOR_ALWAYS_FLAG;
use super::constants::CARGO_EXAMPLE_FLAG;
use super::constants::CARGO_FEATURES_FLAG;
use super::constants::CARGO_PACKAGE_FLAG;
use super::constants::CARGO_RELEASE_FLAG;
use super::constants::CARGO_RUN_SUBCOMMAND;
use super::constants::PERF_LOG_FILE;
use super::constants::PREVIOUS_PERF_LOG_FILE;
use super::input;
use super::panes::CiFetchKind;
use super::panes::PendingCiFetch;
use super::panes::PendingExampleRun;
use super::panes::RunTargetKind;
use super::project_list::ExpandTarget;
use super::render;
use super::settings;
use crate::channel;
use crate::channel::Receiver;
use crate::channel::Select;
use crate::channel::Sender;
use crate::channel::TryRecvError;
use crate::ci;
use crate::config;
use crate::constants::CARGO_COMMAND_NAME;
use crate::http::HttpClient;
use crate::project::AbsolutePath;
use crate::project::RootItem;
use crate::project::WorkspaceMetadataStore;
use crate::scan;
use crate::scan::BackgroundMsg;
use crate::scan::CiFetchResult;

pub(super) enum ExampleMsg {
    Output(String),
    /// Carriage-return line — replaces the last output line (cargo progress).
    Progress(String),
    Finished,
}

/// Message sent when a background CI fetch completes.
pub(super) enum CiFetchMsg {
    /// The fetch completed with updated runs for the given project path.
    Complete {
        path:   String,
        result: CiFetchResult,
        kind:   CiFetchKind,
    },
}

pub(super) enum CleanMsg {
    Finished(AbsolutePath),
}

#[derive(Clone, Copy)]
struct FrameMetrics {
    frame_elapsed:       Duration,
    input_elapsed:       Duration,
    bg_elapsed:          Duration,
    cpu_elapsed:         Duration,
    run_targets_elapsed: Duration,
    rows_elapsed:        Duration,
    disk_elapsed:        Duration,
    fit_elapsed:         Duration,
    detail_elapsed:      Duration,
    draw_elapsed:        Duration,
    input_count:         usize,
}

fn setup_terminal() -> io::Result<Terminal<CrosstermBackend<Stdout>>> {
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    rearm_input_modes()?;
    let backend = CrosstermBackend::new(stdout);
    Terminal::new(backend)
}

pub(super) fn rearm_input_modes() -> io::Result<()> {
    execute!(io::stdout(), EnableMouseCapture, EnableFocusChange)
}

/// Probe the terminal for its actual background appearance via an OSC 11
/// query. Returns `None` when the terminal doesn't answer —
/// `terminal-colorsaurus` fails fast for terminals that don't support the
/// query, so this doesn't block on the timeout in that case. Call this in
/// the window after raw mode is enabled but before the input thread
/// starts, so the query response isn't consumed by `crossterm::event::read`.
fn detect_terminal_appearance() -> Option<Appearance> {
    match terminal_colorsaurus::theme_mode(QueryOptions::default()) {
        Ok(ThemeMode::Dark) => Some(Appearance::Dark),
        Ok(ThemeMode::Light) => Some(Appearance::Light),
        Err(err) => {
            tracing::debug!(error = %err, "terminal background detection unavailable");
            None
        },
    }
}

fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> io::Result<()> {
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture,
        DisableFocusChange
    )?;
    Ok(())
}

/// Report a fatal startup/teardown error to both the perf log and
/// stderr. The tracing subscriber writes to a temp file (see
/// [`tui_pane::init_perf_log`]), not the terminal, so a bare
/// `tracing::error!` exits silently from the user's point of view.
/// Echoing to stderr ensures the console shows *something* before the
/// process exits. Call only after the terminal has been restored to
/// the normal screen.
fn report_fatal(message: &str) {
    tracing::error!("{message}");
    eprintln!("cargo-port: {message}");
}

pub fn run() -> ExitCode {
    let startup_settings = match settings::load_cargo_port_settings_for_startup() {
        Ok(settings) => settings,
        Err(err) => {
            report_fatal(&err);
            return ExitCode::FAILURE;
        },
    };
    let cfg = startup_settings.config.clone();
    config::set_active_config(&cfg);
    let perf_log_path = std::env::temp_dir().join(PERF_LOG_FILE);
    let previous_perf_log_path = std::env::temp_dir().join(PREVIOUS_PERF_LOG_FILE);
    tui_pane::init_perf_log(&perf_log_path, &previous_perf_log_path);

    let Ok(rt) = tokio::runtime::Runtime::new() else {
        report_fatal("failed to create async runtime");
        return ExitCode::FAILURE;
    };
    let Some(http_client) = HttpClient::new(rt.handle().clone()) else {
        report_fatal("failed to create HTTP client");
        return ExitCode::FAILURE;
    };
    let scan_started_at = std::time::Instant::now();
    tracing::trace!(
        target: tui_pane::PERF_LOG_TARGET,
        kind = "initial",
        run = 1,
        "scan_start"
    );
    let scan_dirs = scan::resolve_include_dirs(&cfg.tui.include_dirs);
    let metadata_store = Arc::new(Mutex::new(WorkspaceMetadataStore::new()));
    let (background_tx, background_rx) = scan::spawn_streaming_scan(
        scan_dirs,
        &cfg.tui.inline_dirs,
        cfg.tui.include_non_rust,
        http_client.clone(),
        Arc::clone(&metadata_store),
    );
    let appearance_tx = background_tx.clone();
    tui_pane::spawn_appearance_poller(rt.handle(), move |appearance| {
        let _ = appearance_tx.send(scan::BackgroundMsg::AppearanceChanged(appearance));
    });
    let projects: Vec<RootItem> = Vec::new();

    let original_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |panic_info| {
        let _ = disable_raw_mode();
        let _ = execute!(
            io::stdout(),
            LeaveAlternateScreen,
            DisableMouseCapture,
            DisableFocusChange
        );
        original_hook(panic_info);
    }));

    let mut terminal = match setup_terminal() {
        Ok(t) => t,
        Err(e) => {
            report_fatal(&format!("failed to initialize terminal: {e}"));
            return ExitCode::FAILURE;
        },
    };

    let mut app = match App::new(
        &projects,
        background_tx,
        background_rx,
        startup_settings,
        http_client,
        scan_started_at,
        metadata_store,
    ) {
        Ok(app) => app,
        Err(e) => {
            let _ = restore_terminal(&mut terminal);
            report_fatal(&format!("failed to initialize app: {e:#}"));
            return ExitCode::FAILURE;
        },
    };
    tracing::info!(perf_log = %perf_log_path.display(), "tui_ready");
    // Probe the terminal background while no thread is reading input yet,
    // so the OSC 11 response can't be swallowed by `spawn_input_thread`.
    app.set_terminal_appearance(detect_terminal_appearance());
    let input_rx = spawn_input_thread();

    let result = event_loop(&mut terminal, &mut app, &input_rx);

    // Persist tree UI state on exit — including a restart, so the relaunched
    // process restores the same selection and expansions.
    save_tree_state(&app);

    let should_restart = app.framework.restart_requested();
    let _ = restore_terminal(&mut terminal);

    if should_restart {
        restart_self();
    }

    let status = match result {
        Ok(()) => 0,
        Err(e) => {
            report_fatal(&format!("{e}"));
            1
        },
    };
    std::process::exit(status);
}

/// Replace the current process with a fresh instance of the same binary.
fn restart_self() {
    let exe = AbsolutePath::from(
        std::env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("cargo-port")),
    );
    let args: Vec<String> = std::env::args().skip(1).collect();

    #[cfg(unix)]
    {
        let err = std::process::Command::new(exe.as_path()).args(&args).exec();
        tracing::error!("Failed to restart: {err}");
    }

    #[cfg(windows)]
    {
        match std::process::Command::new(exe.as_path())
            .args(&args)
            .spawn()
        {
            Ok(_) => std::process::exit(0),
            Err(err) => tracing::error!("Failed to restart: {err}"),
        }
    }
}

fn spawn_input_thread() -> Receiver<Event> {
    let (tx, rx) = channel::unbounded();
    thread::spawn(move || {
        while let Ok(event) = crossterm::event::read() {
            if tx.send(event).is_err() {
                break;
            }
        }
    });
    rx
}

/// Outcome of draining the input channel for one frame.
struct InputDrain {
    count:        usize,
    elapsed:      Duration,
    /// The input thread dropped its sender (a crossterm read error ended
    /// `spawn_input_thread`). A TUI that can no longer read input is
    /// dead, so the loop exits. The event-driven design relies on
    /// detecting this: `Select` reports a *disconnected* crossbeam
    /// receiver as permanently ready, so without this guard the loop
    /// would busy-spin at 100% CPU on the dead input channel (PD8).
    disconnected: bool,
}

/// Event-driven render loop.
///
/// Each iteration drains every ready source, renders one frame, then
/// blocks in [`wait_for_event`] until something happens — input, a
/// background message, a new CPU sample, or the animation heartbeat. The
/// full drain runs on *every* wake regardless of which channel fired, so
/// the mtime-polled config/keymap/theme reload in [`poll_background_frame`]
/// and the 1s running-targets poll stay alive while idle (PD1).
///
/// Loop contracts (PD9): quit/restart are set only from input dispatch,
/// which is a `Select` source — so a request always wakes the loop. The
/// first frame is drawn before the first block, then scan/CPU wakes
/// update it; if a future background handler sets quit, it must also be a
/// `Select` source or it will not wake the loop.
fn event_loop(
    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
    app: &mut App,
    input_rx: &Receiver<Event>,
) -> io::Result<()> {
    let mut rearmed_after_first_draw = false;
    loop {
        let frame_started = Instant::now();

        let input = process_input_frame(app, input_rx);
        if input.disconnected {
            tracing::error!("input channel disconnected; exiting event loop");
            return Ok(());
        }
        if app.framework.quit_requested() || app.framework.restart_requested() {
            return Ok(());
        }

        let (bg_stats, bg_elapsed) = poll_background_frame(app);
        let tick_now = Instant::now();
        let cpu_elapsed = measure(|| app.panes.cpu_tick());
        let run_targets_elapsed = measure(|| app.running_targets_tick(tick_now));
        app.scan.prune_shimmers(tick_now);

        let rows_elapsed = measure(|| app.ensure_visible_rows_cached());
        let disk_elapsed = measure(|| app.ensure_disk_cache());
        let fit_elapsed = measure(|| app.ensure_fit_widths_cached());
        let detail_elapsed = measure(|| app.ensure_detail_cached());
        let draw_elapsed = draw_frame(terminal, app)?;
        if !rearmed_after_first_draw {
            let _ = rearm_input_modes();
            rearmed_after_first_draw = true;
        }

        if app.framework.quit_requested() || app.framework.restart_requested() {
            flush_pending_selection(app);
            break;
        }

        spawn_pending_background_tasks(app);
        log_slow_frame(
            app,
            &bg_stats,
            &FrameMetrics {
                frame_elapsed: frame_started.elapsed(),
                input_elapsed: input.elapsed,
                bg_elapsed,
                cpu_elapsed,
                run_targets_elapsed,
                rows_elapsed,
                disk_elapsed,
                fit_elapsed,
                detail_elapsed,
                draw_elapsed,
                input_count: input.count,
            },
        );

        // Block until the next event or animation tick. `frame_ms` above
        // measures real work only — the wait is deliberately excluded.
        wait_for_event(app, input_rx);
    }
    Ok(())
}

/// Block until one of the render-loop's channels is ready, or until the
/// animation heartbeat elapses. [`Select::ready_timeout`] signals
/// readiness *without consuming* — the per-source drain in [`event_loop`]
/// does the receiving and runs in full on every wake.
///
/// The `Select` is rebuilt every call because `swap_background_channel`
/// (rescan) replaces the background receiver wholesale. The CPU-sample
/// receiver is registered only while the monitor is sampling: a failed
/// worker spawn leaves the sample sender dropped, and a disconnected
/// crossbeam receiver is reported permanently ready, which would
/// busy-spin the loop (PD8). The four App-held background senders never
/// disconnect (App keeps a clone of each), so only input and CPU samples
/// are at risk; input disconnect is handled in [`process_input_frame`].
fn wait_for_event(app: &App, input_rx: &Receiver<Event>) {
    let timeout = app.animation_timeout();
    let mut select = Select::new();
    select.recv(input_rx);
    select.recv(app.background.background_receiver());
    select.recv(app.background.ci_fetch_rx());
    select.recv(app.background.clean_rx());
    select.recv(app.background.example_rx());
    if app.panes.cpu.is_sampling() {
        select.recv(app.panes.cpu.sample_rx());
    }
    // The fired index is ignored: the loop body drains every source.
    let _ = select.ready_timeout(timeout);
}

fn process_input_frame(app: &mut App, input_rx: &Receiver<Event>) -> InputDrain {
    let started = Instant::now();
    let mut count = 0usize;
    let mut disconnected = false;
    loop {
        match input_rx.try_recv() {
            Ok(event) => {
                count += 1;
                tracing::trace!(
                    target: tui_pane::PERF_LOG_TARGET,
                    event = %tui_pane::event_label(&event),
                    "input_event_received"
                );
                input::handle_event(app, &event);
                if app.framework.quit_requested() || app.framework.restart_requested() {
                    break;
                }
            },
            Err(TryRecvError::Empty) => break,
            Err(TryRecvError::Disconnected) => {
                disconnected = true;
                break;
            },
        }
    }
    if count == 0 {
        flush_deferred_selection(app);
    }
    InputDrain {
        count,
        elapsed: started.elapsed(),
        disconnected,
    }
}

fn flush_deferred_selection(app: &mut App) {
    if app.project_list.sync().is_changed() {
        save_tree_state(app);
        app.project_list.mark_sync_stable();
    }
}

fn flush_pending_selection(app: &App) {
    if app.project_list.sync().is_changed() {
        save_tree_state(app);
    }
}

fn poll_background_frame(app: &mut App) -> (PollBackgroundStats, Duration) {
    let started = Instant::now();
    app.maybe_reload_config_from_disk();
    app.maybe_reload_keymap_from_disk();
    app.maybe_reload_themes_from_disk();
    let stats = app.poll_background();
    app.tick_startup_panel();
    (stats, started.elapsed())
}

fn measure(action: impl FnOnce()) -> Duration {
    let started = Instant::now();
    action();
    started.elapsed()
}

fn draw_frame(
    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
    app: &mut App,
) -> io::Result<Duration> {
    let started = Instant::now();
    terminal.draw(|frame| render::ui(frame, app))?;
    Ok(started.elapsed())
}

fn spawn_pending_background_tasks(app: &mut App) {
    if let Some(run) = app.inflight.take_pending_example_run() {
        spawn_example_process(app, &run);
    }

    if let Some(pending) = app.inflight.pending_cleans_mut().pop_front() {
        spawn_clean_process(app, &pending);
    }

    if let Some(fetch) = app.inflight.take_pending_ci_fetch() {
        let abs_path = AbsolutePath::from(Path::new(&fetch.project_path));
        if spawn_ci_fetch(app, &fetch) {
            app.ci.fetch_tracker.start(abs_path);
            app.scan.bump_generation();
        } else if let Some(task_id) = app.ci.take_fetch_toast() {
            let empty: HashSet<TrackedItemKey> = HashSet::new();
            app.framework.toasts.complete_missing_items(task_id, &empty);
        }
    }
}

fn log_slow_frame(app: &App, bg_stats: &PollBackgroundStats, metrics: &FrameMetrics) {
    if metrics.frame_elapsed.as_millis() < SLOW_FRAME_MS {
        return;
    }
    tracing::trace!(
        target: tui_pane::PERF_LOG_TARGET,
        frame_ms = tui_pane::perf_log_ms(metrics.frame_elapsed.as_millis()),
        input_ms = tui_pane::perf_log_ms(metrics.input_elapsed.as_millis()),
        bg_ms = tui_pane::perf_log_ms(metrics.bg_elapsed.as_millis()),
        cpu_ms = tui_pane::perf_log_ms(metrics.cpu_elapsed.as_millis()),
        run_targets_ms = tui_pane::perf_log_ms(metrics.run_targets_elapsed.as_millis()),
        rows_ms = tui_pane::perf_log_ms(metrics.rows_elapsed.as_millis()),
        disk_ms = tui_pane::perf_log_ms(metrics.disk_elapsed.as_millis()),
        fit_ms = tui_pane::perf_log_ms(metrics.fit_elapsed.as_millis()),
        detail_ms = tui_pane::perf_log_ms(metrics.detail_elapsed.as_millis()),
        draw_ms = tui_pane::perf_log_ms(metrics.draw_elapsed.as_millis()),
        input_count = metrics.input_count,
        bg_msgs = bg_stats.bg_msgs,
        disk_usage_msgs = bg_stats.disk_usage_msgs,
        git_info_msgs = bg_stats.git_info_msgs,
        lint_status_msgs = bg_stats.lint_status_msgs,
        language_progress_msgs = bg_stats.language_progress_msgs,
        ci_msgs = bg_stats.ci_msgs,
        example_msgs = bg_stats.example_msgs,
        tree_results = bg_stats.tree_results,
        fit_results = bg_stats.fit_results,
        disk_results = bg_stats.disk_results,
        needs_rebuild = bg_stats.needs_rebuild,
        items = app.project_list.len(),
        scan_complete = app.scan.is_complete(),
        "slow_frame"
    );
}

fn spawn_example_process(app: &mut App, run: &PendingExampleRun) {
    let mut cmd = Command::new(CARGO_COMMAND_NAME);
    match run.kind {
        RunTargetKind::Binary => {
            cmd.arg(CARGO_RUN_SUBCOMMAND);
        },
        RunTargetKind::Example => {
            cmd.arg(CARGO_RUN_SUBCOMMAND)
                .arg(CARGO_EXAMPLE_FLAG)
                .arg(&run.target_name);
        },
        RunTargetKind::Bench => {
            cmd.arg(CARGO_BENCH_SUBCOMMAND)
                .arg(CARGO_BENCH_FLAG)
                .arg(&run.target_name);
        },
    }
    if run.build_mode.is_release() {
        cmd.arg(CARGO_RELEASE_FLAG);
    }
    if let Some(pkg) = &run.package_name {
        cmd.arg(CARGO_PACKAGE_FLAG).arg(pkg);
    }
    // Cargo does not auto-enable a target's `required-features`, so a
    // feature-gated target (e.g. an example with `required-features`)
    // errors out unless we pass them ourselves.
    if !run.required_features.is_empty() {
        cmd.arg(CARGO_FEATURES_FLAG)
            .arg(run.required_features.join(","));
    }
    cmd.current_dir(&run.abs_path)
        .arg(CARGO_COLOR_ALWAYS_FLAG)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    isolate_example_process(&mut cmd);

    let mut child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) => {
            app.set_example_output(vec![format!("Failed to start: {e}")]);
            app.inflight
                .set_example_running(Some(run.target_name.clone()));
            return;
        },
    };

    // On Unix, `isolate_example_process` makes the cargo child PID the
    // process-group id too. `stop_example_process` uses it from the main
    // thread to terminate the whole launched run without signaling cargo-port.
    let pid = child.id();
    *app.inflight
        .example_child()
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(pid);

    let name = run.target_name.clone();
    let mode = run.build_mode.label();
    app.set_example_output(vec![format!("Building {name}{mode}...")]);
    app.inflight
        .set_example_running(Some(format!("{name}{mode}")));

    // Take ownership of pipes before moving child to thread
    let stderr = child.stderr.take();
    let stdout = child.stdout.take();

    let pid_holder = app.inflight.example_child();
    let tx = app.background.example_sender();
    thread::spawn(move || {
        let stderr_reader = stderr.map(|stream| {
            let tx = tx.clone();
            thread::spawn(move || read_with_progress(&tx, stream))
        });
        let stdout_reader = stdout.map(|stream| {
            let tx = tx.clone();
            thread::spawn(move || read_with_progress(&tx, stream))
        });

        // Wait for the child to finish and clear the PID.
        // Disk usage is updated automatically by the filesystem watcher.
        let _ = child.wait();
        if let Some(reader) = stderr_reader {
            let _ = reader.join();
        }
        if let Some(reader) = stdout_reader {
            let _ = reader.join();
        }
        *pid_holder
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = None;

        let _ = tx.send(ExampleMsg::Finished);
    });
}

#[cfg(unix)]
fn isolate_example_process(cmd: &mut Command) { cmd.process_group(0); }

#[cfg(not(unix))]
fn isolate_example_process(_cmd: &mut Command) {}

#[cfg(unix)]
pub(super) fn stop_example_process(pid: u32) -> bool {
    signal_with_kill("-TERM", format!("-{pid}")) || signal_with_kill("-TERM", pid.to_string())
}

#[cfg(unix)]
fn signal_with_kill(signal: &str, target: String) -> bool {
    Command::new("kill")
        .arg(signal)
        .arg(target)
        .status()
        .is_ok_and(|status| status.success())
}

#[cfg(not(unix))]
pub(super) fn stop_example_process(pid: u32) -> bool {
    let mut system = System::new();
    let pid = Pid::from_u32(pid);
    system.refresh_processes_specifics(
        ProcessesToUpdate::Some(&[pid]),
        true,
        ProcessRefreshKind::nothing(),
    );
    system
        .process(pid)
        .is_some_and(|process| process.kill_with(Signal::Term).unwrap_or(false))
}

/// Read a stream byte-by-byte, splitting on `\n` (new line) and `\r` (progress update).
/// `\r`-terminated chunks are sent as `Progress` so the UI replaces the last line.
fn read_with_progress(tx: &Sender<ExampleMsg>, stream: impl io::Read) {
    let mut reader = BufReader::new(stream);
    let mut buf = Vec::new();
    let mut byte = [0u8; 1];

    while reader.read_exact(&mut byte).is_ok() {
        match byte[0] {
            b'\n' => {
                let line = String::from_utf8_lossy(&buf).to_string();
                let _ = tx.send(ExampleMsg::Output(line));
                buf.clear();
            },
            b'\r' => {
                if !buf.is_empty() {
                    let line = String::from_utf8_lossy(&buf).to_string();
                    let _ = tx.send(ExampleMsg::Progress(line));
                    buf.clear();
                }
            },
            b => buf.push(b),
        }
    }
    // Flush any remaining data
    if !buf.is_empty() {
        let line = String::from_utf8_lossy(&buf).to_string();
        let _ = tx.send(ExampleMsg::Output(line));
    }
}

fn spawn_clean_process(app: &mut App, pending: &PendingClean) {
    let mut cmd = std::process::Command::new(CARGO_COMMAND_NAME);
    cmd.arg(CARGO_CLEAN_SUBCOMMAND)
        .current_dir(&pending.abs_path)
        .stdout(Stdio::null())
        .stderr(Stdio::null());

    let mut child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) => {
            app.clean_spawn_failed(&pending.abs_path);
            app.show_timed_toast("cargo clean failed", e.to_string());
            return;
        },
    };
    let tx = app.background.clean_sender();
    let abs_path = pending.abs_path.clone();
    thread::spawn(move || {
        let _ = child.wait();
        let _ = tx.send(CleanMsg::Finished(abs_path));
    });
}

fn spawn_ci_fetch(app: &App, fetch: &PendingCiFetch) -> bool {
    // Derive (repo_url, owner, repo) from local git info — no network needed.
    // Use `fetch_url_for` so a worktree without upstream tracking still resolves.
    let path = Path::new(&fetch.project_path);
    let Some(repo_url) = app.project_list.fetch_url_for(path) else {
        return false;
    };
    let Some(owner_repo) = ci::parse_owner_repo(&repo_url) else {
        return false;
    };

    let tx = app.background.ci_fetch_sender();
    let background_tx = app.background.background_sender();
    let client = app.net.http_client();
    let project_path = fetch.project_path.clone();
    let ci_run_count = fetch.ci_run_count;
    let oldest_created_at = fetch.oldest_created_at.clone();
    let kind = fetch.kind;
    let url = repo_url;

    thread::spawn(move || {
        let (result, network) = match kind {
            CiFetchKind::FetchOlder => {
                let oldest = oldest_created_at
                    .as_deref()
                    .unwrap_or("1970-01-01T00:00:00Z");
                scan::fetch_older_runs(
                    &client,
                    &url,
                    owner_repo.owner(),
                    owner_repo.repo(),
                    oldest,
                    ci_run_count,
                )
            },
            CiFetchKind::Sync => {
                let (result, _meta, signal) = scan::fetch_ci_runs_cached(
                    &client,
                    &url,
                    owner_repo.owner(),
                    owner_repo.repo(),
                    ci_run_count,
                );
                (result, signal)
            },
        };
        scan::emit_service_signal(&background_tx, network);
        let _ = tx.send(CiFetchMsg::Complete {
            path: project_path,
            result,
            kind,
        });
    });
    true
}

fn last_selected_path_file() -> AbsolutePath { scan::cache_dir().join("last_selected.txt").into() }

/// Read the pre-`tree_state.toml` selection file. Retained only to migrate the
/// last selection forward on the first launch after the upgrade.
fn load_last_selected() -> Option<AbsolutePath> {
    let path = last_selected_path_file();
    let raw = std::fs::read_to_string(&*path).ok()?;
    let trimmed = raw.trim();
    (!trimmed.is_empty() && Path::new(trimmed).is_absolute()).then(|| AbsolutePath::from(trimmed))
}

fn tree_state_file() -> AbsolutePath { scan::cache_dir().join("tree_state.toml").into() }

/// On-disk form of the project-tree UI state: the selected project and the set
/// of expanded containers. Each expanded entry is a tab-delimited token
/// (`kind\tpath[\tgroup]`) — see [`encode_expand_target`].
#[derive(serde::Serialize, serde::Deserialize, Default)]
struct TreeStateFile {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    selected: Option<String>,
    #[serde(default)]
    expanded: Vec<String>,
}

/// Load the persisted selection and expansion targets. Falls back to the legacy
/// `last_selected.txt` for the selection when `tree_state.toml` is absent (the
/// first launch after the upgrade), so the cursor still lands where it did.
pub(super) fn load_tree_state() -> (Option<AbsolutePath>, Vec<ExpandTarget>) {
    let Ok(text) = std::fs::read_to_string(&*tree_state_file()) else {
        return (load_last_selected(), Vec::new());
    };
    let file: TreeStateFile = toml::from_str(&text).unwrap_or_default();
    let selected = file
        .selected
        .as_deref()
        .filter(|raw| Path::new(raw).is_absolute())
        .map(AbsolutePath::from);
    let expanded = file
        .expanded
        .iter()
        .filter_map(|token| decode_expand_target(token))
        .collect();
    (selected, expanded)
}

/// Write the current selection and expanded containers to `tree_state.toml`.
fn save_tree_state(app: &App) {
    let file = TreeStateFile {
        selected: app
            .project_list
            .last_selected_path()
            .map(ToString::to_string),
        expanded: app
            .project_list
            .export_expanded()
            .iter()
            .map(encode_expand_target)
            .collect(),
    };
    if let Ok(text) = toml::to_string(&file) {
        let _ = std::fs::write(tree_state_file(), text);
    }
}

/// Tab-delimited token for one expanded container. The leading kind tag keeps a
/// worktree group's `Worktree` entry distinct from the `Root` at the same path.
fn encode_expand_target(target: &ExpandTarget) -> String {
    match target {
        ExpandTarget::Root(path) => format!("root\t{path}"),
        ExpandTarget::Group(path, group) => format!("group\t{path}\t{group}"),
        ExpandTarget::Worktree(path) => format!("worktree\t{path}"),
        ExpandTarget::WorktreeGroup(path, group) => format!("worktreegroup\t{path}\t{group}"),
    }
}

fn decode_expand_target(token: &str) -> Option<ExpandTarget> {
    let mut parts = token.split('\t');
    let kind = parts.next()?;
    let raw = parts.next()?;
    if !Path::new(raw).is_absolute() {
        return None;
    }
    let path = AbsolutePath::from(raw);
    match kind {
        "root" => Some(ExpandTarget::Root(path)),
        "worktree" => Some(ExpandTarget::Worktree(path)),
        "group" => Some(ExpandTarget::Group(path, parts.next()?.to_string())),
        "worktreegroup" => Some(ExpandTarget::WorktreeGroup(path, parts.next()?.to_string())),
        _ => None,
    }
}

/// Spawn a background thread to fetch details for a single project ahead of the main scan.
pub(super) fn spawn_priority_fetch(app: &App, _path: &str, abs_path: &str, name: Option<&String>) {
    let tx = app.background.background_sender();
    let client = app.net.http_client();
    let abs = AbsolutePath::from(abs_path);
    let project_name = name.cloned();

    thread::spawn(move || {
        let path: AbsolutePath = abs.clone();
        scan::emit_git_info(&tx, &abs);

        let bytes = scan::dir_size(&abs);
        let _ = tx.send(BackgroundMsg::DiskUsage {
            path: path.clone(),
            bytes,
        });

        if let Some(name) = project_name.as_ref() {
            let _ = tx.send(BackgroundMsg::CratesIoFetchQueued { name: name.clone() });
            let (info, signal) = client.fetch_crates_io_info(name);
            scan::emit_service_signal(&tx, signal);
            if let Some(info) = info {
                let _ = tx.send(BackgroundMsg::CratesIoVersion {
                    path,
                    version: info.version,
                    prerelease: info.prerelease,
                    downloads: info.downloads,
                });
            }
            let _ = tx.send(BackgroundMsg::CratesIoFetchComplete { name: name.clone() });
        }
    });
}

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

    #[test]
    fn expand_target_token_round_trips_every_variant() {
        let targets = [
            ExpandTarget::Root(AbsolutePath::from("/proj")),
            ExpandTarget::Group(AbsolutePath::from("/proj"), "examples".to_string()),
            ExpandTarget::Worktree(AbsolutePath::from("/proj-wt")),
            ExpandTarget::WorktreeGroup(AbsolutePath::from("/proj-wt"), "benches".to_string()),
        ];
        for target in targets {
            let token = encode_expand_target(&target);
            assert_eq!(decode_expand_target(&token), Some(target));
        }
    }
}