nsh-cli 0.1.1

The nsh command line tool: build, package and run apps.
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
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
//! What the command line prints: one line per step, and the details only
//! when something fails (`docs/dev/tooling.md`, "Output").
//!
//! - `✓` a step that finished, `→` where to find something, `•` what a
//!   device did, `warn` / `error` problems; plain lines are the app's own
//!   output (its `console.*`).
//! - While a step runs ([`Step`]), a spinner line shows it, replaced by the
//!   step's `✓` line. Only on a terminal: otherwise just the result lines.
//! - Colors on terminals, unless `NO_COLOR` is set.
//! - The output of the tools the CLI runs (cargo, the hosts) is captured
//!   ([`cargo`], [`capture`]) and printed under the `error` line only when
//!   they fail ([`failure`], [`report`]).
//! - Verbose (`-v`, `NSH_VERBOSE=1`): no spinner, and everything as it
//!   comes: log lines with their levels and targets (`logger.rs`), the
//!   tools' and hosts' own output.
//!
//! Everything goes to stderr: stdout is the commands' results (`nsh trace`'s
//! mount items, the signing commands' lines).

use std::{
    fmt::Display,
    io::{BufRead as _, BufReader, IsTerminal as _, Write as _},
    path::Path,
    process::{Command, ExitStatus, Stdio},
    sync::{
        Mutex, OnceLock,
        atomic::{AtomicBool, AtomicI32, AtomicU64, AtomicUsize, Ordering},
    },
    time::{Duration, Instant},
};

use anyhow::Context as _;

/// How the terminal is used, decided once ([`init`]).
struct Term {
    /// stderr is a terminal: spinners, wrapping.
    tty: bool,
    color: bool,
    verbose: bool,
}

static TERM: OnceLock<Term> = OnceLock::new();

fn term() -> &'static Term {
    TERM.get_or_init(|| Term::detect(false))
}

impl Term {
    fn detect(verbose: bool) -> Term {
        let tty = std::io::stderr().is_terminal();
        let no_color = std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty());
        Term {
            tty,
            color: tty && !no_color,
            verbose,
        }
    }
}

/// Sets up the output: verbose or not, and what stderr is.
pub(crate) fn init(verbose: bool) {
    let _ = TERM.set(Term::detect(verbose));
    #[cfg(windows)]
    win::init();
    #[cfg(unix)]
    // SAFETY: a zeroed `sigaction` is a valid value (no flags, an empty
    // mask) before the fields are set; the handler only uses
    // async-signal-safe calls (`write`, `signal`, `raise`, `getppid`) and
    // atomics.
    unsafe {
        let mut action: libc::sigaction = std::mem::zeroed();
        action.sa_sigaction = on_signal as *const () as libc::sighandler_t;
        action.sa_flags = libc::SA_SIGINFO;
        libc::sigemptyset(&mut action.sa_mask);
        libc::sigaction(libc::SIGINT, &action, std::ptr::null_mut());
        libc::sigaction(libc::SIGTERM, &action, std::ptr::null_mut());
    }
}

/// Whether everything is shown as it comes.
pub(crate) fn verbose() -> bool {
    term().verbose
}

/// Whether output is colored.
pub(crate) fn color() -> bool {
    term().color
}

/// Mirrors [`Out::drawn`] for the signal handler, which cannot lock.
static SPINNER_DRAWN: AtomicBool = AtomicBool::new(false);

/// Live [`Stoppable`]s.
static STOPPABLE: AtomicUsize = AtomicUsize::new(0);
/// The signal that asked a [`Stoppable`] command to stop (0: none).
static STOP_SIGNAL: AtomicI32 = AtomicI32::new(0);

/// Until dropped, Ctrl-C (SIGINT) and SIGTERM ask the command to stop
/// ([`stop_requested`]) instead of ending the process, so that it undoes
/// what it set up outside it on its way out (`nsh run`'s `adb reverse`);
/// a second one ends the process. [`crate::main_with`] then ends it as the
/// signal would have.
pub(crate) struct Stoppable(());

