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
use std::collections::HashSet;
use std::error::Error;
use std::io;
use std::time::{Duration, Instant};
use crossterm::event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode, KeyModifiers, MouseEventKind};
use crossterm::execute;
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode};
use ratatui::Terminal;
use ratatui::backend::{Backend, CrosstermBackend};
use super::DependencyMap;
use super::app::App;
use super::header::Tab;
use super::tabs::{_render, tab1_components, tab2_hooks, tab5_demos};
pub fn run(
tick_rate: Duration,
components: Vec<String>,
installed: HashSet<String>,
dependencies: DependencyMap,
) -> Result<Vec<String>, Box<dyn Error>> {
// 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 and run it
let app = App::new("Rust/UI CLI", components, installed, dependencies);
let app_result = run_app(&mut terminal, app, tick_rate);
// Restore terminal
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture)?;
terminal.show_cursor()?;
app_result
}
/* ========================================================== */
/* ✨ FUNCTIONS ✨ */
/* ========================================================== */
fn run_app<B: Backend>(
terminal: &mut Terminal<B>,
mut app: App,
tick_rate: Duration,
) -> Result<Vec<String>, Box<dyn Error>>
where
<B as Backend>::Error: 'static,
{
let mut last_tick = Instant::now();
loop {
terminal.draw(|frame| _render::render(frame, &mut app))?;
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if !event::poll(timeout)? {
app.on_tick();
last_tick = Instant::now();
continue;
}
match event::read()? {
event::Event::Key(key) if key.kind == event::KeyEventKind::Press => {
// Handle search mode in Components tab
if app.components_search_active && matches!(app.header.tabs.current, Tab::Components) {
match key.code {
KeyCode::Char('j') | KeyCode::Down => app.on_down(),
KeyCode::Char('k') | KeyCode::Up => app.on_up(),
KeyCode::Char(c) => app.components_search_input(c),
KeyCode::Backspace => app.components_search_backspace(),
KeyCode::Esc => app.toggle_components_search(),
_ => {}
}
// Handle search mode in Demos tab
} else if app.demos_search_active && matches!(app.header.tabs.current, Tab::Demos) {
match key.code {
KeyCode::Char('j') | KeyCode::Down => app.on_down(),
KeyCode::Char('k') | KeyCode::Up => app.on_up(),
KeyCode::Char(c) => app.demos_search_input(c),
KeyCode::Backspace => app.demos_search_backspace(),
KeyCode::Esc => app.toggle_demos_search(),
_ => {}
}
// Handle search mode in Hooks tab
} else if app.hooks_search_active && matches!(app.header.tabs.current, Tab::Hooks) {
match key.code {
KeyCode::Char('j') | KeyCode::Down => app.on_down(),
KeyCode::Char('k') | KeyCode::Up => app.on_up(),
KeyCode::Char(c) => app.hooks_search_input(c),
KeyCode::Backspace => app.hooks_search_backspace(),
KeyCode::Esc => app.toggle_hooks_search(),
_ => {}
}
} else {
match key.code {
KeyCode::Char('?') => {
app.toggle_help_popup();
}
KeyCode::Char('/') if matches!(app.header.tabs.current, Tab::Components) => {
app.toggle_components_search();
}
KeyCode::Char('/') if matches!(app.header.tabs.current, Tab::Demos) => {
app.toggle_demos_search();
}
KeyCode::Char('/') if matches!(app.header.tabs.current, Tab::Hooks) => {
app.toggle_hooks_search();
}
KeyCode::Char(' ') if matches!(app.header.tabs.current, Tab::Components) => {
if let Some(component) = tab1_components::get_selected_component(&app) {
app.toggle_component_checkbox(&component);
}
}
KeyCode::Char(' ') if matches!(app.header.tabs.current, Tab::Demos) => {
if let Some(demo) = tab5_demos::get_selected_demo(&app) {
app.toggle_demo_checkbox(&demo);
}
}
KeyCode::Char(' ') if matches!(app.header.tabs.current, Tab::Hooks) => {
if let Some(hook) = tab2_hooks::get_selected_hook(&app) {
app.toggle_hook_checkbox(hook);
}
}
// Ctrl+letter to jump to first item starting with that letter
KeyCode::Char(c)
if key.modifiers.contains(KeyModifiers::CONTROL)
&& c.is_ascii_alphabetic() =>
{
match app.header.tabs.current {
Tab::Components => app.jump_to_letter_components(c),
Tab::Demos => app.jump_to_letter_demos(c),
Tab::Hooks => app.jump_to_letter_hooks(c),
_ => {}
}
}
KeyCode::Enter
if matches!(app.header.tabs.current, Tab::Components)
&& !app.show_popup
&& !app.components_checked.is_empty() =>
{
app.toggle_popup();
}
// Handle Enter in popup - confirm or cancel based on button focus
KeyCode::Enter
if matches!(app.header.tabs.current, Tab::Components)
&& app.show_popup
&& !app.components_checked.is_empty() =>
{
if app.popup_confirm_focused {
let selected: Vec<String> = app.components_checked.into_iter().collect();
return Ok(selected);
} else {
app.toggle_popup(); // Cancel - close popup
}
}
KeyCode::Enter
if matches!(app.header.tabs.current, Tab::Demos)
&& !app.show_popup
&& !app.demos_checked.is_empty() =>
{
app.toggle_popup();
}
KeyCode::Enter
if matches!(app.header.tabs.current, Tab::Demos)
&& app.show_popup
&& !app.demos_checked.is_empty() =>
{
if app.popup_confirm_focused {
let selected: Vec<String> = app.demos_checked.into_iter().collect();
return Ok(selected);
} else {
app.toggle_popup(); // Cancel - close popup
}
}
KeyCode::Enter
if matches!(app.header.tabs.current, Tab::Hooks)
&& !app.show_popup
&& !app.hooks_checked.is_empty() =>
{
app.toggle_popup();
}
KeyCode::Enter
if matches!(app.header.tabs.current, Tab::Hooks)
&& app.show_popup
&& !app.hooks_checked.is_empty() =>
{
if app.popup_confirm_focused {
let selected: Vec<String> = app.hooks_checked.into_iter().collect();
return Ok(selected);
} else {
app.toggle_popup(); // Cancel - close popup
}
}
KeyCode::Esc if app.show_help_popup => {
app.toggle_help_popup();
}
KeyCode::Esc
if matches!(app.header.tabs.current, Tab::Components) && !app.show_popup =>
{
// Handle double-tap Escape to deselect all components
let now = Instant::now();
let is_double_tap = if let Some(last_time) = app.last_escape_time {
now.duration_since(last_time).as_millis() < 500
} else {
false
};
if is_double_tap && !app.components_checked.is_empty() {
app.deselect_all_components();
app.last_escape_time = None;
} else {
app.last_escape_time = Some(now);
}
}
KeyCode::Esc
if matches!(app.header.tabs.current, Tab::Components) && app.show_popup =>
{
app.toggle_popup();
}
KeyCode::Esc if matches!(app.header.tabs.current, Tab::Demos) && !app.show_popup => {
// Handle double-tap Escape to deselect all demos
let now = Instant::now();
let is_double_tap = if let Some(last_time) = app.last_escape_time {
now.duration_since(last_time).as_millis() < 500
} else {
false
};
if is_double_tap && !app.demos_checked.is_empty() {
app.deselect_all_demos();
app.last_escape_time = None;
} else {
app.last_escape_time = Some(now);
}
}
KeyCode::Esc if matches!(app.header.tabs.current, Tab::Demos) && app.show_popup => {
app.toggle_popup();
}
KeyCode::Esc if matches!(app.header.tabs.current, Tab::Hooks) && !app.show_popup => {
// Handle double-tap Escape to deselect all hooks
let now = Instant::now();
let is_double_tap = if let Some(last_time) = app.last_escape_time {
now.duration_since(last_time).as_millis() < 500
} else {
false
};
if is_double_tap && !app.hooks_checked.is_empty() {
app.deselect_all_hooks();
app.last_escape_time = None;
} else {
app.last_escape_time = Some(now);
}
}
KeyCode::Esc if matches!(app.header.tabs.current, Tab::Hooks) && app.show_popup => {
app.toggle_popup();
}
KeyCode::Char('h') | KeyCode::Left => {
if app.show_popup {
app.toggle_popup_button_focus();
} else if !app.show_help_popup {
app.on_left();
}
}
KeyCode::Char('j') | KeyCode::Down => {
if !app.show_popup && !app.show_help_popup {
app.on_down();
}
}
KeyCode::Char('k') | KeyCode::Up => {
if !app.show_popup && !app.show_help_popup {
app.on_up();
}
}
KeyCode::Char('l') | KeyCode::Right => {
if app.show_popup {
app.toggle_popup_button_focus();
} else if !app.show_help_popup {
app.on_right();
}
}
KeyCode::Tab if app.show_popup => {
app.toggle_popup_button_focus();
}
KeyCode::Char(c) => app.on_key(c),
_ => {}
}
}
}
event::Event::Mouse(mouse) => {
match mouse.kind {
MouseEventKind::Down(_) => {
let terminal_width = terminal.size()?.width;
let now = std::time::Instant::now();
let current_pos = (mouse.column, mouse.row);
// Check for double-click (within 500ms and same position)
let is_double_click = if let (Some(last_time), Some(last_pos)) =
(app.last_click_time, app.last_click_pos)
{
now.duration_since(last_time).as_millis() < 500 && last_pos == current_pos
} else {
false
};
if is_double_click {
// Handle double-click on component list items
if let Some(visual_index) =
app.get_components_double_click_info(mouse.column, mouse.row, terminal_width)
&& let Some(component) =
tab1_components::get_component_at_visual_index(&app, visual_index)
{
app.toggle_component_checkbox(&component);
}
// Handle double-click on demo list items
if let Some(visual_index) =
app.get_demos_double_click_info(mouse.column, mouse.row, terminal_width)
&& let Some(demo) = tab5_demos::get_demo_at_visual_index(&app, visual_index)
{
app.toggle_demo_checkbox(&demo);
}
// Handle double-click on hook list items
if let Some(visual_index) =
app.get_hooks_double_click_info(mouse.column, mouse.row, terminal_width)
&& let Some(hook) = tab2_hooks::get_hook_at_visual_index(&app, visual_index)
{
app.toggle_hook_checkbox(hook);
}
// Reset click tracking after double-click
app.last_click_time = None;
app.last_click_pos = None;
} else {
// Single click - update tracking
app.on_mouse_click(mouse.column, mouse.row, terminal_width);
app.last_click_time = Some(now);
app.last_click_pos = Some(current_pos);
}
}
MouseEventKind::ScrollUp => {
if !app.show_popup && !app.show_help_popup {
app.on_up();
}
}
MouseEventKind::ScrollDown => {
if !app.show_popup && !app.show_help_popup {
app.on_down();
}
}
_ => {}
}
}
_ => {}
}
if app.should_quit {
return Ok(Vec::new());
}
}
}