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
pub mod app;
pub mod event;
pub mod theme;
pub mod ui;
pub use app::App;
pub use theme::{resolve_theme, Theme, ThemeColors};
use std::time::Duration;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use event::{Event, EventHandler};
pub async fn run_tui(mut app: App, mut client: octocrab::Octocrab) -> anyhow::Result<()> {
// Buffer stderr while TUI is active to prevent output corrupting the display
crate::stderr_buffer::activate();
// Init terminal (sets up panic hooks automatically)
let mut terminal = ratatui::init();
// Create event handler with tick rate and auto-refresh interval
let refresh_secs = app.config.auto_refresh_interval;
let mut events = EventHandler::new(250, refresh_secs); // 250ms tick, N-second refresh
// Spawn initial fetch as background task
let client_clone = client.clone();
let config_clone = app.config.clone();
let snooze_clone = app.snooze_state.clone();
let cache_config_clone = app.cache_config.clone();
let verbose = app.verbose;
let auth_username_clone = app.auth_username.clone();
let mut pending_fetch: Option<tokio::task::JoinHandle<_>> = Some(tokio::spawn(async move {
tokio::time::timeout(
Duration::from_secs(20),
crate::fetch::fetch_and_score_prs(
&client_clone,
&config_clone,
&snooze_clone,
&cache_config_clone,
verbose,
auth_username_clone.as_deref(),
),
)
.await
}));
app.is_loading = true;
// Spawn background version check (after TUI renders, non-blocking)
let mut pending_version_check: Option<tokio::task::JoinHandle<_>> = if !app.no_version_check {
// First, check if we have a fresh cached result (synchronous, instant)
let current_version = env!("CARGO_PKG_VERSION").to_string();
let cached_status = crate::version_check::load_cached_status(¤t_version);
match &cached_status {
crate::version_check::VersionStatus::UpdateAvailable { .. } => {
app.set_version_status(cached_status);
None // No need to fetch, cache is fresh
}
_ => {
// Spawn background check
let token = std::env::var("PR_BRO_GH_TOKEN").ok();
token.map(|t| {
tokio::spawn(async move {
crate::version_check::check_version(&t, ¤t_version).await
})
})
}
}
} else {
None
};
// Main loop
loop {
// Draw UI
terminal.draw(|frame| ui::draw(frame, &mut app))?;
// Handle events
match events.next().await {
Event::Key(key) => {
app.last_interaction = std::time::Instant::now();
handle_key_event(&mut app, key);
}
Event::Tick => {
app.update_flash();
app.advance_spinner();
}
Event::Refresh => {
app.needs_refresh = true;
}
}
// Check if background fetch has completed
if let Some(handle) = &mut pending_fetch {
if handle.is_finished() {
let handle = pending_fetch.take().unwrap();
match handle.await {
Ok(Ok(Ok((active, snoozed, rate_limit)))) => {
app.update_prs(active, snoozed, rate_limit);
}
Ok(Ok(Err(e))) => {
if e.downcast_ref::<crate::fetch::AuthError>().is_some() {
// Auth failure: restore terminal, re-prompt, re-init
ratatui::restore();
match crate::credentials::reprompt_for_token() {
Ok(new_token) => {
// Recreate client with new token
match crate::github::create_client(
&new_token,
&app.cache_config,
) {
Ok((new_client, new_cache_handle)) => {
client = new_client.clone();
if new_cache_handle.is_some() {
app.cache_handle = new_cache_handle;
}
// Re-fetch authenticated username
let new_username = new_client
.current()
.user()
.await
.ok()
.map(|u| u.login);
app.auth_username = new_username;
// Re-init terminal
terminal = ratatui::init();
// Trigger immediate refresh with new client
app.needs_refresh = true;
app.show_flash(
"Re-authenticated. Refreshing...".to_string(),
);
}
Err(ce) => {
// Re-init terminal even on failure (must restore TUI)
terminal = ratatui::init();
app.show_flash(format!("Re-auth failed: {}", ce));
}
}
}
Err(pe) => {
// User cancelled or error during prompt
// Re-init terminal (must restore TUI)
terminal = ratatui::init();
app.show_flash(format!("Re-auth cancelled: {}", pe));
}
}
} else {
app.show_flash(format!("Refresh failed: {}", e));
}
}
Ok(Err(_elapsed)) => {
// Timeout: fetch took longer than 20 seconds
app.show_flash(
"Refresh timed out (20s). Will retry on next refresh.".to_string(),
);
}
Err(e) => {
app.show_flash(format!("Refresh task panicked: {}", e));
}
}
app.is_loading = false;
}
}
// Check if background version check completed
if let Some(handle) = &mut pending_version_check {
if handle.is_finished() {
let handle = pending_version_check.take().unwrap();
if let Ok(status) = handle.await {
app.set_version_status(status);
}
// Silently ignore join errors
}
}
// Spawn new refresh if needed and no fetch is pending
if app.needs_refresh && pending_fetch.is_none() {
// Check if this is a manual refresh (force_refresh) or auto-refresh
let is_manual = app.force_refresh;
let modal_open = app.input_mode != app::InputMode::Normal;
let recent_interaction = app.last_interaction.elapsed() < Duration::from_secs(10);
// Suppress auto-refresh if modal is open or user interacted recently.
// Manual refresh ('r' key) always proceeds.
// When suppressed, needs_refresh stays true so it retries on the next tick.
if is_manual || (!modal_open && !recent_interaction) {
app.needs_refresh = false;
if is_manual {
if let Some(cache) = &app.cache_handle {
cache.clear_memory();
}
app.force_refresh = false;
}
// Spawn background fetch
let client_clone = client.clone();
let config_clone = app.config.clone();
let snooze_clone = app.snooze_state.clone();
let cache_config_clone = app.cache_config.clone();
let verbose = app.verbose;
let auth_username_clone = app.auth_username.clone();
pending_fetch = Some(tokio::spawn(async move {
tokio::time::timeout(
Duration::from_secs(20),
crate::fetch::fetch_and_score_prs(
&client_clone,
&config_clone,
&snooze_clone,
&cache_config_clone,
verbose,
auth_username_clone.as_deref(),
),
)
.await
}));
app.is_loading = true;
}
}
if app.should_quit {
break;
}
}
// Restore terminal
ratatui::restore();
// Flush buffered stderr messages now that the terminal is restored
for msg in crate::stderr_buffer::drain() {
eprintln!("{}", msg);
}
Ok(())
}
fn handle_key_event(app: &mut App, key: KeyEvent) {
match app.input_mode {
app::InputMode::Normal => {
match key.code {
// Quit
KeyCode::Char('q') => app.should_quit = true,
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
app.should_quit = true
}
// Navigation
KeyCode::Char('j') | KeyCode::Down => app.next_row(),
KeyCode::Char('k') | KeyCode::Up => app.previous_row(),
// Open PR in browser
KeyCode::Enter | KeyCode::Char('o') => {
if let Some(pr) = app.selected_pr() {
let title = pr.title.clone();
if let Err(e) = app.open_selected() {
app.show_flash(format!("Failed to open browser: {}", e));
} else {
app.show_flash(format!("Opened: {}", title));
}
}
}
// Snooze
KeyCode::Char('s') => app.start_snooze_input(),
// Unsnooze
KeyCode::Char('u') => app.unsnooze_selected(),
// Undo
KeyCode::Char('z') => app.undo_last(),
// Tab switching
KeyCode::Tab => app.toggle_view(),
// Refresh (manual = force fresh data)
KeyCode::Char('r') => {
app.needs_refresh = true;
app.force_refresh = true;
app.show_flash("Refreshing (fresh data)...".to_string());
}
// Help
KeyCode::Char('?') => app.show_help(),
// Score breakdown
KeyCode::Char('b') => app.show_score_breakdown(),
// Dismiss update banner
KeyCode::Char('x') if app.has_update_banner() => {
app.dismiss_update_banner();
}
_ => {}
}
}
app::InputMode::SnoozeInput => {
match key.code {
// Confirm snooze
KeyCode::Enter => app.confirm_snooze_input(),
// Cancel snooze
KeyCode::Esc => app.cancel_snooze_input(),
// Backspace
KeyCode::Backspace => {
app.snooze_input.pop();
}
// Character input (alphanumeric + space)
KeyCode::Char(c) if c.is_alphanumeric() || c == ' ' => {
app.snooze_input.push(c);
}
// Ignore all other keys (don't propagate to Normal mode)
_ => {}
}
}
app::InputMode::ScoreBreakdown => match key.code {
KeyCode::Esc | KeyCode::Char('b') => app.dismiss_score_breakdown(),
KeyCode::Char('j') | KeyCode::Down => app.next_row(),
KeyCode::Char('k') | KeyCode::Up => app.previous_row(),
_ => {}
},
app::InputMode::Help => {
// Any key exits help
app.dismiss_help();
}
}
}