tui-termion: Read events from the terminal
9158624c80605728d335dd47af09f57c5184d406
We were previously reading events from stdin.
1 parent
adbce514
crates/diff/README.md
+22 -0
| 1 | 1 | # rustik-diff |
|
| 2 | 2 | ||
| 3 | 3 | Pure unified-diff parsing and terminal-buffer rendering for Rustik. |
|
| 4 | 4 | ||
| 5 | + | ## Git |
|
| 6 | + | ||
| 7 | + | Build or install the `rustik-diff` binary, then configure Git to use it for |
|
| 8 | + | diff-like commands: |
|
| 9 | + | ||
| 10 | + | ```sh |
|
| 11 | + | git config --global pager.diff rustik-diff |
|
| 12 | + | git config --global pager.show rustik-diff |
|
| 13 | + | git config --global pager.log rustik-diff |
|
| 14 | + | ``` |
|
| 15 | + | ||
| 16 | + | You can also run it directly: |
|
| 17 | + | ||
| 18 | + | ```sh |
|
| 19 | + | rustik-diff --cached |
|
| 20 | + | git diff HEAD~1 | rustik-diff |
|
| 21 | + | ``` |
|
| 22 | + | ||
| 23 | + | When run without piped input, `rustik-diff` forwards its arguments to |
|
| 24 | + | `git diff`. When Git runs it as a pager, it reads the patch from standard input |
|
| 25 | + | and reads interactive keys from the terminal. |
|
| 26 | + | ||
| 5 | 27 | ## License |
|
| 6 | 28 | ||
| 7 | 29 | MIT. |
crates/tui-termion/src/term.rs
+29 -26
| 2 | 2 | //! |
|
| 3 | 3 | //! This module owns the raw-screen setup and event iterator plumbing needed to |
|
| 4 | 4 | //! run a `rustik_tui` app on a real terminal. Keeping it outside the core crate avoids |
|
| 5 | 5 | //! tying widget rendering to termion. |
|
| 6 | 6 | ||
| 7 | + | use std::fs::File; |
|
| 7 | 8 | use std::io; |
|
| 8 | 9 | use std::io::IsTerminal; |
|
| 9 | 10 | use std::io::Write as _; |
|
| 10 | 11 | use std::sync::mpsc; |
|
| 11 | 12 | use std::thread; |
| 24 | 25 | ||
| 25 | 26 | use crate::TermionBackend; |
|
| 26 | 27 | ||
| 27 | 28 | /// Signals observed by terminal applications. |
|
| 28 | 29 | const SIGNALS: &[signals::Signal] = &[signals::Signal::WindowChanged]; |
|
| 30 | + | /// Controlling terminal path used when standard input is application data. |
|
| 31 | + | const TTY_DEVICE: &str = "/dev/tty"; |
|
| 32 | + | /// Process stdout descriptor path used as a terminal fallback. |
|
| 33 | + | const STDOUT_DEVICE: &str = "/proc/self/fd/1"; |
|
| 34 | + | /// Process stderr descriptor path used as a terminal fallback. |
|
| 35 | + | const STDERR_DEVICE: &str = "/proc/self/fd/2"; |
|
| 29 | 36 | ||
| 30 | 37 | /// Raw alternate stdout screen. |
|
| 31 | 38 | pub type StdoutScreen = AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>; |
|
| 32 | 39 | ||
| 33 | 40 | /// Creates a raw alternate-screen backend on stdout. |
|
| 34 | 41 | pub fn screen() -> io::Result<TermionBackend<StdoutScreen>> { |
|
| 35 | 42 | let screen = MouseTerminal::from(io::stdout().into_raw_mode()?).into_alternate_screen()?; |
|
| 36 | 43 | Ok(TermionBackend::new(screen)) |
|
| 37 | 44 | } |
|
| 38 | 45 | ||
| 39 | - | /// Returns typed events from an input stream. |
|
| 40 | - | pub fn events_from<R: io::Read>(input: R) -> impl Iterator<Item = io::Result<Event<()>>> { |
|
| 41 | - | input.events().map(|event| event.map(crate::event)) |
|
| 42 | - | } |
|
| 43 | - | ||
| 44 | - | /// Returns typed events from stdin. |
|
| 46 | + | /// Returns typed events from the terminal. |
|
| 45 | 47 | pub fn events() -> impl Iterator<Item = io::Result<Event<()>>> { |
|
| 46 | 48 | event_receiver().into_iter() |
|
| 47 | 49 | } |
|
| 48 | 50 | ||
| 51 | + | /// Returns typed events from the terminal device. |
|
| 52 | + | fn terminal_events() -> io::Result<impl Iterator<Item = io::Result<Event<()>>>> { |
|
| 53 | + | let terminal = File::open(TTY_DEVICE) |
|
| 54 | + | .or_else(|_| File::open(STDOUT_DEVICE)) |
|
| 55 | + | .or_else(|_| File::open(STDERR_DEVICE))?; |
|
| 56 | + | ||
| 57 | + | Ok(terminal.events().map(|event| event.map(crate::event))) |
|
| 58 | + | } |
|
| 59 | + | ||
| 49 | 60 | /// Returns the current terminal viewport size. |
|
| 50 | 61 | pub fn viewport() -> Option<Size> { |
|
| 51 | 62 | termion::terminal_size() |
|
| 52 | 63 | .map(|(cols, rows)| Size::new(cols as usize, rows as usize)) |
|
| 53 | 64 | .ok() |
| 116 | 127 | } |
|
| 117 | 128 | } |
|
| 118 | 129 | Ok(()) |
|
| 119 | 130 | } |
|
| 120 | 131 | ||
| 121 | - | /// Spawns a blocking stdin reader and returns its event channel. |
|
| 132 | + | /// Spawns a blocking terminal reader and returns its event channel. |
|
| 122 | 133 | fn event_receiver() -> mpsc::Receiver<io::Result<Event<()>>> { |
|
| 123 | 134 | let (tx, rx) = mpsc::channel(); |
|
| 124 | 135 | ||
| 125 | - | // Spawns a blocking stdin event reader. |
|
| 136 | + | // Spawns a blocking terminal event reader. |
|
| 126 | 137 | thread::spawn({ |
|
| 127 | 138 | let tx = tx.clone(); |
|
| 128 | - | move || { |
|
| 129 | - | for event in events_from(io::stdin()) { |
|
| 130 | - | if tx.send(event).is_err() { |
|
| 131 | - | break; |
|
| 139 | + | move || match terminal_events() { |
|
| 140 | + | Ok(events) => { |
|
| 141 | + | for event in events { |
|
| 142 | + | if tx.send(event).is_err() { |
|
| 143 | + | break; |
|
| 144 | + | } |
|
| 132 | 145 | } |
|
| 133 | 146 | } |
|
| 147 | + | Err(error) => { |
|
| 148 | + | let _ = tx.send(Err(error)); |
|
| 149 | + | } |
|
| 134 | 150 | } |
|
| 135 | 151 | }); |
|
| 136 | 152 | // Spawns a signal notification reader. |
|
| 137 | 153 | thread::spawn(move || match signals::Signals::install(SIGNALS) { |
|
| 138 | 154 | Ok(signals) => { |
| 180 | 196 | } |
|
| 181 | 197 | ||
| 182 | 198 | #[cfg(test)] |
|
| 183 | 199 | #[allow(clippy::unwrap_used)] |
|
| 184 | 200 | mod tests { |
|
| 185 | - | use std::io::Cursor; |
|
| 186 | - | ||
| 187 | 201 | use super::*; |
|
| 188 | 202 | use rustik_tui_core as tui_core; |
|
| 189 | - | use tui_core::{Key, Label}; |
|
| 190 | - | ||
| 191 | - | #[test] |
|
| 192 | - | fn events_from_maps_termion_input_stream() { |
|
| 193 | - | let events = events_from(Cursor::new([b'a', b'\n', 0x1b])) |
|
| 194 | - | .collect::<io::Result<Vec<_>>>() |
|
| 195 | - | .unwrap(); |
|
| 196 | - | ||
| 197 | - | assert_eq!(events[0], Event::Key(Key::Char('a'))); |
|
| 198 | - | assert_eq!(events[1], Event::Key(Key::Enter)); |
|
| 199 | - | assert_eq!(events[2], Event::Key(Key::Esc)); |
|
| 200 | - | } |
|
| 203 | + | use tui_core::Label; |
|
| 201 | 204 | ||
| 202 | 205 | #[test] |
|
| 203 | 206 | fn update_redraws_after_resize_even_when_app_continues() { |
|
| 204 | 207 | struct ResizeAware; |
|
| 205 | 208 |