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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! 2026-09-26: The TUI event loop, run on the "metrale-tui" thread. Each
//! iteration waits up to 50 ms for a crossterm event, drains the data
//! channels, runs the tick work when `TICK` (100 ms) has passed, draws a
//! frame, and checks the exit conditions.
//!
//! Owner: server tui.
//! Invariants:
//! - `run` releases `TUI_ACTIVE` on every return: the `ActiveClaim` is taken
//! first, and on the normal exit it is dropped after the terminal guard.
use std::sync::atomic::Ordering;
use std::sync::mpsc::Receiver;
use std::time::{Duration, Instant};
use crossterm::event::{Event, MouseButton, MouseEventKind};
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use super::app::{App, Section};
use super::capture_layer::ProgressEvent;
use super::events_rules::{Exit, LibraryPhase, exit_kind, key_is_actionable, newest, tick_work};
use super::init::{ActiveClaim, TUI_ACTIVE};
use super::terminal_guard::TerminalGuard;
use super::{events_rules, render, shutdown};
const TICK: Duration = Duration::from_millis(100);
pub fn run(
mut app: App,
progress_rx: Receiver<ProgressEvent>,
levers_rx: Receiver<crate::tui::RunHandles>,
) {
// 2026-09-26: Taken first, so its drop releases `TUI_ACTIVE` on every
// return below. `tui::start` sets the flag before spawning this thread.
let claim = ActiveClaim::claim();
super::terminal_guard::install_panic_hook();
let guard = match TerminalGuard::enter() {
Ok(g) => g,
Err(e) => {
tracing::warn!("TUI unavailable ({e}); continuing with plain logs");
return;
}
};
debug_assert!(
TUI_ACTIVE.load(Ordering::SeqCst),
"start() claims the terminal"
);
let mut terminal = match Terminal::new(CrosstermBackend::new(std::io::stdout())) {
Ok(t) => t,
Err(e) => {
drop(guard);
tracing::warn!("TUI terminal init failed ({e}); plain logs");
return;
}
};
let mut last_tick = Instant::now();
// 2026-09-26: Set when a drag ends; consumed after the next draw, whose
// buffer holds the selected text.
let mut copy_after_draw = false;
let mut copy_result: Option<(Result<usize, String>, bool)> = None;
let mut clear_selection = false;
let mut ticks: u32 = 0;
// 2026-09-26: The loop `break`s with its `Exit`, which the code after the
// loop acts on.
let exit = loop {
if crossterm::event::poll(Duration::from_millis(50)).unwrap_or(false) {
match crossterm::event::read() {
Ok(Event::Key(k)) if key_is_actionable(k.kind) => app.on_key(k),
Ok(Event::Mouse(m)) => {
let size = terminal.size().ok();
// 2026-09-26: The copy waits for the next draw. ratatui
// 0.29 resets the buffer it swaps in after each draw, so
// the text is only in the `CompletedFrame` that
// `terminal.draw` returns.
if on_mouse(&mut app, m, size) == MouseOutcome::CopySelection {
copy_after_draw = true;
}
}
Ok(Event::Resize(..)) => {
let _ = terminal.clear();
}
// 2026-09-26: `TerminalGuard::enter` enables bracketed paste,
// so a paste arrives as one event.
Ok(Event::Paste(text)) => app.on_paste(text),
_ => {}
}
}
// 2026-09-26: The newest published run wins; a hot-swap republishes it.
if let Some(h) = newest(&levers_rx) {
app.run = Some(h);
}
for ev in progress_rx.try_iter() {
app.progress.apply(ev);
}
app.chat.pump();
app.bench.pump();
app.help.pump();
if let Some((text, error)) = app.help.take_message() {
app.toast(text, error);
}
// 2026-09-26: Downloads are pumped whatever section is shown, so one
// started in the Library still settles and reports elsewhere.
if let Some(settled) = app.download.pump() {
app.library_dirty = true;
if let crate::tui::download_state::Settled::Finished(_) = settled {
app.repaint = true;
}
}
if let Some((text, error)) = app.download.last_message.take() {
app.toast(text, error);
}
// 2026-09-26: A pending download starts once no job is running, also
// when the running job finished while the switch prompt was open; the
// prompt is then dropped.
app.start_pending_download();
if app.download_switch.is_some() && app.download.job.is_none() {
app.download_switch = None;
}
if last_tick.elapsed() >= TICK {
last_tick = Instant::now();
ticks = ticks.wrapping_add(1);
app.on_tick();
if events_rules::samples_metrics(ticks) {
app.stats.sample(app.run.as_ref());
}
// 2026-09-26: The Library reducer cannot reach `App`, so it raises a
// flag the tick moves into `App::library_dirty`.
if std::mem::take(&mut app.lib.mark_dirty) {
app.library_dirty = true;
}
// 2026-09-26: The tick's lazy work, decided by
// `events_rules::tick_work`.
let work = tick_work(
app.section,
LibraryPhase {
dirty: app.library_dirty,
scan_in_flight: app.lib.scan_in_flight(),
recipes_attached: app.lib.attached(),
recipes_unavailable: app.lib.recipes_unavailable(),
},
);
// 2026-09-26: The scan runs on its own thread and `poll_scan` below
// only `try_recv`s. The dirty flag is cleared only when a scan
// starts.
if work.start_scan {
app.library_dirty = false;
app.lib.start_scan(app.args.cache_dir.as_deref());
}
if work.attach_recipes {
app.attach_recipes();
}
if work.poll_library {
if let Some(found) = app.lib.poll_scan() {
app.library = found;
app.lib.rebuild(&app.library);
}
app.lib.poll(&app.library);
app.lib.poll_date();
// 2026-09-26: Ask for the visible recipe's commit date.
// `want_date_for` returns at once while a date fetch is
// pending, for an id already dated, and for a recipe with its
// own `updated`.
if let Some(id) = app.lib.visible_recipe_id() {
app.lib.want_date_for(&id);
}
}
// 2026-09-26: `load_history` returns at once until
// `history_loaded` is cleared.
if work.load_history {
app.bench.load_history();
}
// 2026-09-26: Whatever section is shown, so a pre-flight started in
// Benchmarks still resolves elsewhere.
app.bench.poll_preflight();
}
if std::mem::take(&mut app.repaint) {
let _ = terminal.clear();
}
match terminal.draw(|f| render::draw(f, &app)) {
Err(e) => {
tracing::warn!("TUI draw error: {e}; detaching");
// 2026-09-26: A failed draw leaves the loop as `/detach` does;
// the server keeps serving.
break Exit::Detach;
}
Ok(frame) => {
// 2026-09-26: `CompletedFrame` borrows the buffer just rendered,
// which holds the on-screen text.
if std::mem::take(&mut copy_after_draw)
&& let Some(sel) = app.selection
{
let text = super::selection::extract(frame.buffer, frame.area, &sel);
copy_result = Some((super::clipboard::copy(&text), text.is_empty()));
// 2026-09-26: The selection is in screen cells, so it is
// cleared once read.
clear_selection = true;
}
}
}
// 2026-09-26: Both of these need `&mut app`, which the frame borrow
// above forbids.
if std::mem::take(&mut clear_selection) {
app.selection = None;
}
if let Some((res, was_empty)) = copy_result.take() {
match res {
// 2026-09-26: "sent", not "copied": `Ok` means the sequence
// was written, not that the terminal applied it.
Ok(n) => app.toast(
format!("sent {n} characters to the terminal clipboard (OSC 52)"),
false,
),
// 2026-09-26: An empty selection is logged at debug level, not
// toasted.
Err(e) if was_empty => tracing::debug!("copy skipped: {e}"),
Err(e) => app.toast(e, true),
}
}
if let Some(e) = exit_kind(app.should_quit, app.detach, shutdown::requested()) {
break e;
}
};
// 2026-09-26: Stop the recipe refresh from starting further files; nothing
// will render its answer.
app.lib.cancel_refresh();
// 2026-09-26: Restore the terminal, then release the claim, so no log line
// is written into the alternate screen. Both drops come before the log
// lines below, which need stdout.
drop(guard);
drop(claim);
let log = super::init::tee_file_path().unwrap_or("-");
match exit {
Exit::Quit => shutdown::request("TUI quit"),
Exit::Detach => {
tracing::info!("TUI detached — plain logs resume (full history: {log})")
}
// 2026-09-26: Not "detached": shutdown was already requested elsewhere
// (a signal, `tui::stop_and_join`), so the server is draining.
Exit::ShuttingDown => {
tracing::info!("TUI closed — shutdown already under way (full history: {log})")
}
}
}
/// 2026-09-26: What the caller must do after a mouse event it cannot do
/// itself.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MouseOutcome {
None,
/// 2026-09-26: A drag finished: read the selection out of the rendered
/// frame and copy.
CopySelection,
}
/// 2026-09-26: What a clicked sidebar row draws.
enum SidebarRow {
/// 2026-09-26: Index into [`Section::ALL`].
Section(usize),
/// 2026-09-26: Subsection of the active section, the only one drawing any.
Sub(usize),
}
/// 2026-09-26: Map a visual sidebar row back to what is drawn on it. The
/// active section's `subs` rows sit directly under it and shift every later
/// section down by `subs`.
fn sidebar_row(active_idx: usize, subs: usize, visual: usize) -> SidebarRow {
if visual <= active_idx {
SidebarRow::Section(visual)
} else if visual <= active_idx + subs {
SidebarRow::Sub(visual - active_idx - 1)
} else {
SidebarRow::Section(visual - subs)
}
}
fn on_mouse(
app: &mut App,
m: crossterm::event::MouseEvent,
size: Option<ratatui::layout::Size>,
) -> MouseOutcome {
let Some(size) = size else {
return MouseOutcome::None;
};
// 2026-09-26: The renderer's own breakpoints (`render::Chrome`).
let chrome = render::Chrome::of(size);
match m.kind {
MouseEventKind::Down(MouseButton::Left) => {
if m.column < chrome.sidebar_w && m.row >= chrome.header_h {
let visual = (m.row - chrome.header_h) as usize;
let active_idx = Section::ALL
.iter()
.position(|s| *s == app.section)
.unwrap_or(0);
// 2026-09-26: The renderer draws `Section::subs` under the
// active section only in the full-width sidebar.
let subs = if chrome.full_sidebar() {
app.section.subs().len()
} else {
0
};
match sidebar_row(active_idx, subs, visual) {
SidebarRow::Section(i) => app.sidebar_click(i),
SidebarRow::Sub(i) => app.sidebar_sub_click(i),
}
// 2026-09-26: A sidebar click does not start a selection.
app.selection = None;
} else if app
.lib_search_click
.get()
.is_some_and(|r| r.contains(ratatui::layout::Position::new(m.column, m.row)))
{
// 2026-09-26: The Library search field, at the rect the
// renderer drew last frame (`App::lib_search_click`).
app.lib.filter_editing = true;
app.selection = None;
} else {
// 2026-09-26: Anywhere else a press starts a selection; nothing
// is copied unless the pointer moves before release.
app.selection = Some(super::selection::Selection::new((m.column, m.row)));
}
}
MouseEventKind::Drag(MouseButton::Left) => {
if let Some(sel) = app.selection.as_mut() {
sel.cursor = (m.column, m.row);
}
}
MouseEventKind::Up(MouseButton::Left) => {
// 2026-09-26: Copy on release only after a drag; a plain click
// clears the selection.
return match app.selection {
Some(sel) if sel.is_drag() => MouseOutcome::CopySelection,
_ => {
app.selection = None;
MouseOutcome::None
}
};
}
// 2026-09-26: Scrolling moves the content under the selection, so it
// clears the selection.
MouseEventKind::ScrollUp => {
app.selection = None;
app.scroll(-3);
}
MouseEventKind::ScrollDown => {
app.selection = None;
app.scroll(3);
}
_ => {}
}
MouseOutcome::None
}
#[cfg(test)]
#[path = "events_tests.rs"]
mod tests;