tinsel 0.4.0

Clean terminal output with a little sparkle: live step lists, spinners, tables, trees, prompts and more.
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
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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
//! Full-screen terminal apps: dashboards, pickers, little toys, in the same pink as
//! everything else.
//!
//! An app is a struct with your state, and two methods: [`view`](App::view) draws the state
//! into a [`Frame`], and [`event`](App::event) changes it when a key is pressed, the mouse
//! moves or time passes. [`run`] takes over the screen, calls them in turn, and puts the
//! terminal back when you're done (or if anything panics).
//!
//! ```no_run
//! use tinsel::app::{self, App, Event, Flow, Frame, Key, List, Panel, Size::*, hints};
//!
//! struct Picker {
//!     flavours: Vec<&'static str>,
//!     selected: usize,
//! }
//!
//! impl App for Picker {
//!     fn view(&self, frame: &mut Frame) {
//!         let [main, footer] = frame.area().rows([Fill(1), Fixed(1)]);
//!         let list = List::new(self.flavours.clone()).selected(self.selected);
//!         frame.render(main, Panel::new("ice cream").body(list).focused(true));
//!         frame.render(footer.shrink(1, 0), hints(&[("↑↓", "choose"), ("enter", "done")]));
//!     }
//!
//!     fn event(&mut self, event: &Event) -> Flow {
//!         app::navigate(&mut self.selected, self.flavours.len(), event);
//!         match event {
//!             Event::Key(Key::Enter | Key::Esc | Key::Char('q')) => Flow::Quit,
//!             _ => Flow::Continue,
//!         }
//!     }
//! }
//!
//! fn main() -> std::io::Result<()> {
//!     let picker = app::run(Picker { flavours: vec!["strawberry", "pistachio", "mint"], selected: 0 })?;
//!     println!("one scoop of {}", picker.flavours[picker.selected]);
//!     Ok(())
//! }
//! ```
//!
//! # Laying things out
//!
//! [`Frame::area`] is the whole screen as a [`Rect`]; [`Rect::rows`] and [`Rect::columns`]
//! cut it up by [`Size`], and [`Frame::render`] draws a [`Widget`] into a piece. Everything
//! tinsel prints ([`Table`](crate::Table), [`Tree`](crate::Tree), [`Card`](crate::Card),
//! any [`Block`]) is a widget already; on top of those there's [`Panel`], [`List`],
//! [`Grid`] (a table with a cursor), [`ScrollView`] (lines to scroll through, or follow as they
//! grow), [`Input`] (a box to type into) and [`TextArea`] (several lines of it), [`Tabs`], [`Plot`], [`Stat`], [`Loading`] (a spinner, and a bar once there's progress), [`big`] text, [`hints`]
//! for the footer and [`Sparkles`] for the background.
//!
//! # Time
//!
//! [`Frame::time`] is the seconds since the app started, for anything animated, and
//! [`Event::Tick`] arrives [`fps`](App::fps) times a second, for polling whatever your
//! dashboard shows. Work done on other threads can wake the app the moment it's ready with
//! a [`Waker`], so it needn't tick quickly just to notice.
//!
//! # Leaving for a moment
//!
//! [`suspend`] hands the terminal back while something else uses it (a sign-in command, an
//! editor, a shell), then takes it again and redraws.
//!
//! # Tests
//!
//! [`snapshot`] draws one frame of an app at any size, without a terminal, so you can check
//! exactly what it shows. When stdout isn't a terminal, [`run`] prints that one frame and
//! returns.

mod field;
mod frame;
mod grid;
mod input;
mod keys;
mod layout;
mod overlay;
mod scroll;
mod textarea;
mod widgets;

use std::io::{self, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, PoisonError, mpsc};
use std::time::{Duration, Instant};

pub use field::Input;
pub use frame::Frame;
pub use grid::{Cursor, Grid};
pub use input::{Key, Mouse, MouseKind};
pub use keys::Keys;
pub use layout::{Rect, Size};
pub use overlay::{Modal, Toasts};
pub use scroll::{Scroll, ScrollView};
pub use textarea::TextArea;
pub use widgets::{Footer, List, Loading, Panel, Plot, Sparkles, Stat, Tabs, Widget, big, hints};

use crate::block::Block;
use crate::term;

/// Something that happened, handed to [`App::event`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {
    /// A key was pressed.
    Key(Key),
    /// Text was pasted, all at once, with its line breaks as `\n`: a newline in it is part
    /// of the text, not a press of Enter. [`Input`] takes it as typed. Terminals that can't
    /// say what's pasted (older ones, Windows consoles outside Windows Terminal) send it as
    /// keys instead.
    Paste(String),
    /// The mouse was clicked or scrolled (only if [`App::mouse`] says so).
    Mouse(Mouse),
    /// Time passed: [`App::fps`] of these arrive a second.
    Tick,
    /// The terminal changed size, to this many columns and rows.
    Resize(usize, usize),
    /// A [`Waker`] was woken: something another thread was doing is ready to look at.
    Wake,
}

