hypercmd 0.1.2

Retained terminal interfaces authored in HTML and Rust
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
//! Native terminal ownership and the bounded, wake-driven input loop.
use crate::{Error, Scope, layout::LayoutOptions};

/// Limits are checked before terminal modes change and before decoded edits grow.
pub struct NativeOptions {
    pub max_paste_bytes: usize,
    pub max_edit_bytes: usize,
    pub layout: LayoutOptions,
}

impl Default for NativeOptions {
    fn default() -> Self {
        Self {
            max_paste_bytes: crate::controls::PASTE_LIMIT,
            max_edit_bytes: crate::controls::EDIT_LIMIT,
            layout: LayoutOptions::default(),
        }
    }
}

/// Run a prepared root for the terminal application lifetime.
/// See [`run_with`] for the process-signal lifetime contract.
pub fn run(scope: Scope) -> Result<(), Error> {
    run_with(scope, &NativeOptions::default())
}

/// Run with explicit input and layout limits.
///
/// This has the same process-signal lifetime contract as [`run`]: on Unix the
/// upstream registry retains its OS handlers after callback removal. A caller
/// that continues after return must establish its subsequent signal policy.
/// Initial scene/layout validation precedes signal registration and mode changes.
#[cfg_attr(
    unix,
    expect(
        clippy::needless_pass_by_value,
        reason = "the session owns the application root and disposes it on return"
    )
)]
pub fn run_with(scope: Scope, options: &NativeOptions) -> Result<(), Error> {
    #[cfg(unix)]
    {
        unix::run(&scope, options)
    }
    #[cfg(not(unix))]
    {
        let _ = (scope, options);
        Err(Error::terminal(
            "native input is currently tested on Unix; Windows support is experimental and unavailable",
        ))
    }
}

#[cfg(unix)]
mod unix {
    mod screen;
    use super::{Error, LayoutOptions, NativeOptions, Scope};
    use crate::{
        Controller, Input, Services,
        input::{Decoded, Decoder},
        layout,
    };
    use crossterm::{
        cursor::Hide,
        event::{
            EnableBracketedPaste, EnableMouseCapture, KeyboardEnhancementFlags,
            PushKeyboardEnhancementFlags,
        },
        execute,
        terminal::{self, EnterAlternateScreen},
    };
    use mio::{Events, Interest, Poll, Token, unix::SourceFd};
    use ratatui::style::Color;
    use rustix::fs::{OFlags, fcntl_setfl};
    use screen::Screen;
    use signal_hook::{
        SigId,
        consts::{SIGCONT, SIGHUP, SIGINT, SIGSTOP, SIGTERM, SIGTSTP, SIGWINCH},
    };
    use std::{
        io::{self, IsTerminal, Read, Write},
        os::{fd::AsRawFd, unix::net::UnixStream},
        panic::{self, AssertUnwindSafe},
        sync::{
            Arc, Mutex,
            atomic::{AtomicBool, Ordering},
        },
        time::{Duration, Instant},
    };

    static RUNNING: AtomicBool = AtomicBool::new(false);
    const INPUT: Token = Token(0);
    const SIGNAL: Token = Token(1);
    const TASK: Token = Token(2);
    const BYTES_PER_TURN: usize = 256;

    #[cfg(target_os = "macos")]
    const LINK_OPENER: &str = "open";
    #[cfg(not(target_os = "macos"))]
    const LINK_OPENER: &str = "xdg-open";

