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
mod app;
mod ui;
use anyhow::{Context, Result};
use crossterm::{
event::{self, Event, KeyCode, KeyModifiers},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use futures_util::{SinkExt, StreamExt};
use hyperstack_sdk::{parse_frame, try_parse_subscribed_frame, ClientMessage, Frame};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_tungstenite::{connect_async, tungstenite::Message};
use self::app::{App, TuiAction, ViewMode};
use super::StreamArgs;
pub async fn run_tui(url: String, view: &str, args: &StreamArgs) -> Result<()> {
// Connect WebSocket
let (ws, _) = connect_async(&url)
.await
.with_context(|| format!("Failed to connect to {}", url))?;
let (mut ws_tx, mut ws_rx) = ws.split();
// Subscribe
let sub = crate::commands::stream::build_subscription(view, args);
let msg = serde_json::to_string(&ClientMessage::Subscribe(sub))?;
ws_tx.send(Message::Text(msg)).await?;
// Channel for frames from WS task
// 10k buffer accommodates large snapshot batches during pause. Overflow
// frames are dropped and counted in the "Dropped: N" header indicator.
let (frame_tx, mut frame_rx) = mpsc::channel::<Frame>(10_000);
// Shutdown signal for graceful WebSocket close
let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel::<()>();
// Dropped frame counter (shared with WS task)
let dropped_frames = Arc::new(AtomicU64::new(0));
let dropped_frames_ws = Arc::clone(&dropped_frames);
// Spawn WS reader task
let ws_handle = tokio::spawn(async move {
let ping_period = std::time::Duration::from_secs(30);
let mut ping_interval =
tokio::time::interval_at(tokio::time::Instant::now() + ping_period, ping_period);
loop {
tokio::select! {
_ = &mut shutdown_rx => {
let _ = ws_tx.close().await;
break;
}
msg = ws_rx.next() => {
match msg {
Some(Ok(Message::Binary(bytes))) => {
match parse_frame(&bytes) {
Ok(frame) => {
if frame_tx.try_send(frame).is_err() {
dropped_frames_ws.fetch_add(1, Ordering::Relaxed);
}
}
Err(_) => {
// Subscribed frames have a different shape (no `entity` field)
if try_parse_subscribed_frame(&bytes).is_some() {
let subscribed = Frame {
mode: hyperstack_sdk::Mode::List,
entity: String::new(),
op: "subscribed".to_string(),
key: String::new(),
data: serde_json::Value::Null,
append: Vec::new(),
seq: None,
};
let _ = frame_tx.try_send(subscribed);
}
}
}
}
Some(Ok(Message::Text(text))) => {
if let Ok(frame) = serde_json::from_str::<Frame>(&text) {
if frame_tx.try_send(frame).is_err() {
dropped_frames_ws.fetch_add(1, Ordering::Relaxed);
}
}
}
Some(Ok(Message::Ping(payload))) => {
let _ = ws_tx.send(Message::Pong(payload)).await;
}
Some(Ok(Message::Close(_))) | Some(Err(_)) | None => break,
_ => {}
}
}
_ = ping_interval.tick() => {
if let Ok(msg) = serde_json::to_string(&ClientMessage::Ping) {
let _ = ws_tx.send(Message::Text(msg)).await;
}
}
}
}
});
// Setup terminal with panic hook to restore on crash.
// We store the original hook in a Mutex so we can reclaim it on normal exit.
let original_hook = Arc::new(std::sync::Mutex::new(Some(std::panic::take_hook())));
let hook_clone = Arc::clone(&original_hook);
std::panic::set_hook(Box::new(move |panic_info| {
let _ = disable_raw_mode();
let _ = execute!(io::stdout(), LeaveAlternateScreen);
if let Ok(guard) = hook_clone.lock() {
if let Some(ref orig) = *guard {
orig(panic_info);
}
}
}));
enable_raw_mode()?;
let terminal_setup = || -> Result<Terminal<CrosstermBackend<io::Stdout>>> {
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout);
Ok(Terminal::new(backend)?)
};
let mut terminal = match terminal_setup() {
Ok(t) => t,
Err(e) => {
let _ = disable_raw_mode();
let _ = execute!(io::stdout(), LeaveAlternateScreen);
return Err(e);
}
};
let mut app = App::new(view.to_string(), url.clone(), Arc::clone(&dropped_frames));
// Main loop: poll terminal events + receive frames
let tick_rate = std::time::Duration::from_millis(50);
let result = run_loop(&mut terminal, &mut app, &mut frame_rx, tick_rate).await;
// Restore terminal (always attempt all steps)
let _ = disable_raw_mode();
let _ = execute!(terminal.backend_mut(), LeaveAlternateScreen,);
let _ = terminal.show_cursor();
// Signal graceful shutdown, then wait briefly for the task to close
let _ = shutdown_tx.send(());
let _ = tokio::time::timeout(std::time::Duration::from_secs(2), ws_handle).await;
// Restore original panic hook (ours is only needed while TUI is active).
// Note: if run_loop panics, this block is unreachable and the TUI hook stays
// installed. This is acceptable since the process terminates on panic anyway.
let _ = std::panic::take_hook(); // drop our TUI hook
if let Ok(mut guard) = original_hook.lock() {
if let Some(hook) = guard.take() {
std::panic::set_hook(hook);
}
}
result
}
async fn run_loop(
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
app: &mut App,
frame_rx: &mut mpsc::Receiver<Frame>,
tick_rate: std::time::Duration,
) -> Result<()> {
loop {
// Update visible rows from terminal size (minus header/timeline/status/borders)
let term_size = terminal.size()?;
// 3 fixed rows (header + timeline + status) + 2 border rows = 5
app.visible_rows = term_size.height.saturating_sub(5) as usize;
app.terminal_width = term_size.width;
terminal.draw(|f| ui::draw(f, app))?;
// Drain available frames (non-blocking). When paused, leave
// frames in the channel so they're applied on resume.
if !app.paused {
loop {
match frame_rx.try_recv() {
Ok(frame) => app.apply_frame(frame),
Err(mpsc::error::TryRecvError::Disconnected) => {
app.set_disconnected();
break;
}
Err(mpsc::error::TryRecvError::Empty) => break,
}
}
}
// Poll for terminal events with timeout
if event::poll(tick_rate)? {
if let Event::Key(key) = event::read()? {
// When filter input is active, capture all keys for typing
let action = if app.filter_input_active {
match key.code {
KeyCode::Esc => TuiAction::BackToList,
KeyCode::Enter => TuiAction::BackToList,
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
TuiAction::Quit
}
KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
TuiAction::FilterClear
}
KeyCode::Char('w') if key.modifiers.contains(KeyModifiers::CONTROL) => {
TuiAction::FilterDeleteWord
}
// Ignore other control/alt combos — don't insert them as text
KeyCode::Char(_)
if key
.modifiers
.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
{
continue
}
KeyCode::Char(c) => TuiAction::FilterChar(c),
KeyCode::Backspace => TuiAction::FilterBackspace,
_ => continue,
}
} else {
// Number prefix accumulation (vim count)
if let KeyCode::Char(c @ '0'..='9') = key.code {
// Don't treat '0' as count start (could be "go to beginning" in future)
if c != '0' || app.pending_count.is_some() {
let digit = c as usize - '0' as usize;
let current = app.pending_count.unwrap_or(0);
app.pending_count = Some(
(current.saturating_mul(10).saturating_add(digit)).min(99_999),
);
app.pending_g = false;
continue;
}
}
match key.code {
KeyCode::Char('q') => TuiAction::Quit,
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
TuiAction::Quit
}
// In Detail mode: j/k scroll the JSON pane; arrows still navigate entities
KeyCode::Char('j') => {
if app.view_mode == ViewMode::Detail {
TuiAction::ScrollDetailDown
} else {
TuiAction::NextEntity
}
}
KeyCode::Char('k') => {
if app.view_mode == ViewMode::Detail {
TuiAction::ScrollDetailUp
} else {
TuiAction::PrevEntity
}
}
KeyCode::Down => TuiAction::NextEntity,
KeyCode::Up => TuiAction::PrevEntity,
KeyCode::Char('G') => {
if app.view_mode == ViewMode::Detail {
TuiAction::ScrollDetailBottom
} else {
TuiAction::GotoBottom
}
}
KeyCode::Char('g') => {
if app.pending_g {
// gg = go to top (of list or detail pane)
if app.view_mode == ViewMode::Detail {
TuiAction::ScrollDetailTop
} else {
TuiAction::GotoTop
}
} else {
app.pending_g = true;
app.pending_count = None;
continue;
}
}
KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if app.view_mode == ViewMode::Detail {
TuiAction::ScrollDetailHalfDown
} else {
TuiAction::HalfPageDown
}
}
KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if app.view_mode == ViewMode::Detail {
TuiAction::ScrollDetailHalfUp
} else {
TuiAction::HalfPageUp
}
}
KeyCode::Char('e') if key.modifiers.contains(KeyModifiers::CONTROL) => {
TuiAction::ScrollDetailDown
}
KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => {
TuiAction::ScrollDetailUp
}
KeyCode::PageDown => TuiAction::ScrollDetailDown,
KeyCode::PageUp => TuiAction::ScrollDetailUp,
KeyCode::Char('n') => TuiAction::NextMatch,
KeyCode::Enter => TuiAction::FocusDetail,
KeyCode::Esc => {
app.pending_count = None;
app.pending_g = false;
TuiAction::BackToList
}
KeyCode::Right | KeyCode::Char('l') => TuiAction::HistoryForward,
KeyCode::Left | KeyCode::Char('h') => {
if app.pending_g {
app.pending_g = false;
continue;
}
TuiAction::HistoryBack
}
KeyCode::Home => TuiAction::HistoryOldest,
KeyCode::End => TuiAction::HistoryNewest,
KeyCode::Char('d') => TuiAction::ToggleDiff,
KeyCode::Char('r') => TuiAction::ToggleRaw,
KeyCode::Char('p') => TuiAction::TogglePause,
KeyCode::Char('/') => TuiAction::StartFilter,
KeyCode::Char('s') => TuiAction::CycleSortMode,
KeyCode::Char('o') => TuiAction::ToggleSortDirection,
KeyCode::Char('S') => TuiAction::SaveSnapshot,
_ => {
app.pending_count = None;
app.pending_g = false;
continue;
}
}
};
if let TuiAction::Quit = action {
break;
}
app.handle_action(action);
}
// Resize and other events are handled implicitly:
// layout is recalculated from terminal.size() at the top of each loop iteration
}
}
Ok(())
}