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
//! Terminal event pump: crossterm's async stream plus a periodic tick,
//! multiplexed onto one channel. Kept thin — all decisions live in
//! `App::handle`, which is what the tests drive.
use std::time::Duration;
use crossterm::event::{Event, EventStream, KeyEvent, MouseEvent};
use futures_util::StreamExt as _;
use tokio::sync::mpsc::UnboundedSender;
use tokio::task::JoinHandle;
use crate::mcp::McpRequest;
#[derive(Debug)]
pub enum AppEvent {
Key(KeyEvent),
Mouse(MouseEvent),
Resize,
Tick,
/// Debounced filesystem change from the watcher (`watch` module).
RepoChanged,
/// Agent tool call routed through the event channel so the app stays
/// the single owner of the review state (`mcp` module).
Mcp(McpRequest),
/// A shelled-out network git op finished (`app::GitOp`). The result returns
/// as an event so the run loop keeps drawing while the process runs.
GitDone {
label: String,
ok: bool,
output: String,
},
/// Branch-scoped CI run list from a provider poll.
CiRuns(Vec<diffler_ci::CiRun>),
/// The checked-out branch's PR, fetched once per branch (not every poll).
CiPr(Option<diffler_ci::PullRequest>),
/// A run's jobs + dependency DAG, mapped onto the graph view.
CiRunDetail(diffler_ci::RunDetail),
/// A run's artifacts + annotations for the graph page's extras panel.
CiExtras(diffler_ci::RunExtras),
/// An incremental job-log slice from a provider poll, with the job's step
/// boundaries (empty when the provider exposes none).
CiLog {
text: String,
steps: Vec<diffler_ci::LogStepMeta>,
next_offset: u64,
done: bool,
},
/// A CI provider call failed; surfaced as a status-bar message.
CiError(String),
Quit,
}
const TICK: Duration = Duration::from_millis(250);
/// Forward terminal events and ticks into `tx` until the terminal stream
/// ends or the receiver is dropped.
pub fn spawn_event_loop(tx: UnboundedSender<AppEvent>) -> JoinHandle<()> {
tokio::spawn(async move {
let mut events = EventStream::new();
let mut tick = tokio::time::interval(TICK);
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
let event = tokio::select! {
_ = tick.tick() => Some(AppEvent::Tick),
event = events.next() => match event {
Some(Ok(Event::Key(key))) => Some(AppEvent::Key(key)),
Some(Ok(Event::Mouse(mouse))) => Some(AppEvent::Mouse(mouse)),
Some(Ok(Event::Resize(_, _))) => Some(AppEvent::Resize),
Some(Ok(_)) => None,
Some(Err(_)) | None => {
let _ = tx.send(AppEvent::Quit);
return;
}
},
};
if let Some(event) = event
&& tx.send(event).is_err()
{
return;
}
}
})
}