impl Stoppable {
    pub(crate) fn new() -> Stoppable {
        STOPPABLE.fetch_add(1, Ordering::SeqCst);
        Stoppable(())
    }
}

impl Drop for Stoppable {
    fn drop(&mut self) {
        STOPPABLE.fetch_sub(1, Ordering::SeqCst);
    }
}

/// Whether a signal asked the command to stop ([`Stoppable`]).
pub(crate) fn stop_requested() -> bool {
    STOP_SIGNAL.load(Ordering::SeqCst) != 0
}

/// Passes a requested stop on to `child` (a command line this one runs,
/// which is [`Stoppable`] itself): it may not have had the signal, if it
/// was sent to this process alone. Once it did, the one passed on is not
/// taken as a second.
#[cfg(unix)]
pub(crate) fn pass_stop_on(child: &std::process::Child) {
    let signal = STOP_SIGNAL.load(Ordering::SeqCst);
    if signal != 0
        && let Ok(pid) = libc::pid_t::try_from(child.id())
    {
        // SAFETY: a plain syscall on a child that has not been waited for.
        unsafe { libc::kill(pid, signal) };
    }
}

/// Windows delivers Ctrl-C to every process of the console, the child
/// included: nothing to pass on.
#[cfg(windows)]
pub(crate) fn pass_stop_on(_child: &std::process::Child) {}

/// After a requested stop: ends the process as the signal would have
/// (the shell sees an interrupted command).
pub(crate) fn exit_if_stopped() {
    let signal = STOP_SIGNAL.load(Ordering::SeqCst);
    if signal != 0 {
        exit_by_signal(signal);
    }
}

/// Ends the process with `signal`'s default action.
pub(crate) fn exit_by_signal(signal: libc::c_int) {
    // SAFETY: back to the default action, then raise it.
    unsafe {
        libc::signal(signal, libc::SIG_DFL);
        libc::raise(signal);
    }
}

/// Ctrl-C (or a stop): asks a [`Stoppable`] command to stop, else (or the
/// second time) clears a spinner's line and ends the process as the signal
/// would have. A stop the parent passes on ([`pass_stop_on`]) after this
/// one had it is not a second.
#[cfg(unix)]
extern "C" fn on_signal(signal: libc::c_int, info: *mut libc::siginfo_t, _: *mut libc::c_void) {
    if STOPPABLE.load(Ordering::SeqCst) > 0 {
        if STOP_SIGNAL
            .compare_exchange(0, signal, Ordering::SeqCst, Ordering::SeqCst)
            .is_ok()
        {
            return;
        }
        // SAFETY: the kernel passes a valid `siginfo_t` with SA_SIGINFO.
        let sender = unsafe { info.as_ref() }.map_or(0, sender);
        // SAFETY: `getppid` is async-signal-safe and cannot fail.
        if sender != 0 && sender == unsafe { libc::getppid() } {
            return;
        }
    }
    if SPINNER_DRAWN.load(Ordering::SeqCst) {
        let clear = b"\r\x1b[2K";
        // SAFETY: async-signal-safe calls with a valid buffer.
        unsafe { libc::write(libc::STDERR_FILENO, clear.as_ptr().cast(), clear.len()) };
    }
    // SAFETY: as above: back to the default action, then raise it again.
    unsafe {
        libc::signal(signal, libc::SIG_DFL);
        libc::raise(signal);
    }
}

/// The process that sent a signal (0: the kernel, the terminal's Ctrl-C).
#[cfg(unix)]
fn sender(info: &libc::siginfo_t) -> libc::pid_t {
    #[cfg(target_os = "linux")]
    // SAFETY: SIGINT and SIGTERM carry the sender's pid.
    return unsafe { info.si_pid() };
    #[cfg(not(target_os = "linux"))]
    return info.si_pid;
}

/// The console's side of [`init`] and [`columns`] on Windows.
#[cfg(windows)]
mod win {
    use std::sync::atomic::Ordering;