    pub(super) fn run(scope: &Scope, options: &NativeOptions) -> Result<(), Error> {
        if !io::stdin().is_terminal() || !io::stdout().is_terminal() {
            return Err(Error::terminal(
                "interactive mode requires terminal stdin and stdout; redirected output is unsupported",
            ));
        }
        if std::env::var("TERM").as_deref() == Ok("dumb") {
            return Err(Error::terminal(
                "TERM=dumb cannot provide an interactive screen",
            ));
        }
        if options.max_paste_bytes == 0 || options.max_edit_bytes == 0 {
            return Err(Error::limit("input limits must be greater than zero"));
        }
        if RUNNING.swap(true, Ordering::AcqRel) {
            return Err(Error::terminal(
                "only one Hypercmd terminal session may run at a time",
            ));
        }
        let _running = Running;
        // The UI thread's panic is reported only after Session has unwound.
        let original: Arc<dyn Fn(&panic::PanicHookInfo<'_>) + Send + Sync> =
            panic::take_hook().into();
        let other_threads = original.clone();
        let ui_thread = std::thread::current().id();
        let message = Arc::new(Mutex::new(String::new()));
        let captured = message.clone();
        panic::set_hook(Box::new(move |info| {
            if crate::services::capture_worker_panic(info) {
                return;
            }
            if std::thread::current().id() == ui_thread {
                if let Ok(mut text) = captured.lock() {
                    *text = info.to_string();
                }
            } else {
                other_threads(info);
            }
        }));
        let result = panic::catch_unwind(AssertUnwindSafe(|| run_loop(scope, options)));
        panic::set_hook(Box::new(move |info| original(info)));
        match result {
            Ok(result) => result,
            Err(payload) => {
                if let Ok(message) = message.lock() {
                    eprintln!("{}", safe_diagnostic(&message));
                }
                panic::resume_unwind(payload)
            }
        }
    }

    struct NativeWake {
        inner: mio::Waker,
        failed: AtomicBool,
    }
    impl std::task::Wake for NativeWake {
        fn wake(self: Arc<Self>) {
            self.wake_by_ref();
        }
        fn wake_by_ref(self: &Arc<Self>) {
            if self.inner.wake().is_err() {
                self.failed.store(true, Ordering::Release);
            }
        }
    }

    struct Running;
    impl Drop for Running {
        fn drop(&mut self) {
            RUNNING.store(false, Ordering::Release);
        }
    }

    fn run_loop(scope: &Scope, options: &NativeOptions) -> Result<(), Error> {
        let mut runner = Runner::start(scope, options)?;
        runner.run()?;
        runner.finish()
    }

    enum Reading {
        More,
        Drained,
        Stop,
    }

    // The session must restore the terminal before signal registrations are removed.
    struct Runner<'a> {
        screen: Screen,
        session: Session,
        signals: Signals,
        decoder: Decoder,
        controller: Controller,
        services: Services,
        task_waker: std::task::Waker,
        wake: Arc<NativeWake>,
        events: Events,
        poll: Poll,
        input: std::fs::File,
        bytes: [u8; BYTES_PER_TURN],
        scope: &'a Scope,
        options: &'a NativeOptions,
        size: (u16, u16),
        ready: bool,
        redraw: bool,
    }

    impl<'a> Runner<'a> {
        fn start(scope: &'a Scope, options: &'a NativeOptions) -> Result<Self, Error> {
            let input = open_terminal_input()?;
            let poll = Poll::new().io()?;
            register(&poll, input.as_raw_fd(), INPUT)?;
            let services = Services::from_owner(&scope.owner())?;
            let wake = Arc::new(NativeWake {
                inner: mio::Waker::new(poll.registry(), TASK).io()?,
                failed: AtomicBool::new(false),
            });
            let task_waker = std::task::Waker::from(wake.clone());
            services.register_waker(&task_waker);
            let mut controller = Controller::new(scope.root());
            controller.set_limits(options.max_paste_bytes, options.max_edit_bytes);
            scope.publish();
            services.poll_turn()?;
            check_scene(scope)?;
            let size = terminal::size().io()?;
            // Validate the first complete layout before entering alternate screen/raw mode.
            let focus = controller.focus();
            let scrolls = controller.scrolls_mut();
            layout::render(
                &scope.root(),
                size,
                focus.as_ref(),
                scrolls,
                &options.layout,
            )?;
            let signals = Signals::new().io()?;
            register(&poll, signals.read.as_raw_fd(), SIGNAL)?;
            let mut session = Session::default();
            session.enter(&mut io::stdout()).io()?;
            Ok(Self {
                screen: Screen::new(),
                session,
                signals,
                decoder: Decoder::new(options.max_paste_bytes),
                controller,
                services,
                task_waker,
                wake,
                events: Events::with_capacity(8),
                poll,
                input,
                bytes: [0; BYTES_PER_TURN],
                scope,
                options,
                size,
                ready: false,
                redraw: true,
            })
        }