/// Whether to keep going after an event.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Flow {
    /// Carry on.
    Continue,
    /// Put the terminal back and return from [`run`].
    Quit,
}

/// A full-screen app. Only [`view`](App::view) is needed: by default `q` and Esc quit,
/// it draws at 30 frames a second, and the mouse is left alone.
pub trait App {
    /// Draw the app as it is now. Called every frame, so keep it quick, and keep slow work
    /// (fetching, reading files) in [`event`](App::event) or another thread.
    fn view(&self, frame: &mut Frame);

    /// Change the app in response to something happening. Ctrl-C comes here too, so the app
    /// can note that it was stopped, and then always quits, whatever this returns.
    fn event(&mut self, event: &Event) -> Flow {
        match event {
            Event::Key(Key::Char('q') | Key::Esc) => Flow::Quit,
            _ => Flow::Continue,
        }
    }

    /// How many times a second to draw and send [`Event::Tick`]. 30 unless changed; lower
    /// it for a dashboard with nothing animated.
    fn fps(&self) -> u32 {
        crate::fps()
    }

    /// The terminal window's title while the app runs, such as the app's name and what it
    /// shows. Left alone unless changed; the old title comes back when the app quits.
    fn title(&self) -> Option<String> {
        None
    }

    /// Whether to report clicks and the scroll wheel. Off unless changed, since while it's on
    /// the terminal can't select text with the mouse.
    fn mouse(&self) -> bool {
        false
    }
}

/// Run an app full-screen until it quits, then hand it back, so you can read what was chosen.
///
/// The screen is put back as it was afterwards, whether the app quits, returns an error or
/// panics. When stdout isn't a terminal, one frame is printed instead, at the terminal's
/// size (or 80×24), and the app is returned straight away.
pub fn run<A: App>(mut app: A) -> io::Result<A> {
    run_app(&mut app)?;
    Ok(app)
}

/// [`run`], for any app.
fn run_app(app: &mut dyn App) -> io::Result<()> {
    if !term::interactive() {
        snapshot(app, term::width(), term::height()).print();
        return Ok(());
    }
    // decide these before stdin is in raw mode: finding out may mean asking the terminal
    crate::style::theme();
    crate::style::color_mode();

    let mut session = Session::enter(app.mouse())?;
    let (tx, rx) = mpsc::channel();
    let _waking = Waking::listen(tx.clone());
    let control = session.control.clone();
    session.reader = Some(std::thread::spawn(move || {
        listen(&mut term::read_byte, &tx, &control);
        control.idle.store(true, Ordering::SeqCst);
    }));
    drive(app, &rx, &mut io::stdout(), &mut || (term::width(), term::height()))?;
    drop(session);
    Ok(())
}

/// The loop itself: draw, wait for an event or the next tick, and go round again until the app
/// quits, Ctrl-C is pressed or the events stop. Kept apart from the terminal so it can be
/// tested with any events, output and screen size.
fn drive(
    app: &mut dyn App,
    rx: &mpsc::Receiver<Event>,
    out: &mut dyn Write,
    screen: &mut dyn FnMut() -> (usize, usize),
) -> io::Result<()> {
    let start = Instant::now();
    let _clock = Clock::start(start);
    let tick = Duration::from_secs_f64(1.0 / app.fps().clamp(1, 240) as f64);
    let mut next_tick = start + tick;
    let mut size = screen();
    let mut shown: Option<Frame> = None;
    let mut titled: Option<String> = None;
    loop {
        if REDRAW.with(|r| r.replace(false)) {
            shown = None;
            titled = None;
        }
        let title = app.title();
        if title.is_some() && title != titled {
            // control characters would end the sequence early
            let clean: String = title
                .iter()
                .flat_map(|t| t.chars())
                .filter(|c| !c.is_control())
                .collect();
            write!(out, "\x1b]0;{clean}\x07")?;
            titled = title;
        }
        let now_size = screen();
        if now_size != size {
            size = now_size;
            shown = None;
            if app.event(&Event::Resize(size.0, size.1)) == Flow::Quit {
                return Ok(());
            }
        }
        let mut frame = Frame::new(size.0, size.1).at(start.elapsed().as_secs_f32());
        app.view(&mut frame);
        let diff = frame.diff(shown.as_ref());
        if !diff.is_empty() {
            write!(out, "\x1b[?2026h{diff}\x1b[?2026l")?;
            out.flush()?;
        }
        shown = Some(frame);

        let wait = next_tick.saturating_duration_since(Instant::now());
        let first = match rx.recv_timeout(wait) {
            Ok(event) => Some(event),
            Err(mpsc::RecvTimeoutError::Timeout) => None,
            // the reader stopped: stdin is gone, so nothing more can be typed
            Err(mpsc::RecvTimeoutError::Disconnected) => return Ok(()),
        };
        for event in first.into_iter().chain(rx.try_iter()) {
            if event == Event::Wake {
                WOKEN.store(false, Ordering::SeqCst);
            }
            let quit = app.event(&event) == Flow::Quit;
            if quit || event == Event::Key(Key::Ctrl('c')) {
                return Ok(());
            }
        }
        let now = Instant::now();
        if now >= next_tick {
            // after a stall, carry on from now rather than sending a burst of ticks
            next_tick = (next_tick + tick).max(now);
            if app.event(&Event::Tick) == Flow::Quit {
                return Ok(());
            }
        }
    }
}