    use windows_sys::Win32::{
        Foundation::{FALSE, TRUE},
        System::Console::{
            CONSOLE_SCREEN_BUFFER_INFO, CTRL_BREAK_EVENT, CTRL_C_EVENT,
            ENABLE_VIRTUAL_TERMINAL_PROCESSING, GetConsoleMode, GetConsoleScreenBufferInfo,
            GetStdHandle, STD_ERROR_HANDLE, SetConsoleCtrlHandler, SetConsoleMode,
        },
    };

    use super::{SPINNER_DRAWN, STOP_SIGNAL, STOPPABLE};

    /// Turns on escape sequences (colors, the spinner's line clearing) and
    /// takes Ctrl-C as `on_signal` does on Unix.
    pub(super) fn init() {
        // SAFETY: plain console calls on this process's stderr handle;
        // `mode` is a local the call fills.
        unsafe {
            let err = GetStdHandle(STD_ERROR_HANDLE);
            let mut mode = 0;
            if GetConsoleMode(err, &mut mode) != 0 {
                SetConsoleMode(err, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
            }
            SetConsoleCtrlHandler(Some(on_ctrl), TRUE);
        }
    }

    /// Runs on a thread of its own: a [`super::Stoppable`] command is asked
    /// to stop the first time; otherwise the spinner's line is cleared and
    /// the default handler ends the process.
    unsafe extern "system" fn on_ctrl(event: u32) -> i32 {
        if event != CTRL_C_EVENT && event != CTRL_BREAK_EVENT {
            return FALSE;
        }
        if STOPPABLE.load(Ordering::SeqCst) > 0
            && STOP_SIGNAL
                .compare_exchange(0, libc::SIGINT, Ordering::SeqCst, Ordering::SeqCst)
                .is_ok()
        {
            return TRUE;
        }
        if SPINNER_DRAWN.load(Ordering::SeqCst) {
            use std::io::Write as _;
            let _ = std::io::stderr().write_all(b"\r\x1b[2K");
        }
        FALSE
    }

    /// The console window's width in columns.
    pub(super) fn columns() -> Option<usize> {
        // SAFETY: a zeroed `CONSOLE_SCREEN_BUFFER_INFO` is valid plain data,
        // filled by the call.
        unsafe {
            let err = GetStdHandle(STD_ERROR_HANDLE);
            let mut info: CONSOLE_SCREEN_BUFFER_INFO = std::mem::zeroed();
            (GetConsoleScreenBufferInfo(err, &mut info) != 0)
                .then(|| (info.srWindow.Right - info.srWindow.Left + 1).max(0) as usize)
        }
    }
}

// ---------------------------------------------------------------------------
// Styles

fn paint(code: &str, text: impl Display) -> String {
    if color() {
        format!("\x1b[{code}m{text}\x1b[0m")
    } else {
        text.to_string()
    }
}

/// Secondary text: timings, sizes, hints.
pub(crate) fn dim(text: impl Display) -> String {
    paint("2", text)
}

pub(crate) fn green(text: impl Display) -> String {
    paint("32", text)
}

pub(crate) fn cyan(text: impl Display) -> String {
    paint("36", text)
}

pub(crate) fn yellow(text: impl Display) -> String {
    paint("33", text)
}

pub(crate) fn red(text: impl Display) -> String {
    paint("31", text)
}

/// `text` without its ANSI escape sequences.
pub(crate) fn strip_ansi(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut chars = text.chars();
    while let Some(c) = chars.next() {
        if c != '\x1b' {
            out.push(c);
            continue;
        }
        // CSI: ESC [ parameters final-byte (@ to ~).
        if chars.next() == Some('[') {
            for c in chars.by_ref() {
                if ('@'..='~').contains(&c) {
                    break;
                }
            }
        }
    }
    out
}

/// The width `text` takes on a terminal (one column per character).
fn width(text: &str) -> usize {
    strip_ansi(text).chars().count()
}

/// `path` relative to the current directory when it is inside it.
pub(crate) fn path(path: &Path) -> String {
    let full = dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    let cwd = std::env::current_dir().and_then(dunce::canonicalize);
    match cwd.as_deref().map(|cwd| full.strip_prefix(cwd)) {
        Ok(Ok(rest)) if rest.as_os_str().is_empty() => ".".to_owned(),
        Ok(Ok(rest)) => rest.display().to_string(),
        _ => path.display().to_string(),
    }
}

/// A size: `500 B`, `3.2 KB`, `1.4 MB`.
pub(crate) fn bytes(n: usize) -> String {
    const K: f64 = 1024.0;
    let n_f = n as f64;
    if n < 1024 {
        format!("{n} B")
    } else if n_f < K * K {
        format!("{:.1} KB", n_f / K)
    } else {
        format!("{:.1} MB", n_f / (K * K))
    }
}

/// A duration: `21ms`, `5.0s`, `1m 3s`.
pub(crate) fn duration(d: Duration) -> String {
    let ms = d.as_millis();
    if ms < 1000 {
        format!("{ms}ms")
    } else if ms < 60_000 {
        format!("{:.1}s", d.as_secs_f64())
    } else {
        format!("{}m {}s", ms / 60_000, ms % 60_000 / 1000)
    }
}

// ---------------------------------------------------------------------------
// Lines

/// What is printed, and the spinner drawn below it.
struct Out {
    /// Running steps; the last one is drawn.
    spinners: Vec<Spinner>,
    /// The spinner line is on screen.
    drawn: bool,
    frame: usize,
}

struct Spinner {
    id: u64,
    label: String,
    status: String,
    start: Instant,
}

static OUT: Mutex<Out> = Mutex::new(Out {
    spinners: Vec::new(),
    drawn: false,
    frame: 0,
});

const FRAMES: [char; 10] = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];