        fn run(&mut self) -> Result<(), Error> {
            loop {
                let signal_pending = self.signals.drain().io()?;
                if self.signals.shutdown.swap(false, Ordering::AcqRel) {
                    return Ok(());
                }
                self.apply_signals()?;
                self.refresh()?;
                if let Some(input) = self.decoder.expire(Instant::now()) {
                    self.handle_input(input)?;
                    continue;
                }
                if self.ready {
                    match self.read_input()? {
                        Reading::Stop => return Ok(()),
                        Reading::More => continue,
                        Reading::Drained => self.ready = false,
                    }
                }
                self.wait(signal_pending)?;
            }
        }

        fn apply_signals(&mut self) -> Result<(), Error> {
            if self.signals.suspend.swap(false, Ordering::AcqRel) {
                self.session.restore().io()?;
                signal_hook::low_level::raise(SIGSTOP).io()?;
                self.session.enter(&mut io::stdout()).io()?;
                self.screen.clear().io()?;
                self.redraw = true;
            }
            if self.signals.resize.swap(false, Ordering::AcqRel)
                || self.signals.resume.swap(false, Ordering::AcqRel)
            {
                self.size = terminal::size().io()?;
                self.redraw = true;
            }
            Ok(())
        }

        fn refresh(&mut self) -> Result<(), Error> {
            if self.wake.failed.load(Ordering::Acquire) {
                return Err(Error::terminal("task notification failed"));
            }
            self.services.poll_turn()?;
            check_fault(self.scope)?;
            if self.scope.take_dirty() || self.redraw {
                let layout = &self.options.layout;
                draw(
                    self.scope,
                    &mut self.controller,
                    &mut self.screen,
                    self.size,
                    layout,
                )?;
                self.redraw = false;
                check_fault(self.scope)?;
            }
            Ok(())
        }

        // Readiness is edge-triggered: keep reading until WouldBlock, while
        // yielding to shutdown, resize and painting after every buffer.
        fn read_input(&mut self) -> Result<Reading, Error> {
            let count = match rustix::io::read(&self.input, &mut self.bytes) {
                Ok(0) => return Ok(Reading::Stop),
                Ok(count) => count,
                Err(rustix::io::Errno::AGAIN) => return Ok(Reading::Drained),
                Err(rustix::io::Errno::INTR) => return Ok(Reading::More),
                Err(error) => return Err(io_error(&error.into())),
            };
            for index in 0..count {
                match self.decoder.feed(self.bytes[index], Instant::now())? {
                    Some(Decoded::Input(input)) => self.handle_input(input)?,
                    Some(Decoded::Shutdown) => return Ok(Reading::Stop),
                    Some(Decoded::Suspend) => {
                        self.signals.suspend.store(true, Ordering::Release);
                        break;
                    }
                    None => {}
                }
            }
            Ok(Reading::More)
        }

        fn handle_input(&mut self, input: Input) -> Result<(), Error> {
            if let Err(error) = self.open_link(&input) {
                self.scope.root.0.scene.report(error);
            }
            self.controller.handle(input)?;
            check_fault(self.scope)
        }

        fn open_link(&self, input: &Input) -> Result<(), Error> {
            let Input::Click { column, row } = input else {
                return Ok(());
            };
            let Some(target) = self.controller.link_at(*column, *row) else {
                return Ok(());
            };
            if !target.starts_with("https://") && !target.starts_with("http://") {
                return Err(Error::terminal(
                    "Link clicks require an HTTP(S) destination",
                ));
            }
            let target = target.to_owned();
            let opening = self
                .services
                .worker(&fusor_async::CancellationToken::default(), move |_| {
                    launch_link(&target)
                })?;
            let scene = self.scope.root.0.scene.clone();
            self.services.spawn(&self.scope.owner(), async move {
                if let Err(error) = opening.await.and_then(std::convert::identity) {
                    scene.report(error);
                }
            })
        }