/// Where wakes go while an app is running.
static WAKE: Mutex<Option<mpsc::Sender<Event>>> = Mutex::new(None);
/// Whether an [`Event::Wake`] is on its way and not yet handled, so a burst of wakes sends one.
static WOKEN: AtomicBool = AtomicBool::new(false);

/// Wakes the running app from another thread, so it can show what changed straight away
/// instead of at its next [`Event::Tick`]. Make one whenever you like (before [`run`] is
/// fine), clone it into every thread that needs it, and call [`wake`](Waker::wake) when
/// there's something new: the app gets an [`Event::Wake`] at once. So a dashboard can tick
/// once a second and still show a result the moment it arrives.
///
/// The wake says only that something happened. To hand the result over, send it down your
/// own channel and read it on `Event::Wake`:
///
/// ```no_run
/// use std::sync::mpsc;
/// use tinsel::app::{self, App, Event, Flow, Frame, Waker};
///
/// struct Status {
///     results: mpsc::Receiver<String>,
///     latest: String,
/// }
///
/// impl App for Status {
///     fn view(&self, frame: &mut Frame) {
///         frame.print(frame.area(), 0, &self.latest);
///     }
///
///     fn event(&mut self, event: &Event) -> Flow {
///         if *event == Event::Wake {
///             self.latest = self.results.try_iter().last().unwrap_or_default();
///         }
///         Flow::Continue
///     }
///
///     fn fps(&self) -> u32 {
///         1
///     }
/// }
///
/// let (tx, rx) = mpsc::channel();
/// let waker = Waker::new();
/// std::thread::spawn(move || {
///     tx.send("all healthy".to_string()).unwrap();
///     waker.wake();
/// });
/// app::run(Status { results: rx, latest: "checking…".into() })?;
/// # Ok::<(), std::io::Error>(())
/// ```
#[derive(Clone, Debug, Default)]
pub struct Waker {
    _private: (),
}

impl Waker {
    /// A waker for whichever app is running, or the next one to start.
    pub fn new() -> Self {
        Waker::default()
    }

    /// Send the app an [`Event::Wake`]. Wakes that come quicker than the app handles them
    /// arrive as one; a wake before the app starts arrives as it starts.
    pub fn wake(&self) {
        if WOKEN.swap(true, Ordering::SeqCst) {
            return;
        }
        if let Some(tx) = WAKE.lock().unwrap_or_else(PoisonError::into_inner).as_ref() {
            let _ = tx.send(Event::Wake);
        }
    }
}

/// Wakes delivered to a running app, for as long as it lives.
struct Waking;

impl Waking {
    fn listen(tx: mpsc::Sender<Event>) -> Waking {
        if WOKEN.load(Ordering::SeqCst) {
            let _ = tx.send(Event::Wake);
        }
        *WAKE.lock().unwrap_or_else(PoisonError::into_inner) = Some(tx);
        Waking
    }
}

impl Drop for Waking {
    fn drop(&mut self) {
        *WAKE.lock().unwrap_or_else(PoisonError::into_inner) = None;
    }
}

thread_local! {
    static STARTED: std::cell::Cell<Option<Instant>> = const { std::cell::Cell::new(None) };
}

/// The running app's clock, for [`time`]; stopped however the loop ends.
struct Clock;

impl Clock {
    fn start(at: Instant) -> Clock {
        STARTED.with(|s| s.set(Some(at)));
        Clock
    }
}

impl Drop for Clock {
    fn drop(&mut self) {
        STARTED.with(|s| s.set(None));
    }
}

/// Seconds since the app started, on the same clock as [`Frame::time`], so something that
/// happens in [`App::event`] can be animated from that moment in [`App::view`] (a
/// [`Modal`] rising into place). 0 when no app is running, as in tests.
///
/// ```
/// assert_eq!(tinsel::app::time(), 0.0);
/// ```
pub fn time() -> f32 {
    STARTED.with(|s| s.get()).map_or(0.0, |at| at.elapsed().as_secs_f32())
}

/// One frame of an app at `width` by `height`, as text: for tests, and for printing what a
/// dashboard shows into a log.
///
/// ```
/// use tinsel::app::{App, Frame, Panel};
///
/// struct Hello;
///
/// impl App for Hello {
///     fn view(&self, frame: &mut Frame) {
///         frame.render(frame.area(), Panel::new("hello").body("hi there"));
///     }
/// }
///
/// tinsel::set_color_mode(tinsel::ColorMode::None);
/// let shown = tinsel::app::snapshot(&Hello, 16, 3).plain();
/// assert_eq!(shown.lines().nth(1), Some("│ hi there     │"));
/// ```
pub fn snapshot<A: App + ?Sized>(app: &A, width: usize, height: usize) -> Block {
    let mut frame = Frame::new(width, height);
    app.view(&mut frame);
    frame.snapshot()
}