fn out() -> std::sync::MutexGuard<'static, Out> {
    OUT.lock().unwrap_or_else(|e| e.into_inner())
}

impl Out {
    fn clear(&mut self, err: &mut impl std::io::Write) {
        if self.drawn {
            let _ = err.write_all(b"\r\x1b[2K");
            self.drawn = false;
            SPINNER_DRAWN.store(false, Ordering::SeqCst);
        }
    }

    fn draw(&mut self, err: &mut impl std::io::Write) {
        let Some(spinner) = self.spinners.last() else {
            return self.clear(err);
        };
        let columns = columns().unwrap_or(80).saturating_sub(1);
        let mut rest = String::new();
        let elapsed = spinner.start.elapsed();
        if elapsed >= Duration::from_secs(1) {
            rest.push_str(&format!(" {}s", elapsed.as_secs()));
        }
        if !spinner.status.is_empty() {
            rest.push_str(" · ");
            rest.push_str(&spinner.status);
        }
        // "⠋ label…" then the dim rest, cut to the terminal's width (a line
        // that wraps could not be cleared).
        let label: String = format!("{}…", spinner.label)
            .chars()
            .take(columns.saturating_sub(2))
            .collect();
        let room = columns.saturating_sub(2 + label.chars().count());
        let rest: String = rest.chars().take(room).collect();
        let frame = FRAMES[self.frame % FRAMES.len()];
        let rest = if rest.is_empty() { rest } else { dim(rest) };
        let _ = write!(err, "\r\x1b[2K{} {label}{rest}", cyan(frame));
        let _ = err.flush();
        self.drawn = true;
        SPINNER_DRAWN.store(true, Ordering::SeqCst);
    }
}

/// Prints `text` (whole lines) above the spinner.
fn print(text: &str) {
    let mut out = out();
    let mut err = std::io::stderr().lock();
    out.clear(&mut err);
    let _ = err.write_all(text.as_bytes());
    if !text.ends_with('\n') {
        let _ = err.write_all(b"\n");
    }
    out.draw(&mut err);
}

/// The terminal's width, when stderr is one.
fn columns() -> Option<usize> {
    if !term().tty {
        return None;
    }
    #[cfg(unix)]
    // SAFETY: TIOCGWINSZ fills the zeroed winsize it is given.
    let size = unsafe {
        let mut size: libc::winsize = std::mem::zeroed();
        (libc::ioctl(libc::STDERR_FILENO, libc::TIOCGWINSZ, &mut size) == 0)
            .then_some(usize::from(size.ws_col))
    };
    #[cfg(windows)]
    let size = win::columns();
    size.filter(|&c| c > 20)
}