        // Even a self-waking task must give newly ready keyboard and signal events
        // a turn; polling with a zero timeout observes readiness without sleeping.
        fn wait(&mut self, signal_pending: bool) -> Result<(), Error> {
            self.services.register_waker(&self.task_waker);
            let busy =
                signal_pending || self.scope.root.0.scene.dirty.get() || self.services.is_ready();
            let timeout = if busy {
                Some(Duration::ZERO)
            } else {
                [
                    self.decoder.timeout(Instant::now()),
                    self.services.timeout(),
                ]
                .into_iter()
                .flatten()
                .min()
            };
            match self.poll.poll(&mut self.events, timeout) {
                Ok(()) => {}
                Err(error) if error.kind() == io::ErrorKind::Interrupted => return Ok(()),
                Err(error) => return Err(io_error(&error)),
            }
            self.ready |= self.events.iter().any(|event| event.token() == INPUT);
            Ok(())
        }

        fn finish(mut self) -> Result<(), Error> {
            self.session.restore().io()?;
            let omitted = self.scope.root.0.scene.omitted.get();
            if omitted > 0 {
                eprintln!(
                    "Limit: {omitted} earlier reactive errors omitted (64-entry diagnostic history)"
                );
            }
            for error in self.scope.take_errors() {
                eprintln!("{}", describe(&error));
            }
            Ok(())
        }
    }

    // An independent open description avoids setting inherited stdout
    // nonblocking when a launcher has duplicated one terminal descriptor.
    fn open_terminal_input() -> Result<std::fs::File, Error> {
        let path = rustix::termios::ttyname(io::stdin(), Vec::new()).io()?;
        let path = <std::ffi::OsStr as std::os::unix::ffi::OsStrExt>::from_bytes(path.to_bytes());
        let input = std::fs::File::open(path).io()?;
        fcntl_setfl(&input, OFlags::NONBLOCK).io()?;
        Ok(input)
    }

    fn register(poll: &Poll, descriptor: std::os::fd::RawFd, token: Token) -> Result<(), Error> {
        poll.registry()
            .register(&mut SourceFd(&descriptor), token, Interest::READABLE)
            .io()
    }

    fn draw(
        scope: &Scope,
        controller: &mut Controller,
        screen: &mut Screen,
        size: (u16, u16),
        options: &LayoutOptions,
    ) -> Result<(), Error> {
        let diagnostic = scope.root.0.scene.errors.borrow().last().cloned();
        let omitted = scope.root.0.scene.omitted.get();
        let focus = controller.focus();
        let content_size = (
            size.0,
            size.1.saturating_sub(u16::from(diagnostic.is_some())),
        );
        let mut presentation = layout::render(
            &scope.root(),
            content_size,
            focus.as_ref(),
            controller.scrolls_mut(),
            options,
        )?;
        presentation
            .buffer
            .resize(ratatui::layout::Rect::new(0, 0, size.0, size.1));
        controller.decorate(&mut presentation);
        let (color, rgb) = color_capabilities();
        for cell in &mut presentation.buffer.content {
            for color_value in [&mut cell.fg, &mut cell.bg] {
                if !color {
                    *color_value = Color::Reset;
                } else if !rgb {
                    *color_value = indexed_color(*color_value);
                }
            }
        }
        if let Some(error) = diagnostic {
            if size.1 > 0 {
                let mut message = describe(&error);
                if omitted > 0 {
                    message = format!("Limit: {omitted} earlier errors omitted; {message}");
                }
                presentation.buffer.set_stringn(
                    0,
                    size.1 - 1,
                    message,
                    usize::from(size.0),
                    ratatui::style::Style::default()
                        .add_modifier(ratatui::style::Modifier::REVERSED),
                );
            }
        }
        screen.draw(&presentation).io()?;
        controller.presented(presentation)
    }

    fn launch_link(target: &str) -> Result<(), Error> {
        let output = std::process::Command::new(LINK_OPENER)
            .arg(target)
            .output()
            .map_err(|error| {
                Error::terminal(format!(
                    "Cannot run {LINK_OPENER}: {error}; check your browser opener installation"
                ))
            })?;
        if !output.status.success() {
            return Err(Error::terminal(format!(
                "{LINK_OPENER} failed ({}): {}; check your default browser",
                output.status,
                String::from_utf8_lossy(&output.stderr).trim()
            )));
        }
        Ok(())
    }