/// Move a selection through a list of `len` things in response to an event: ↑↓ and j/k one
/// at a time, Page Up/Down ten, Home/End (or g/G) to the ends, and the scroll wheel. Returns
/// whether it moved. Made for [`List`], but works for anything with a position.
///
/// ```
/// use tinsel::app::{Event, Key, navigate};
///
/// let mut selected = 0;
/// navigate(&mut selected, 3, &Event::Key(Key::Down));
/// navigate(&mut selected, 3, &Event::Key(Key::End));
/// assert_eq!(selected, 2);
/// ```
pub fn navigate(selected: &mut usize, len: usize, event: &Event) -> bool {
    let last = len.saturating_sub(1);
    let to = match event {
        Event::Key(Key::Up | Key::Char('k')) => selected.saturating_sub(1),
        Event::Key(Key::Down | Key::Char('j')) => *selected + 1,
        Event::Key(Key::PageUp) => selected.saturating_sub(10),
        Event::Key(Key::PageDown) => *selected + 10,
        Event::Key(Key::Home | Key::Char('g')) => 0,
        Event::Key(Key::End | Key::Char('G')) => last,
        Event::Mouse(m) if m.kind == MouseKind::ScrollUp => selected.saturating_sub(1),
        Event::Mouse(m) if m.kind == MouseKind::ScrollDown => *selected + 1,
        _ => return false,
    };
    let to = to.min(last);
    let moved = to != *selected;
    *selected = to;
    moved
}

// MARK: - The terminal

/// Whether an app has the screen, so a panic can give it back before its message is printed.
static ON_SCREEN: AtomicBool = AtomicBool::new(false);

// the window title is saved on entering (22t) and put back on leaving (23t)
// and pastes arrive bracketed (2004), so they can be told apart from typing
const ENTER: &str = "\x1b[22;0t\x1b[?1049h\x1b[?25l\x1b[?7l\x1b[?2004h\x1b[2J";
const LEAVE: &str = "\x1b[0m\x1b[?1000l\x1b[?1006l\x1b[?2004l\x1b[?7h\x1b[?25h\x1b[?1049l\x1b[23;0t";
const MOUSE_ON: &str = "\x1b[?1000h\x1b[?1006h";

/// The alternate screen, raw keys and a hidden cursor, for as long as it lives.
struct Session {
    control: Arc<Control>,
    reader: Option<std::thread::JoinHandle<()>>,
    hook: Arc<PanicHook>,
}

/// How the app's thread tells the key reader what to do.
#[derive(Default)]
struct Control {
    /// Finish for good.
    stop: AtomicBool,
    /// Stop reading for now: something else wants the keyboard.
    pause: AtomicBool,
    /// Set by the reader while it isn't reading, and won't until `pause` is lifted.
    idle: AtomicBool,
}

/// What [`suspend`] needs of the running app's session. Raw mode lives here rather than in
/// the [`Session`], so the terminal can be handed back and taken again in the middle of a run.
struct Running {
    raw: Option<term::RawMode>,
    control: Arc<Control>,
    mouse: bool,
}