/// `tag message`, the message wrapped to the terminal with a hanging
/// indent under its first word (its own lines too).
fn tagged(tag: &str, message: &str) -> String {
    let columns = columns().map(|c| c.saturating_sub(width(tag) + 1).max(20));
    wrap(tag, message, columns)
}

/// [`tagged`] for lines of `room` columns after the tag (`None`: no
/// wrapping).
fn wrap(tag: &str, message: &str, room: Option<usize>) -> String {
    let indent = width(tag) + 1;
    let mut text = String::new();
    for (i, line) in message.lines().enumerate() {
        let mut col = 0;
        if i > 0 {
            text.push('\n');
        }
        text.push_str(&if i == 0 {
            format!("{tag} ")
        } else {
            " ".repeat(indent)
        });
        for (j, word) in line.split(' ').enumerate() {
            let w = width(word);
            if j > 0 {
                // A word longer than a line (a path) stays: the terminal
                // wraps it.
                if room.is_some_and(|room| col + 1 + w > room && w <= room) {
                    text.push('\n');
                    text.push_str(&" ".repeat(indent));
                    col = 0;
                } else {
                    text.push(' ');
                    col += 1;
                }
            }
            text.push_str(word);
            col += w;
        }
    }
    text
}

/// `✓ message`: a step finished.
pub(crate) fn done(message: impl Display) {
    print(&format!("{} {message}", green("✓")));
}

/// `→ message`: where to find something.
pub(crate) fn note(message: impl Display) {
    print(&format!("{} {message}", cyan("→")));
}

/// `  message`: more about the line above.
pub(crate) fn detail(message: impl Display) {
    print(&format!("  {message}"));
}

/// `• message`: what a device did.
pub(crate) fn event(message: impl Display) {
    print(&format!("{} {message}", cyan("•")));
}

/// `warn message`.
pub(crate) fn warn(message: impl Display) {
    print(&tagged(&yellow("warn"), &message.to_string()));
}

/// `error message`.
pub(crate) fn error(message: impl Display) {
    print(&tagged(&red("error"), &message.to_string()));
}

/// A line as it is: the app's own output, or a tool's.
pub(crate) fn plain(message: impl Display) {
    print(&message.to_string());
}

/// A line only verbose runs show.
pub(crate) fn debug(message: impl Display) {
    if verbose() {
        print(&dim(message));
    }
}

// ---------------------------------------------------------------------------
// Steps

/// A step that takes a while: a spinner line while it runs (on a terminal),
/// replaced by its `✓` line ([`Step::done`]) or by nothing
/// ([`Step::finish`]; also when it is dropped, as on errors).
pub(crate) struct Step {
    id: u64,
    start: Instant,
}

impl Step {
    /// Starts the step, `label` saying what it does ("building …").
    pub(crate) fn start(label: impl Into<String>) -> Step {
        static NEXT: AtomicU64 = AtomicU64::new(0);
        let id = NEXT.fetch_add(1, Ordering::Relaxed);
        let start = Instant::now();
        if term().tty && !verbose() {
            spin();
            let mut out = out();
            out.spinners.push(Spinner {
                id,
                label: label.into(),
                status: String::new(),
                start,
            });
            out.draw(&mut std::io::stderr().lock());
        }
        Step { id, start }
    }

    /// What the step is at (shown after its label).
    pub(crate) fn status(&self, status: &str) {
        let mut out = out();
        if let Some(spinner) = out.spinners.iter_mut().find(|s| s.id == self.id) {
            spinner.status = status.to_owned();
        }
    }

    /// How long it has run.
    pub(crate) fn elapsed(&self) -> Duration {
        self.start.elapsed()
    }

    /// Ends it with `✓ message (time)`.
    pub(crate) fn done(self, message: impl Display) {
        self.done_with(message, None);
    }

    /// Ends it with `✓ message (time, note)`.
    pub(crate) fn done_with(self, message: impl Display, note: Option<&str>) {
        let time = duration(self.elapsed());
        let paren = match note {
            Some(note) => format!("({time}, {note})"),
            None => format!("({time})"),
        };
        drop(self);
        done(format!("{message} {}", dim(paren)));
    }