    fn check_fault(scope: &Scope) -> Result<(), Error> {
        if scope.is_faulted() {
            Err(scope
                .take_errors()
                .into_iter()
                .last()
                .unwrap_or_else(|| Error::template("coherent publication faulted the scene")))
        } else {
            Ok(())
        }
    }

    fn check_scene(scope: &Scope) -> Result<(), Error> {
        scope.take_errors().into_iter().next().map_or(Ok(()), Err)
    }
    fn describe(error: &Error) -> String {
        format!("{:?}: {}", error.kind, safe_diagnostic(&error.message))
    }

    fn safe_diagnostic(message: &str) -> String {
        let mut safe = String::new();
        for ch in message.chars().take(4096) {
            if ch.is_control() {
                safe.extend(ch.escape_default());
            } else {
                safe.push(ch);
            }
        }
        safe
    }

    fn color_capabilities() -> (bool, bool) {
        let rgb = std::env::var("COLORTERM")
            .is_ok_and(|color| matches!(color.as_str(), "truecolor" | "24bit"));
        let indexed = std::env::var("TERM").is_ok_and(|term| term.contains("256color"));
        let enabled = std::env::var_os("NO_COLOR").is_none();
        (enabled && (rgb || indexed), enabled && rgb)
    }

    fn indexed_color(color: Color) -> Color {
        const CUBE_LEVELS: [u8; 6] = [0, 95, 135, 175, 215, 255];
        const CUBE_START: usize = 16;
        const GRAY_START: u8 = 232;
        const GRAY_FIRST: u16 = 8;
        const GRAY_STEP: u16 = 10;
        const GRAY_COUNT: u16 = 24;
        let Color::Rgb(red, green, blue) = color else {
            return color;
        };
        let channels = [red, green, blue];
        let cube = channels.map(|channel| {
            CUBE_LEVELS
                .iter()
                .enumerate()
                .min_by_key(|(_, level)| channel.abs_diff(**level))
                .expect("the color cube has six levels")
        });
        let distance = |candidate: [u8; 3]| {
            channels
                .into_iter()
                .zip(candidate)
                .map(|(channel, other)| u32::from(channel.abs_diff(other)).pow(2))
                .sum::<u32>()
        };
        let average = channels.into_iter().map(u16::from).sum::<u16>() / channels.len() as u16;
        let gray_index =
            ((average.saturating_sub(GRAY_FIRST) + GRAY_STEP / 2) / GRAY_STEP).min(GRAY_COUNT - 1);
        let gray_level = (GRAY_FIRST + gray_index * GRAY_STEP) as u8;
        if distance([gray_level; 3]) < distance(cube.map(|(_, level)| *level)) {
            Color::Indexed(GRAY_START + gray_index as u8)
        } else {
            let cube_index = cube.into_iter().fold(0, |index, (coordinate, _)| {
                index * CUBE_LEVELS.len() + coordinate
            });
            Color::Indexed((CUBE_START + cube_index) as u8)
        }
    }

    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    enum ScreenMode {
        Alternate,
        HiddenCursor,
        BracketedPaste,
        Mouse,
        Keyboard,
        Hyperlink,
    }

