nsh-cli 0.6.0

The nsh command line tool: build, package and run apps.
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
//! 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 test --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`) and atomics.
    unsafe {
        let mut action: libc::sigaction = std::mem::zeroed();
        action.sa_sigaction = on_signal as *const () as libc::sighandler_t;
        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 && !LOGGING.load(Ordering::Acquire)
}

/// 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`] 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 || crate::dev_port::stop_requested()
}

/// 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.
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.
#[cfg(unix)]
extern "C" fn on_signal(signal: libc::c_int) {
    if STOPPABLE.load(Ordering::SeqCst) > 0
        && STOP_SIGNAL
            .compare_exchange(0, signal, Ordering::SeqCst, Ordering::SeqCst)
            .is_ok()
    {
        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 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)
}

/// `url`, clickable in terminals that support hyperlinks (OSC 8), cyan;
/// the bare URL where colors are off (terminals linkify it themselves).
pub(crate) fn link(url: &str) -> String {
    if color() {
        format!("\x1b]8;;{url}\x1b\\{}\x1b]8;;\x1b\\", cyan(url))
    } else {
        url.to_owned()
    }
}

/// `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;
        }
        match chars.next() {
            // CSI: ESC [ parameters final-byte (@ to ~).
            Some('[') => {
                for c in chars.by_ref() {
                    if ('@'..='~').contains(&c) {
                        break;
                    }
                }
            }
            // OSC (hyperlinks, [`link`]): ESC ] ... ending with BEL or ESC \.
            Some(']') => {
                while let Some(c) = chars.next() {
                    if c == '\x07' {
                        break;
                    }
                    if c == '\x1b' {
                        chars.next();
                        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,
    log: Option<std::fs::File>,
}

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

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

static LOGGING: AtomicBool = AtomicBool::new(false);

/// Routes CLI diagnostics and tool output away from the terminal until dropped.
pub(crate) struct LogFile;

impl LogFile {
    pub(crate) fn open(path: &Path) -> std::io::Result<Self> {
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)?;
        let mut out = out();
        out.clear(&mut std::io::stderr().lock());
        out.log = Some(file);
        LOGGING.store(true, Ordering::Release);
        Ok(Self)
    }
}

impl Drop for LogFile {
    fn drop(&mut self) {
        let mut out = out();
        out.log = None;
        out.spinners.clear();
        LOGGING.store(false, Ordering::Release);
    }
}

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) {
        if self.log.is_some() {
            return;
        }
        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) };
        // One write, the line first and its old tail erased after: stderr is
        // unbuffered, so a `write!` here would reach the terminal in pieces
        // and one that clears the line first would show it blank between
        // them (the spinner flickers).
        let line = format!("\r{} {label}{rest}\x1b[K", cyan(frame));
        let _ = err.write_all(line.as_bytes());
        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();
    if let Some(log) = &mut out.log {
        let _ = writeln!(log, "{text}");
        return;
    }
    // The cleared spinner, the text and the spinner again in one write, so
    // the terminal never shows the line without its spinner.
    let mut buf = Vec::with_capacity(text.len() + 128);
    out.clear(&mut buf);
    buf.extend_from_slice(text.as_bytes());
    if !text.ends_with('\n') {
        buf.push(b'\n');
    }
    out.draw(&mut buf);
    let mut err = std::io::stderr().lock();
    let _ = err.write_all(&buf);
    let _ = err.flush();
}

/// 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));
    }
}

/// Whether the command line may ask the user: stdin and stdout are a
/// terminal (not a pipe, a CI job or an editor's task).
pub(crate) fn interactive() -> bool {
    std::io::stdin().is_terminal() && std::io::stdout().is_terminal()
}