    /// Ends it without a line.
    pub(crate) fn finish(self) {}
}

impl Drop for Step {
    fn drop(&mut self) {
        let mut out = out();
        let before = out.spinners.len();
        out.spinners.retain(|s| s.id != self.id);
        if out.spinners.len() != before {
            out.draw(&mut std::io::stderr().lock());
        }
    }
}

/// Starts the thread that animates spinners (once).
fn spin() {
    static STARTED: OnceLock<()> = OnceLock::new();
    STARTED.get_or_init(|| {
        let _ = std::thread::Builder::new()
            .name("nsh-spinner".into())
            .spawn(|| {
                loop {
                    std::thread::sleep(Duration::from_millis(80));
                    let mut out = out();
                    if !out.spinners.is_empty() {
                        out.frame += 1;
                        out.draw(&mut std::io::stderr().lock());
                    }
                }
            });
    });
}

// ---------------------------------------------------------------------------
// Tools

/// A failure whose details (a tool's output) are printed below its
/// message ([`report`]).
#[derive(Debug)]
pub(crate) struct Failure {
    message: String,
    details: String,
}

/// A [`Failure`] saying `message`, with `details` below it.
pub(crate) fn failure(message: impl Into<String>, details: impl Into<String>) -> anyhow::Error {
    Failure {
        message: message.into(),
        details: details.into(),
    }
    .into()
}

impl Display for Failure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for Failure {}

/// Prints `error`: `error <message>`, and the details of the
/// [`Failure`] in it.
pub(crate) fn report(error: &anyhow::Error) {
    self::error(format!("{error:#}"));
    if let Some(failure) = error.chain().find_map(|e| e.downcast_ref::<Failure>())
        && !failure.details.trim().is_empty()
    {
        plain(failure.details.trim_end());
    }
}

/// Runs `cmd` with its output (stdout and stderr, in order) captured, each
/// line passed to `on_line` too; returns its status and the output.
/// Verbose runs let the output through instead (and return none).
pub(crate) fn capture(
    cmd: &mut Command,
    mut on_line: impl FnMut(&str),
) -> std::io::Result<(ExitStatus, String)> {
    if verbose() {
        return Ok((cmd.status()?, String::new()));
    }
    let (reader, writer) = std::io::pipe()?;
    cmd.stdin(Stdio::null())
        .stdout(writer.try_clone()?)
        .stderr(writer);
    let child = cmd.spawn();
    // The command keeps its copies of the pipe's write end: without
    // dropping them the read below never ends.
    cmd.stdout(Stdio::null()).stderr(Stdio::null());
    let mut child = child?;
    let mut output = String::new();
    let mut reader = BufReader::new(reader);
    let mut line = Vec::new();
    while reader.read_until(b'\n', &mut line)? > 0 {
        let text = String::from_utf8_lossy(&line);
        on_line(text.trim_end_matches(['\n', '\r']));
        output.push_str(&text);
        line.clear();
    }
    Ok((child.wait()?, output))
}

/// Runs the cargo build `cmd` as step `building <what>`: the spinner
/// shows the crate it compiles, the output is shown only if it fails, and
/// warnings are counted (`-v` shows them).
pub(crate) fn cargo(cmd: &mut Command, what: &str) -> anyhow::Result<()> {
    let step = Step::start(format!("building {what}"));
    if !verbose() {
        cmd.env("CARGO_TERM_COLOR", if color() { "always" } else { "never" });
    }
    let mut warnings = 0;
    let (status, output) = capture(cmd, |line| {
        let line = strip_ansi(line);
        let line = line.trim_start();
        if let Some(rest) = line.strip_prefix("Compiling ") {
            step.status(rest.split(' ').next().unwrap_or(rest));
        }
        warnings += generated_warnings(line);
    })
    .context("run cargo")?;
    if !status.success() {
        return Err(failure(format!("building {what} failed"), output));
    }
    let note = match warnings {
        0 => None,
        1 => Some("1 warning: -v shows it".to_owned()),
        n => Some(format!("{n} warnings: -v shows them")),
    };
    step.done_with(format!("built {what}"), note.as_deref());
    Ok(())
}