    impl ScreenMode {
        fn undo(self) -> &'static [u8] {
            match self {
                Self::Alternate => b"\x1b[?1049l",
                Self::HiddenCursor => b"\x1b[?25h",
                Self::BracketedPaste => b"\x1b[?2004l",
                Self::Keyboard => b"\x1b[<1u",
                Self::Hyperlink => b"\x1b]8;;\x1b\\",
                Self::Mouse => b"\x1b[?1006l\x1b[?1015l\x1b[?1003l\x1b[?1002l\x1b[?1000l",
            }
        }
    }

    // A mode is recorded before it is entered, so a partial entry is still undone.
    #[derive(Default)]
    struct Session {
        raw: bool,
        modes: std::collections::BTreeSet<ScreenMode>,
    }

    impl Session {
        fn enter(&mut self, output: &mut impl Write) -> io::Result<()> {
            self.raw = true;
            terminal::enable_raw_mode()?;
            self.modes.insert(ScreenMode::Alternate);
            execute!(output, EnterAlternateScreen)?;
            self.modes.insert(ScreenMode::HiddenCursor);
            execute!(output, Hide)?;
            self.modes.insert(ScreenMode::BracketedPaste);
            execute!(output, EnableBracketedPaste)?;
            self.modes.insert(ScreenMode::Mouse);
            execute!(output, EnableMouseCapture)?;
            self.modes.insert(ScreenMode::Keyboard);
            execute!(
                output,
                PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES)
            )?;
            self.modes.insert(ScreenMode::Hyperlink);
            output.write_all(ScreenMode::Hyperlink.undo())?;
            output.flush()
        }

        // Newest mode first; a mode whose undo fails stays active for the next attempt.
        fn restore(&mut self) -> io::Result<()> {
            let mut first = None;
            let mut output = io::stdout();
            for mode in self.modes.clone().into_iter().rev() {
                match output.write_all(mode.undo()).and_then(|()| output.flush()) {
                    Ok(()) => {
                        self.modes.remove(&mode);
                    }
                    Err(error) => {
                        first.get_or_insert(error);
                    }
                }
            }
            if self.raw {
                match terminal::disable_raw_mode() {
                    Ok(()) => self.raw = false,
                    Err(error) => {
                        first.get_or_insert(error);
                    }
                }
            }
            first.map_or(Ok(()), Err)
        }
    }
    impl Drop for Session {
        fn drop(&mut self) {
            let _ = self.restore();
        }
    }

    struct Signals {
        read: UnixStream,
        registrations: Vec<SigId>,
        shutdown: Arc<AtomicBool>,
        suspend: Arc<AtomicBool>,
        resume: Arc<AtomicBool>,
        resize: Arc<AtomicBool>,
    }
    impl Signals {
        fn new() -> io::Result<Self> {
            let (read, write) = UnixStream::pair()?;
            read.set_nonblocking(true)?;
            let mut signals = Self {
                read,
                registrations: Vec::new(),
                shutdown: Arc::new(AtomicBool::new(false)),
                suspend: Arc::new(AtomicBool::new(false)),
                resume: Arc::new(AtomicBool::new(false)),
                resize: Arc::new(AtomicBool::new(false)),
            };
            for (signal, flag) in [
                (SIGINT, &signals.shutdown),
                (SIGTERM, &signals.shutdown),
                (SIGHUP, &signals.shutdown),
                (SIGTSTP, &signals.suspend),
                (SIGCONT, &signals.resume),
                (SIGWINCH, &signals.resize),
            ] {
                signals
                    .registrations
                    .push(signal_hook::flag::register(signal, flag.clone())?);
                signals
                    .registrations
                    .push(signal_hook::low_level::pipe::register(
                        signal,
                        write.try_clone()?,
                    )?);
            }
            Ok(signals)
        }
        fn drain(&mut self) -> io::Result<bool> {
            let mut bytes = [0; 4096];
            match self.read.read(&mut bytes) {
                Ok(count) => Ok(count == bytes.len()),
                Err(error)
                    if matches!(
                        error.kind(),
                        io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted
                    ) =>
                {
                    Ok(false)
                }
                Err(error) => Err(error),
            }
        }
    }
    impl Drop for Signals {
        fn drop(&mut self) {
            for registration in self.registrations.drain(..) {
                signal_hook::low_level::unregister(registration);
            }
        }
    }
    #[cfg(test)]
    mod tests;

    trait Io<T> {
        fn io(self) -> Result<T, Error>;
    }
    impl<T, E: Into<io::Error>> Io<T> for Result<T, E> {
        fn io(self) -> Result<T, Error> {
            self.map_err(|error| io_error(&error.into()))
        }
    }
    fn io_error(error: &io::Error) -> Error {
        Error::terminal(format!("terminal I/O: {error}"))
    }
}