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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Event handling and main TUI loop
use std::io;
use anyhow::Result;
use crossterm::{
event::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, KeyModifiers,
},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};
use super::app::{App, AppMode, ExportFormat};
use super::ui;
/// Run the TUI application
pub fn run_tui() -> Result<()> {
// Setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// Create app state
let mut app = App::new()?;
// Main loop
let res = run_app(&mut terminal, &mut app);
// Restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Err(err) = res {
eprintln!("Error: {}", err);
}
Ok(())
}
/// Main application loop
fn run_app<B: ratatui::backend::Backend>(terminal: &mut Terminal<B>, app: &mut App) -> Result<()> {
// Initial draw
terminal.draw(|f| ui::render(f, app))?;
loop {
// Block waiting for input - no polling delay, instant response
if let Event::Key(key) = event::read()? {
// Only handle key press events, ignore release/repeat to prevent double-triggering
if key.kind != KeyEventKind::Press {
continue;
}
// Clear status message on any keypress (unless in confirm flow)
if !app.confirm_delete {
app.status_message = None;
}
// --- Export format picker ---
if app.export_picker_active {
match key.code {
KeyCode::Char('j') | KeyCode::Down => {
let max = ExportFormat::all().len();
if app.export_format_index + 1 < max {
app.export_format_index += 1;
}
}
KeyCode::Char('k') | KeyCode::Up => {
if app.export_format_index > 0 {
app.export_format_index -= 1;
}
}
KeyCode::Enter => app.confirm_export(),
KeyCode::Esc => app.cancel_export(),
KeyCode::Char('1') => {
app.export_format_index = 0;
app.confirm_export();
}
KeyCode::Char('2') => {
app.export_format_index = 1;
app.confirm_export();
}
KeyCode::Char('3') => {
app.export_format_index = 2;
app.confirm_export();
}
KeyCode::Char('4') => {
app.export_format_index = 3;
app.confirm_export();
}
_ => {}
}
terminal.draw(|f| ui::render(f, app))?;
continue;
}
// --- Global search input ---
if app.search_active {
match key.code {
KeyCode::Enter => app.execute_search(),
KeyCode::Esc => app.cancel_search(),
KeyCode::Backspace => app.search_backspace(),
KeyCode::Char(c) => app.search_input(c),
_ => {}
}
terminal.draw(|f| ui::render(f, app))?;
continue;
}
// --- Workspace filter input ---
if app.filter_active {
match key.code {
KeyCode::Enter => app.confirm_filter(),
KeyCode::Esc => app.cancel_filter(),
KeyCode::Backspace => app.filter_backspace(),
KeyCode::Char(c) => app.filter_input(c),
_ => {}
}
terminal.draw(|f| ui::render(f, app))?;
continue;
}
// --- Session filter input ---
if app.session_filter_active {
match key.code {
KeyCode::Enter => app.confirm_session_filter(),
KeyCode::Esc => app.cancel_session_filter(),
KeyCode::Backspace => app.session_filter_backspace(),
KeyCode::Char(c) => app.session_filter_input(c),
_ => {}
}
terminal.draw(|f| ui::render(f, app))?;
continue;
}
// Help mode - any key closes it
if app.mode == AppMode::Help {
app.back();
terminal.draw(|f| ui::render(f, app))?;
continue;
}
// Normal key handling
match key.code {
KeyCode::Char('q') => {
return Ok(());
}
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
return Ok(());
}
KeyCode::Char('?') => {
app.toggle_help();
}
// Navigation
KeyCode::Char('j') | KeyCode::Down => {
app.navigate_down();
}
KeyCode::Char('k') | KeyCode::Up => {
app.navigate_up();
}
KeyCode::Char('g') => {
app.go_to_top();
}
KeyCode::Char('G') => {
app.go_to_bottom();
}
KeyCode::PageUp => {
app.page_up();
}
KeyCode::PageDown => {
app.page_down();
}
KeyCode::Enter => {
app.enter();
}
KeyCode::Esc | KeyCode::Backspace => {
if app.confirm_delete {
app.cancel_delete();
} else {
app.back();
}
}
// Filter (/ works in workspaces and sessions)
KeyCode::Char('/') if matches!(app.mode, AppMode::Workspaces | AppMode::Sessions) => {
app.start_filter();
}
// Global search
KeyCode::Char('s') if !matches!(app.mode, AppMode::SessionDetail) => {
app.start_search();
}
// Refresh
KeyCode::Char('r') => {
app.refresh();
}
// Export
KeyCode::Char('e') if matches!(app.mode, AppMode::Sessions | AppMode::SessionDetail | AppMode::SearchResults) => {
app.export_current_session();
}
// Sort (sessions view)
KeyCode::Char('o') if app.mode == AppMode::Sessions => {
app.cycle_sort();
}
// Delete (sessions view)
KeyCode::Char('d') if matches!(app.mode, AppMode::Sessions | AppMode::SessionDetail) => {
app.delete_current_session();
}
// Yank (copy) session content
KeyCode::Char('y') if matches!(app.mode, AppMode::Sessions | AppMode::SessionDetail | AppMode::SearchResults) => {
app.yank_session();
}
_ => continue, // No redraw needed for unhandled keys
}
// Redraw after handling input
terminal.draw(|f| ui::render(f, app))?;
}
}
}