.cargo/
crates/
brain/
src/
session/
commit.rs
9.3 KiB
event.rs
6.2 KiB
hunk.rs
15.5 KiB
status.rs
31.7 KiB
worker.rs
8.6 KiB
backend.rs
104.7 KiB
comments.rs
22.1 KiB
decorate.rs
6.7 KiB
error.rs
2.6 KiB
main.rs
29.2 KiB
review.rs
2.3 KiB
session.rs
74.7 KiB
Cargo.toml
552 B
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/brain/src/session/event.rs
raw
| 1 | //! Terminal and worker events for the review session. |
| 2 | |
| 3 | use std::io; |
| 4 | use std::sync::mpsc::Sender; |
| 5 | use std::thread; |
| 6 | |
| 7 | use rustik_diff::{ContextChange, HighlightOutput}; |
| 8 | use rustik_tui::event::Modifiers; |
| 9 | use rustik_tui::{Control, Event, Key, Mouse, MouseButton}; |
| 10 | use rustik_tui_termion as tui_termion; |
| 11 | |
| 12 | use super::worker::Output; |
| 13 | |
| 14 | /// Whether a context refetch is in flight. |
| 15 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 16 | pub(super) enum ContextFetch { |
| 17 | /// No context request is currently running. |
| 18 | #[default] |
| 19 | Idle, |
| 20 | /// A context request and at most one desired follow-up are pending. |
| 21 | Pending { |
| 22 | /// Highlighter generation that owns the request. |
| 23 | generation: u64, |
| 24 | /// Most recent context change requested while the worker was busy. |
| 25 | desired: Option<ContextChange>, |
| 26 | }, |
| 27 | } |
| 28 | |
| 29 | impl ContextFetch { |
| 30 | /// Records a context change, returning whether a worker request should start. |
| 31 | pub(super) fn request(&mut self, generation: u64, change: ContextChange) -> bool { |
| 32 | match self { |
| 33 | Self::Pending { |
| 34 | generation: pending, |
| 35 | desired, |
| 36 | } if *pending == generation => { |
| 37 | *desired = Some(change); |
| 38 | false |
| 39 | } |
| 40 | _ => { |
| 41 | *self = Self::Pending { |
| 42 | generation, |
| 43 | desired: None, |
| 44 | }; |
| 45 | true |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Completes this generation's request and returns its desired follow-up. |
| 51 | pub(super) fn complete(&mut self, generation: u64) -> Option<ContextChange> { |
| 52 | match *self { |
| 53 | Self::Pending { |
| 54 | generation: pending, |
| 55 | desired, |
| 56 | } if pending == generation => { |
| 57 | *self = Self::Idle; |
| 58 | desired |
| 59 | } |
| 60 | _ => None, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Cancels this generation's state when its worker rejects the request. |
| 65 | pub(super) fn cancel(&mut self, generation: u64) { |
| 66 | if matches!( |
| 67 | self, |
| 68 | Self::Pending { |
| 69 | generation: pending, |
| 70 | .. |
| 71 | } if *pending == generation |
| 72 | ) { |
| 73 | *self = Self::Idle; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Result of applying one terminal or worker event. |
| 79 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 80 | pub(super) enum Update { |
| 81 | /// Continue without repainting the frame. |
| 82 | Continue, |
| 83 | /// Continue after repainting the frame. |
| 84 | Redraw, |
| 85 | /// Exit the session loop. |
| 86 | Quit, |
| 87 | } |
| 88 | |
| 89 | impl Update { |
| 90 | /// Converts a viewer control result and resize state into a session update. |
| 91 | pub(super) fn from_control(control: Control, resize: Resize) -> Self { |
| 92 | match control { |
| 93 | Control::Quit => Self::Quit, |
| 94 | Control::Redraw => Self::Redraw, |
| 95 | Control::Continue | Control::Wait if resize == Resize::Changed => Self::Redraw, |
| 96 | Control::Continue | Control::Wait => Self::Continue, |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// Whether the terminal size changed during an input event. |
| 102 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 103 | pub(super) enum Resize { |
| 104 | /// The terminal size stayed the same. |
| 105 | Stable, |
| 106 | /// The terminal reported a new size. |
| 107 | Changed, |
| 108 | } |
| 109 | |
| 110 | /// Event delivered to the terminal loop. |
| 111 | pub(super) enum TerminalEvent { |
| 112 | /// Terminal input event. |
| 113 | Input(io::Result<Event<()>>), |
| 114 | /// Background highlighter output, tagged with its generation. |
| 115 | Highlight(u64, HighlightOutput), |
| 116 | /// Completion from the serialized repository worker. |
| 117 | Worker(Box<Output>), |
| 118 | } |
| 119 | |
| 120 | impl TerminalEvent { |
| 121 | /// Receives one channel event, propagating input failures. |
| 122 | pub(super) fn receive<E>(event: Result<Self, E>) -> io::Result<Option<Self>> { |
| 123 | match event { |
| 124 | Ok(Self::Input(Err(error))) => Err(error), |
| 125 | Ok(event) => Ok(Some(event)), |
| 126 | Err(_) => Ok(None), |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Spawns the blocking input reader. |
| 131 | pub(super) fn spawn_input_worker(tx: Sender<Self>) { |
| 132 | thread::spawn(move || { |
| 133 | for event in tui_termion::term::events() { |
| 134 | if tx.send(Self::Input(event)).is_err() { |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | }); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Returns the context change represented by an input event. |
| 143 | pub(super) fn context_change(event: &Event<()>) -> Option<ContextChange> { |
| 144 | match event { |
| 145 | Event::Key(Key::Char('.')) => Some(ContextChange::Increase), |
| 146 | Event::Mouse(Mouse::Press { |
| 147 | button: MouseButton::WheelUp, |
| 148 | modifiers, |
| 149 | .. |
| 150 | }) if modifiers.contains(Modifiers::CONTROL) => Some(ContextChange::Increase), |
| 151 | Event::Key(Key::Char(',')) => Some(ContextChange::Decrease), |
| 152 | Event::Mouse(Mouse::Press { |
| 153 | button: MouseButton::WheelDown, |
| 154 | modifiers, |
| 155 | .. |
| 156 | }) if modifiers.contains(Modifiers::CONTROL) => Some(ContextChange::Decrease), |
| 157 | Event::Key(Key::Char('x')) => Some(ContextChange::ToggleFile), |
| 158 | _ => None, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #[cfg(test)] |
| 163 | mod tests { |
| 164 | use std::sync::mpsc; |
| 165 | |
| 166 | use super::*; |
| 167 | |
| 168 | #[test] |
| 169 | fn terminal_input_error_precedes_channel_disconnect() { |
| 170 | let (tx, rx) = mpsc::channel(); |
| 171 | tx.send(TerminalEvent::Input(Err(io::Error::other( |
| 172 | "terminal input failed", |
| 173 | )))) |
| 174 | .unwrap(); |
| 175 | drop(tx); |
| 176 | |
| 177 | let result = (|| -> io::Result<()> { |
| 178 | while TerminalEvent::receive(rx.recv())?.is_some() {} |
| 179 | Ok(()) |
| 180 | })(); |
| 181 | |
| 182 | let error = result.unwrap_err(); |
| 183 | assert_eq!(error.kind(), io::ErrorKind::Other); |
| 184 | assert_eq!(error.to_string(), "terminal input failed"); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn context_fetch_preserves_two_increases() { |
| 189 | let generation = 7; |
| 190 | let mut fetch = ContextFetch::default(); |
| 191 | let mut context_lines = 3; |
| 192 | |
| 193 | assert!(fetch.request(generation, ContextChange::Increase)); |
| 194 | assert!(!fetch.request(generation, ContextChange::Increase)); |
| 195 | assert_eq!(fetch.complete(generation - 1), None); |
| 196 | |
| 197 | context_lines += 1; |
| 198 | let desired = fetch.complete(generation).unwrap(); |
| 199 | assert_eq!(desired, ContextChange::Increase); |
| 200 | assert!(fetch.request(generation, desired)); |
| 201 | context_lines += 1; |
| 202 | assert_eq!(fetch.complete(generation), None); |
| 203 | |
| 204 | assert_eq!(context_lines, 5); |
| 205 | } |
| 206 | } |