/// `? question`, then the `options` (a name and what it is), numbered:
/// the index of the one the user picks by number or name. Enter (or the
/// end of input) picks the first; anything else asks again.
pub(crate) fn choose(question: &str, options: &[(&str, &str)]) -> anyhow::Result<usize> {
    let name_width = options
        .iter()
        .map(|(name, _)| width(name))
        .max()
        .unwrap_or(0);
    print(&format!("{} {question}", cyan("?")));
    for (i, (name, what)) in options.iter().enumerate() {
        let pad = " ".repeat(name_width - width(name));
        print(&format!(
            "  {} {name}{pad}  {}",
            dim(format!("{}.", i + 1)),
            dim(what)
        ));
    }
    let default = options.first().map_or("", |(name, _)| name);
    let stdin = std::io::stdin();
    loop {
        eprint!(
            "{} {}",
            cyan("?"),
            dim(format!("1-{}, or Enter for {default}: ", options.len()))
        );
        let _ = std::io::stderr().flush();
        let mut line = String::new();
        if stdin
            .lock()
            .read_line(&mut line)
            .context("read the answer")?
            == 0
        {
            eprintln!();
            return Ok(0);
        }
        let answer = line.trim();
        if answer.is_empty() {
            return Ok(0);
        }
        let picked = answer
            .parse::<usize>()
            .ok()
            .and_then(|n| n.checked_sub(1))
            .filter(|&i| i < options.len())
            .or_else(|| {
                options
                    .iter()
                    .position(|(name, _)| name.eq_ignore_ascii_case(answer))
            });
        match picked {
            Some(i) => return Ok(i),
            None => warn(format!("{answer:?} is none of them")),
        }
    }
}

// ---------------------------------------------------------------------------
// 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() && !LOGGING.load(Ordering::Acquire) {
            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 {}

/// `error` as one message: its chain, then the details of the
/// [`Failure`] in it (a compiler's diagnostics), for reports that carry a
/// message instead of printing it (a test's failure).
pub(crate) fn with_details(error: &anyhow::Error) -> String {
    let mut message = format!("{error:#}");
    if let Some(failure) = error.chain().find_map(|e| e.downcast_ref::<Failure>())
        && !failure.details.trim().is_empty()
    {
        message.push('\n');
        message.push_str(failure.details.trim_end());
    }
    message
}

/// 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,
    on_line: impl FnMut(&str),
) -> std::io::Result<(ExitStatus, String)> {
    if verbose() && !LOGGING.load(Ordering::Acquire) {
        return Ok((cmd.status()?, String::new()));
    }
    capture_all(cmd, on_line)
}

/// [`capture`] for output the caller reads (`cargo test`'s results):
/// verbose runs get it too, and show each line as it comes.
pub(crate) fn capture_all(
    cmd: &mut Command,
    mut on_line: impl FnMut(&str),
) -> std::io::Result<(ExitStatus, String)> {
    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);
        let text_line = text.trim_end_matches(['\n', '\r']);
        if verbose() {
            plain(text_line);
        }
        on_line(text_line);
        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<()> {
    cargo_as(cmd, ["building", "built"], what)
}

/// [`cargo`] for another cargo command (`cargo check`): `doing` names the
/// step while it runs and once it ends (`["checking", "checked"]`).
pub(crate) fn cargo_as(cmd: &mut Command, doing: [&str; 2], what: &str) -> anyhow::Result<()> {
    let [doing, done] = doing;
    let step = Step::start(format!("{doing} {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!("{doing} {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!("{done} {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() && !LOGGING.load(Ordering::Acquire) {
        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 };
                if verbose() {
                    plain(&line);
                }
                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 test_failures_carry_the_compilers_diagnostics() {
        let error = failure("building App.tsx failed", "  x `nsh` has no export `If`\n");
        assert_eq!(
            with_details(&error),
            "building App.tsx failed\n  x `nsh` has no export `If`"
        );
        assert_eq!(with_details(&anyhow::anyhow!("no entry")), "no entry");
    }

    #[test]
    fn links_and_colors_strip_to_their_text() {
        let url = "http://127.0.0.1:8790/";
        let linked = format!("\x1b]8;;{url}\x1b\\\x1b[36m{url}\x1b[0m\x1b]8;;\x1b\\ next");
        assert_eq!(strip_ansi(&linked), format!("{url} next"));
        assert_eq!(strip_ansi("\x1b]8;;x\x07y\x1b]8;;\x07"), "y");
    }

    #[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");
    }
}