thread_local! {
    static RUNNING: std::cell::RefCell<Option<Running>> = const { std::cell::RefCell::new(None) };
    /// Set when the screen has to be drawn again from scratch.
    static REDRAW: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

type PanicHook = Box<dyn Fn(&std::panic::PanicHookInfo<'_>) + Sync + Send + 'static>;

impl Session {
    fn enter(mouse: bool) -> io::Result<Session> {
        term::enable_escapes();
        term::remember_terminal();
        let raw = term::RawMode::enter()?;
        // give the screen back before any panic message, which would otherwise be drawn on
        // the alternate screen and vanish with it. Only a panic on this thread ends the app: a
        // worker thread panicking leaves it running, so it has to keep the screen
        let hook: Arc<PanicHook> = Arc::new(std::panic::take_hook());
        let previous = hook.clone();
        let owner = std::thread::current().id();
        std::panic::set_hook(Box::new(move |info| {
            if std::thread::current().id() == owner {
                leave_screen();
                // unwinding drops the session, which puts the terminal back once the reader
                // has stopped; with panic = "abort" nothing else will
                cfg!(panic = "abort").then(term::restore_saved_terminal);
            }
            previous(info);
        }));
        ON_SCREEN.store(true, Ordering::SeqCst);
        // made before writing anything, so a failed write still puts everything back
        let session = Session {
            control: Arc::default(),
            reader: None,
            hook,
        };
        RUNNING.with(|r| {
            *r.borrow_mut() = Some(Running {
                raw: Some(raw),
                control: session.control.clone(),
                mouse,
            })
        });
        enter_screen(mouse)?;
        Ok(session)
    }
}

fn enter_screen(mouse: bool) -> io::Result<()> {
    let mut out = io::stdout();
    write!(out, "{ENTER}{}", if mouse { MOUSE_ON } else { "" })?;
    out.flush()
}

impl Drop for Session {
    fn drop(&mut self) {
        // the reader has to finish while reads still time out, on every way out of `run`:
        // once the terminal is back in line mode, a read waits for Enter and would take the
        // first key of whatever reads stdin next
        self.control.stop.store(true, Ordering::SeqCst);
        let _ = self.reader.take().map(std::thread::JoinHandle::join);
        leave_screen();
        // out of line mode, unless a suspended closure panicked and it's already there
        RUNNING.with(|r| r.borrow_mut().take());
        term::forget_terminal();
        if !std::thread::panicking() {
            let previous = self.hook.clone();
            let _ = std::panic::take_hook();
            std::panic::set_hook(Box::new(move |info| previous(info)));
        }
    }
}

fn leave_screen() {
    if ON_SCREEN.swap(false, Ordering::SeqCst) {
        let mut out = io::stdout();
        let _ = out.write_all(LEAVE.as_bytes());
        let _ = out.flush();
    }
}

/// Leave the full-screen app for a moment, to run something that needs the terminal as it
/// normally is: a command that asks for a password, opens an editor, or prints a sign-in
/// link and waits. The screen and keyboard are handed back, `f` runs, and then the app takes
/// them again and redraws from scratch. Call it from [`App::event`].
///
/// While `f` runs, Ctrl-C goes to the commands it starts, not the app. If `f` panics, the
/// terminal is left as it should be. If the app can't take the terminal back afterwards,
/// that's the error, and the app should quit. Outside a running app (in tests, say) `f` simply runs.
///
/// ```no_run
/// use std::process::Command;
/// use tinsel::app::{self, App, Event, Flow, Frame, Key};
///
/// struct Account {
///     signed_in: bool,
/// }
///
/// impl App for Account {
///     fn view(&self, frame: &mut Frame) {
///         let state = if self.signed_in { "signed in" } else { "s to sign in" };
///         frame.print(frame.area(), 0, state);
///     }
///
///     fn event(&mut self, event: &Event) -> Flow {
///         match event {
///             Event::Key(Key::Char('s')) => {
///                 let login = app::suspend(|| Command::new("my-cli").arg("login").status());
///                 self.signed_in = matches!(login, Ok(Ok(status)) if status.success());
///             }
///             Event::Key(Key::Char('q')) => return Flow::Quit,
///             _ => {}
///         }
///         Flow::Continue
///     }
/// }
/// ```
pub fn suspend<T>(f: impl FnOnce() -> T) -> io::Result<T> {
    let mut f = Some(f);
    let mut out = None;
    suspended(&mut || out = f.take().map(|f| f()))?;
    // `suspended` has run it by the time it returns Ok
    out.ok_or(io::ErrorKind::Other.into())
}

/// [`suspend`], for any closure.
fn suspended(f: &mut dyn FnMut()) -> io::Result<()> {
    let Some((control, mouse)) = RUNNING.with(|r| r.borrow().as_ref().map(|r| (r.control.clone(), r.mouse))) else {
        f();
        return Ok(());
    };
    // the reader has to be out of its read before line mode is back, or it takes the first
    // thing typed; reads give up every 0.1s, so this is quick
    control.pause.store(true, Ordering::SeqCst);
    let deadline = Instant::now() + Duration::from_secs(1);
    while !control.idle.load(Ordering::SeqCst) && Instant::now() < deadline {
        std::thread::sleep(Duration::from_millis(5));
    }
    leave_screen();
    RUNNING.with(|r| r.borrow_mut().as_mut().map(|r| r.raw.take()));

    {
        let _ctrl_c = interrupt::Pass::new();
        f();
    }

    let raw = term::RawMode::enter()?;
    RUNNING.with(|r| r.borrow_mut().as_mut().map(|r| r.raw = Some(raw)));
    ON_SCREEN.store(true, Ordering::SeqCst);
    enter_screen(mouse)?;
    REDRAW.with(|r| r.set(true));
    control.pause.store(false, Ordering::SeqCst);
    Ok(())
}

/// Ctrl-C while suspended: the app survives it, and whatever it's running gets it.
#[cfg(unix)]
mod interrupt {
    unsafe extern "C" {
        fn signal(sig: i32, handler: usize) -> usize;
    }

    const SIGINT: i32 = 2;
    const SIG_ERR: usize = usize::MAX;

    // a handler that does nothing, rather than ignoring the signal: ignoring is inherited by
    // the commands run, so they couldn't be stopped either, while a handler is reset for them
    extern "C" fn pass(_: i32) {}

    pub(super) struct Pass(usize);

    impl Pass {
        pub(super) fn new() -> Pass {
            // SAFETY: `pass` does nothing at all, which is async-signal-safe
            Pass(unsafe { signal(SIGINT, pass as extern "C" fn(i32) as usize) })
        }
    }

    impl Drop for Pass {
        fn drop(&mut self) {
            // SAFETY: the previous handler, as signal() returned it
            (self.0 != SIG_ERR).then(|| unsafe { signal(SIGINT, self.0) });
        }
    }
}

#[cfg(not(unix))]
mod interrupt {
    pub(super) struct Pass;

    impl Pass {
        pub(super) fn new() -> Pass {
            Pass
        }
    }
}

/// Read keys and mouse reports until told to stop, sending each as an event. `read` gives
/// one byte, or `None` after a short pause with nothing typed.
fn listen(read: &mut dyn FnMut() -> io::Result<Option<u8>>, tx: &mpsc::Sender<Event>, control: &Control) {
    let mut pending: Vec<u8> = Vec::new();
    while !control.stop.load(Ordering::SeqCst) {
        // busy first, then a look for a pause: a pause asked for before the look is seen,
        // and one asked for after it waits for this read, which `suspend` sees as busy
        control.idle.store(false, Ordering::SeqCst);
        if control.pause.load(Ordering::SeqCst) {
            control.idle.store(true, Ordering::SeqCst);
            std::thread::sleep(Duration::from_millis(10));
            continue;
        }
        // reads give up after 0.1s, so `stop` is checked often and a pause can end a sequence
        let paused = match read() {
            Ok(Some(b)) => {
                pending.push(b);
                false
            }
            Ok(None) => true,
            Err(_) => return,
        };
        while let Some((event, used)) = input::parse(&pending, paused) {
            pending.drain(..used);
            if let Some(event) = event {
                if tx.send(event).is_err() {
                    return;
                }
            }
        }
    }
}

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

    /// Remembers every event, and quits when told to.
    #[derive(Default)]
    struct Recorder {
        seen: Vec<Event>,
        quit_on: Option<Event>,
        quit_after_ticks: Option<usize>,
    }

    impl App for Recorder {
        fn view(&self, frame: &mut Frame) {
            frame.print(frame.area(), 0, &format!("seen {}", self.seen.len()));
        }

        fn event(&mut self, event: &Event) -> Flow {
            self.seen.push(event.clone());
            let ticks = self.seen.iter().filter(|e| **e == Event::Tick).count();
            if Some(event) == self.quit_on.as_ref() || self.quit_after_ticks == Some(ticks) {
                return Flow::Quit;
            }
            Flow::Continue
        }

        fn fps(&self) -> u32 {
            240
        }
    }

    fn keys(rx_events: &[Event]) -> mpsc::Receiver<Event> {
        let (tx, rx) = mpsc::channel();
        for e in rx_events {
            tx.send(e.clone()).unwrap();
        }
        // kept open, so the loop waits on ticks rather than seeing the events stop
        std::mem::forget(tx);
        rx
    }

    #[test]
    fn the_loop_hands_events_over_and_stops_on_ctrl_c() {
        let mut app = Recorder::default();
        let rx = keys(&[
            Event::Key(Key::Char('x')),
            Event::Key(Key::Ctrl('c')),
            Event::Key(Key::Char('y')),
        ]);
        let mut out = Vec::new();
        drive(&mut app, &rx, &mut out, &mut || (20, 2)).unwrap();
        // the app sees Ctrl-C, which quits even so, and nothing after it is handled
        assert_eq!(app.seen, [Event::Key(Key::Char('x')), Event::Key(Key::Ctrl('c'))]);
        let out = String::from_utf8(out).unwrap();
        assert!(out.contains("seen 0") && out.starts_with("\x1b[?2026h"), "{out:?}");
    }

    #[test]
    fn the_title_is_set_once_until_it_changes() {
        struct Titled(usize);
        impl App for Titled {
            fn view(&self, _: &mut Frame) {}
            fn event(&mut self, event: &Event) -> Flow {
                self.0 += usize::from(*event == Event::Tick);
                if self.0 == 4 { Flow::Quit } else { Flow::Continue }
            }
            fn title(&self) -> Option<String> {
                Some(if self.0 < 2 { "one\x07" } else { "two" }.into())
            }
        }
        let rx = keys(&[]);
        let mut out = Vec::new();
        drive(&mut Titled(0), &rx, &mut out, &mut || (10, 2)).unwrap();
        let out = String::from_utf8(out).unwrap();
        assert_eq!(out.matches("\x1b]0;").count(), 2, "{out:?}");
        assert!(
            out.contains("\x1b]0;one\x07") && out.contains("\x1b]0;two\x07"),
            "{out:?}"
        );
    }

    #[test]
    fn the_loop_ticks_resizes_and_quits_when_the_app_says() {
        let mut app = Recorder {
            quit_after_ticks: Some(2),
            ..Recorder::default()
        };
        let rx = keys(&[]);
        let sizes = std::cell::Cell::new(0);
        let mut screen = || {
            sizes.set(sizes.get() + 1);
            if sizes.get() > 2 { (30, 4) } else { (20, 2) }
        };
        drive(&mut app, &rx, &mut Vec::new(), &mut screen).unwrap();
        assert_eq!(app.seen.iter().filter(|e| **e == Event::Tick).count(), 2);
        assert!(app.seen.contains(&Event::Resize(30, 4)), "{:?}", app.seen);

        // and a resize the app quits on ends it there
        let mut app = Recorder {
            quit_on: Some(Event::Resize(9, 9)),
            ..Recorder::default()
        };
        let mut first = true;
        let mut screen = || if std::mem::take(&mut first) { (5, 5) } else { (9, 9) };
        drive(&mut app, &rx, &mut Vec::new(), &mut screen).unwrap();
        assert_eq!(app.seen, [Event::Resize(9, 9)]);
    }

    #[test]
    fn the_loop_ends_when_the_events_do() {
        let (tx, rx) = mpsc::channel();
        drop(tx);
        let mut app = Recorder::default();
        drive(&mut app, &rx, &mut Vec::new(), &mut || (10, 1)).unwrap();
        assert!(app.seen.is_empty());
    }

    #[test]
    fn the_loop_reports_output_that_fails() {
        /// Takes everything, then can't pass it on.
        struct Broken;
        impl Write for Broken {
            fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
                Ok(buf.len())
            }
            fn flush(&mut self) -> io::Result<()> {
                Err(io::Error::other("gone"))
            }
        }
        let rx = keys(&[]);
        assert!(drive(&mut Recorder::default(), &rx, &mut Broken, &mut || (10, 1)).is_err());
    }

    #[test]
    fn wakes_reach_the_app_once_per_burst() {
        let (tx, rx) = mpsc::channel();
        let waker = Waker::new();
        WOKEN.store(false, Ordering::SeqCst);
        // woken before the app starts: delivered as it starts
        waker.wake();
        let waking = Waking::listen(tx);
        let other = waker.clone();
        std::thread::spawn(move || {
            other.wake();
            other.wake();
        })
        .join()
        .unwrap();
        assert_eq!(rx.try_iter().collect::<Vec<_>>(), [Event::Wake]);

        // once handled, the next wake is sent again
        let mut app = Recorder {
            quit_on: Some(Event::Wake),
            ..Recorder::default()
        };
        let (tx2, rx2) = mpsc::channel();
        tx2.send(Event::Wake).unwrap();
        drive(&mut app, &rx2, &mut Vec::new(), &mut || (10, 1)).unwrap();
        waker.wake();
        assert_eq!(rx.try_iter().collect::<Vec<_>>(), [Event::Wake]);
        drop(waking);
        WOKEN.store(false, Ordering::SeqCst);
        waker.wake();
        assert!(WAKE.lock().unwrap().is_none());

        // not woken before it starts: nothing to deliver
        WOKEN.store(false, Ordering::SeqCst);
        let (tx3, rx3) = mpsc::channel();
        drop(Waking::listen(tx3));
        assert!(rx3.try_recv().is_err());
    }

    #[test]
    fn the_reader_turns_bytes_into_events_and_pauses_into_esc() {
        let mut script = vec![
            Ok(Some(b'a')),
            Ok(Some(0x1b)),
            Ok(None),
            Ok(Some(0x1b)),
            Ok(Some(b'[')),
            Ok(Some(b'A')),
            Err(io::Error::other("closed")),
        ]
        .into_iter();
        let (tx, rx) = mpsc::channel();
        listen(&mut move || script.next().unwrap(), &tx, &Control::default());
        let got: Vec<Event> = rx.try_iter().collect();
        assert_eq!(
            got,
            [Event::Key(Key::Char('a')), Event::Key(Key::Esc), Event::Key(Key::Up)]
        );
    }

    #[test]
    fn the_reader_stops_when_told_or_when_nobody_listens() {
        let (tx, _rx) = mpsc::channel();
        let stopped = Control::default();
        stopped.stop.store(true, Ordering::SeqCst);
        // one reader for both, so it can count its reads
        let mut reads = 0;
        let mut read = || {
            reads += 1;
            Ok(Some(b'q'))
        };
        listen(&mut read, &tx, &stopped);

        let (tx, rx) = mpsc::channel();
        drop(rx);
        listen(&mut read, &tx, &Control::default());
        assert_eq!(reads, 1, "nothing read after stop, then one key nobody took");
    }

    #[test]
    fn a_paused_reader_leaves_the_keyboard_alone() {
        let control = Arc::new(Control::default());
        control.pause.store(true, Ordering::SeqCst);
        let (tx, rx) = mpsc::channel();
        let reader = {
            let control = control.clone();
            std::thread::spawn(move || listen(&mut || Ok(Some(b'x')), &tx, &control))
        };
        while !control.idle.load(Ordering::SeqCst) {
            std::thread::sleep(Duration::from_millis(1));
        }
        std::thread::sleep(Duration::from_millis(30));
        assert!(rx.try_recv().is_err(), "nothing read while paused");
        control.pause.store(false, Ordering::SeqCst);
        assert_eq!(rx.recv().unwrap(), Event::Key(Key::Char('x')));
        control.stop.store(true, Ordering::SeqCst);
        reader.join().unwrap();
    }

    #[test]
    fn suspending_outside_an_app_just_runs_the_closure_and_the_screen_is_redrawn_after() {
        assert_eq!(suspend(|| 7).unwrap(), 7);

        // after a suspend, every row is sent again, changed or not
        struct Suspends(usize);
        impl App for Suspends {
            fn view(&self, frame: &mut Frame) {
                frame.print(frame.area(), 0, "same");
            }
            fn event(&mut self, event: &Event) -> Flow {
                if *event == Event::Key(Key::Char('s')) {
                    REDRAW.with(|r| r.set(true));
                }
                self.0 += 1;
                if self.0 == 2 { Flow::Quit } else { Flow::Continue }
            }
            fn fps(&self) -> u32 {
                240
            }
        }
        let rx = keys(&[Event::Key(Key::Char('s'))]);
        let mut out = Vec::new();
        drive(&mut Suspends(0), &rx, &mut out, &mut || (10, 1)).unwrap();
        assert_eq!(String::from_utf8(out).unwrap().matches("same").count(), 2);
    }

    #[test]
    fn navigation_covers_every_key() {
        use MouseKind::*;
        let scroll = |kind| Event::Mouse(Mouse { kind, x: 0, y: 0 });
        let mut at = 5;
        let steps = [
            (Event::Key(Key::Up), 4),
            (Event::Key(Key::Char('k')), 3),
            (Event::Key(Key::Down), 4),
            (Event::Key(Key::Char('j')), 5),
            (Event::Key(Key::PageDown), 15),
            (Event::Key(Key::PageDown), 19),
            (Event::Key(Key::PageUp), 9),
            (Event::Key(Key::Home), 0),
            (Event::Key(Key::Char('G')), 19),
            (Event::Key(Key::Char('g')), 0),
            (scroll(ScrollDown), 1),
            (scroll(ScrollUp), 0),
            (Event::Key(Key::End), 19),
        ];
        for (event, want) in steps {
            navigate(&mut at, 20, &event);
            assert_eq!(at, want, "after {event:?}");
        }
        assert!(!navigate(&mut at, 20, &Event::Key(Key::End)));
        assert!(!navigate(&mut at, 20, &Event::Tick));
        assert!(!navigate(&mut at, 20, &scroll(Click)));
        let mut empty = 0;
        assert!(!navigate(&mut empty, 0, &Event::Key(Key::Down)));
    }

    #[test]
    fn the_default_app_quits_on_q_and_esc() {
        struct Plain;
        impl App for Plain {
            fn view(&self, _: &mut Frame) {}
        }
        let mut app = Plain;
        assert_eq!(app.event(&Event::Key(Key::Char('q'))), Flow::Quit);
        assert_eq!(app.event(&Event::Key(Key::Esc)), Flow::Quit);
        assert_eq!(app.event(&Event::Tick), Flow::Continue);
        assert_eq!((app.fps(), app.mouse()), (crate::fps(), false));
        app.view(&mut Frame::new(1, 1));
    }

    #[test]
    fn the_reader_waits_out_a_pause_and_stops_when_no_one_listens() {
        let control = Arc::new(Control::default());
        control.pause.store(true, Ordering::SeqCst);
        let (tx, rx) = mpsc::channel();
        let unpause = control.clone();
        let lifted = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(50));
            assert!(unpause.idle.load(Ordering::SeqCst), "paused, so not reading");
            unpause.pause.store(false, Ordering::SeqCst);
        });
        // a byte that's no key, a key, then nobody to send the next one to
        let mut script = vec![Ok(Some(0x1c)), Ok(Some(b'a')), Ok(Some(b'b'))].into_iter();
        let mut rx = Some(rx);
        listen(
            &mut || {
                let next = script.next().unwrap_or(Err(io::Error::other("done")));
                if matches!(next, Ok(Some(b'b'))) {
                    assert_eq!(rx.take().unwrap().try_recv().unwrap(), Event::Key(Key::Char('a')));
                }
                next
            },
            &tx,
            &control,
        );
        lifted.join().unwrap();
        assert!(script.next().is_none());
    }

    #[test]
    fn off_a_terminal_an_app_prints_one_frame() {
        crate::set_color_mode(crate::ColorMode::None);
        let out = crate::output::capture(|| {
            let app = crate::output::with_terminal(false, || run(Recorder::default())).unwrap();
            assert!(app.seen.is_empty());
        });
        assert!(out.starts_with("seen 0"), "{out:?}");
    }

    #[cfg(unix)]
    #[test]
    fn ctrl_c_is_passed_on_while_suspended() {
        unsafe extern "C" {
            fn raise(sig: i32) -> i32;
        }
        // drawing tests elsewhere set their own Ctrl-C handler; capturing keeps them apart
        crate::output::capture(|| {
            let pass = interrupt::Pass::new();
            // SAFETY: SIGINT goes to `pass`, which does nothing
            assert_eq!(unsafe { raise(2) }, 0);
            drop(pass);
        });
        assert_eq!(suspend(|| 7).unwrap(), 7);
    }

    #[test]
    fn the_app_clock_runs_while_it_does() {
        struct Timed(f32);
        impl App for Timed {
            fn view(&self, _: &mut Frame) {}
            fn event(&mut self, _: &Event) -> Flow {
                self.0 = time();
                Flow::Quit
            }
        }
        let mut app = Timed(-1.0);
        drive(&mut app, &keys(&[Event::Tick]), &mut Vec::new(), &mut || (1, 1)).unwrap();
        assert!(app.0 >= 0.0);
    }
}