/// The count in cargo's "warning: `crate` (lib) generated 2 warnings".
fn generated_warnings(line: &str) -> usize {
    let Some(rest) = line.strip_prefix("warning: ") else {
        return 0;
    };
    let Some((_, count)) = rest.split_once(" generated ") else {
        return 0;
    };
    count
        .split(' ')
        .next()
        .and_then(|n| n.parse().ok())
        .unwrap_or(0)
}

/// The last lines a long-running process printed, kept to show if it
/// fails ([`quiet`]).
#[derive(Clone, Default)]
pub(crate) struct Tail(std::sync::Arc<Mutex<std::collections::VecDeque<String>>>);

impl Tail {
    /// The kept lines.
    pub(crate) fn text(&self) -> String {
        let lines = self.0.lock().unwrap_or_else(|e| e.into_inner());
        lines
            .iter()
            .map(String::as_str)
            .collect::<Vec<_>>()
            .join("\n")
    }
}

/// Spawns `cmd`, a process that runs alongside the CLI (a host): its own
/// output is kept, not shown ([`Tail`]); verbose runs show it.
pub(crate) fn quiet(cmd: &mut Command) -> std::io::Result<(std::process::Child, Tail)> {
    const KEEP: usize = 200;
    let tail = Tail::default();
    if verbose() {
        return Ok((cmd.spawn()?, tail));
    }
    let (reader, writer) = std::io::pipe()?;
    cmd.stdout(writer.try_clone()?).stderr(writer);
    let child = cmd.spawn();
    cmd.stdout(Stdio::null()).stderr(Stdio::null());
    let child = child?;
    let lines = tail.clone();
    std::thread::Builder::new()
        .name("nsh-host-output".into())
        .spawn(move || {
            for line in BufReader::new(reader).lines() {
                let Ok(line) = line else { break };
                let mut lines = lines.0.lock().unwrap_or_else(|e| e.into_inner());
                if lines.len() == KEEP {
                    lines.pop_front();
                }
                lines.push_back(line);
            }
        })?;
    Ok((child, tail))
}

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

    #[test]
    fn cargo_warning_summaries_are_counted() {
        for (line, count) in [
            ("warning: `app` (lib) generated 2 warnings", 2),
            ("warning: `app` (lib) generated 1 warning", 1),
            (
                "warning: `app` (lib) generated 3 warnings (run `cargo fix --lib -p app` to apply 1 suggestion)",
                3,
            ),
            ("warning: unused variable: `x`", 0),
            ("   Compiling app v0.1.0", 0),
        ] {
            assert_eq!(generated_warnings(line), count, "{line}");
        }
    }

    #[test]
    fn problems_wrap_under_their_first_word() {
        assert_eq!(
            wrap(
                "warn",
                "iOS Simulator: image failed: An SSL error has occurred",
                Some(20)
            ),
            "warn iOS Simulator: image\n     failed: An SSL error\n     has occurred"
        );
        // Their own lines too; a word longer than a line stays.
        assert_eq!(
            wrap(
                "error",
                "building /a/very/long/path/App.tsx failed\n  at x",
                Some(20)
            ),
            "error building /a/very/long/path/App.tsx\n      failed\n        at x"
        );
        assert_eq!(wrap("warn", "a b", None), "warn a b");
    }

    #[test]
    fn escapes_are_stripped() {
        assert_eq!(
            strip_ansi("\x1b[1m\x1b[32m   Compiling\x1b[0m taffy"),
            "   Compiling taffy"
        );
        assert_eq!(width("\x1b[33mwarn\x1b[0m"), 4);
    }

    #[test]
    fn sizes_and_times_read_well() {
        assert_eq!(bytes(500), "500 B");
        assert_eq!(bytes(3255), "3.2 KB");
        assert_eq!(bytes(3 << 20), "3.0 MB");
        assert_eq!(duration(Duration::from_millis(21)), "21ms");
        assert_eq!(duration(Duration::from_millis(12_640)), "12.6s");
        assert_eq!(duration(Duration::from_secs(63)), "1m 3s");
    }
}