dprs 0.1.14

dprs (Docker PRocesS viewer ) is a terminal user interface for managing Docker containers and monitoring their logs.
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// The dprs (Docker Process Manager) binary provides a terminal user interface
// for managing Docker containers. It allows users to list running containers,
// view details, stop containers, copy IP addresses, and open web interfaces.
// This file contains the main application loop and UI rendering logic for dprs.

use crossterm::{
    event::Event,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    ExecutableCommand,
};
use ratatui::{
    backend::{Backend, CrosstermBackend},
    Terminal,
};
use std::{
    io::{self, stdout},
    time::{Duration, Instant},
};

use dprs::dprs::app::{actions, AppState};
use dprs::dprs::commands::{CommandExecutor, CommandResult};
use dprs::dprs::display;
use dprs::dprs::display::toast::ToastManager;
use dprs::dprs::modes::Mode;
use dprs::shared::config::Config;
use dprs::shared::input::input_watcher::InputWatcher;
use tachyonfx::EffectManager;

fn main() -> Result<(), io::Error> {
    // Setup terminal
    enable_raw_mode()?;
    stdout().execute(EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout());
    let mut terminal = Terminal::new(backend)?;

    // Load configuration and create app state
    let config = Config::load();
    let mut toast_manager = ToastManager::new();

    let result = run_app(&mut terminal, &mut toast_manager, config);

    // Restore terminal
    disable_raw_mode()?;
    stdout().execute(LeaveAlternateScreen)?;

    if let Err(err) = result {
        // Print errors that occur during the TUI loop itself.
        println!("Application error: {}", err);
    }

    Ok(())
}

fn run_app<B: Backend>(
    terminal: &mut Terminal<B>,
    toast_manager: &mut ToastManager,
    mut config: Config,
) -> Result<(), io::Error> {
    let mut last_refresh = Instant::now();
    let refresh_interval = if config.should_auto_refresh() {
        config.auto_refresh_interval()
    } else {
        Duration::from_millis(500) // Default refresh interval
    };
    let mut app_state = AppState::new();
    let mut command_executor = CommandExecutor::new();
    let input_watcher = InputWatcher::new();
    let mut effects: EffectManager<()> = EffectManager::default();
    let mut last_frame = Instant::now();

    // Initial load of containers
    if let Err(e) = app_state.refresh_containers() {
        // This error occurs before the TUI loop starts, so print to stderr.
        // The TUI will still attempt to start and periodic refreshes will occur.
        eprintln!(
            "Initial container load failed: {}. The list may be empty or stale.",
            e
        );
    }

    loop {
        // Calculate elapsed time for effects
        let elapsed = last_frame.elapsed();
        last_frame = Instant::now();

        // Update progress
        app_state.update_progress();

        // Draw UI
        terminal.draw(|f| {
            display::draw::<B>(
                f,
                &mut app_state,
                toast_manager,
                &mut config,
                &mut effects,
                elapsed,
            )
        })?;

        // Handle toast expiration
        toast_manager.check_expired();

        // Handle input events from watcher
        if let Ok(Event::Key(key)) = input_watcher.try_recv() {
            handle_key_event(
                key,
                &mut app_state,
                &mut command_executor,
                toast_manager,
                &mut config,
            );
        }

        // Small sleep to prevent busy waiting
        std::thread::sleep(Duration::from_millis(10));

        // Periodic refresh check (if poll timed out or no event handled that resets the timer)
        if last_refresh.elapsed() >= refresh_interval {
            if let Err(e) = app_state.refresh_containers() {
                toast_manager.show(&format!("Auto-refresh error: {}", e), 3000);
            }
            // No toast for successful auto-refresh to avoid being too noisy.
            last_refresh = Instant::now();
        }

        // Check for exit request
        if app_state.should_exit() {
            return Ok(());
        }
    }
}

fn handle_key_event(
    key: crossterm::event::KeyEvent,
    app_state: &mut AppState,
    command_executor: &mut CommandExecutor,
    toast_manager: &mut ToastManager,
    config: &mut Config,
) {
    match app_state.mode {
        Mode::Normal => handle_normal_mode(key, app_state, toast_manager, config),
        Mode::Visual => handle_visual_mode(key, app_state, toast_manager, config),
        Mode::Command => {
            handle_command_mode(key, app_state, command_executor, toast_manager, config)
        }
        Mode::Search => handle_search_mode(key, app_state, toast_manager),
    }
}

fn handle_normal_mode(
    key: crossterm::event::KeyEvent,
    app_state: &mut AppState,
    toast_manager: &mut ToastManager,
    config: &mut Config,
) {
    use crossterm::event::{KeyCode, KeyModifiers};

    match key.code {
        // Quit
        KeyCode::Char('q') => app_state.request_exit(),

        // Basic navigation
        KeyCode::Char('j') | KeyCode::Down => app_state.next(),
        KeyCode::Char('k') | KeyCode::Up => app_state.previous(),

        // Vim-style navigation
        KeyCode::Char('g') => {
            // Handle gg sequence - for simplicity, just go to first for now
            app_state.go_to_first();
        }
        KeyCode::Char('G') => app_state.go_to_last(),
        KeyCode::Char('w') => app_state.word_next(),
        KeyCode::Char('b') => app_state.word_previous(),
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app_state.half_page_up()
        }
        KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app_state.half_page_down()
        }

        // Mode switching
        KeyCode::Char('v') => app_state.enter_visual_mode(),
        KeyCode::Char(':') => app_state.enter_command_mode(),
        KeyCode::Char('/') => app_state.enter_search_mode(true),
        KeyCode::Char('?') => app_state.enter_search_mode(false),

        // Search navigation
        KeyCode::Char('n') => app_state.next_search_result(),
        KeyCode::Char('N') => app_state.previous_search_result(),

        // Container/Project actions (behavior depends on compose_view_mode)
        KeyCode::Char('s') => {
            if app_state.compose_view_mode {
                if let Some(selected) = app_state.list_state.selected() {
                    match actions::stop_compose_project(app_state, selected, &*config) {
                        Ok(_) => {
                            toast_manager.show("Stop command sent for project. Refreshing...", 2000)
                        }
                        Err(e) => {
                            toast_manager.show(&format!("Error stopping project: {}", e), 3000)
                        }
                    }
                }
            } else {
                match actions::stop_container(app_state) {
                    Ok(_) => toast_manager.show("Stop command sent. Refreshing list...", 2000),
                    Err(e) => toast_manager.show(&format!("Error stopping container: {}", e), 3000),
                }
            }
        }
        KeyCode::Char('c') => {
            if !app_state.compose_view_mode {
                match actions::copy_ip_address(app_state) {
                    Ok(ip) => {
                        toast_manager.show(&format!("IP address copied to clipboard: {}", ip), 2000)
                    }
                    Err(e) => toast_manager.show(&format!("Error copying IP: {}", e), 3000),
                }
            }
        }
        KeyCode::Char('o') => {
            if !app_state.compose_view_mode {
                match actions::open_browser(app_state) {
                    Ok(_) => toast_manager.show("Opening browser...", 2000),
                    Err(e) => toast_manager.show(&format!("Error opening browser: {}", e), 3000),
                }
            }
        }
        KeyCode::Char('r') => {
            if app_state.compose_view_mode {
                if let Some(selected) = app_state.list_state.selected() {
                    match actions::restart_compose_project(app_state, selected, &*config) {
                        Ok(_) => toast_manager
                            .show("Restart command sent for project. Refreshing...", 2000),
                        Err(e) => {
                            toast_manager.show(&format!("Error restarting project: {}", e), 3000)
                        }
                    }
                }
            } else {
                match actions::restart_container(app_state, &*config) {
                    Ok(_) => toast_manager.show("Restart command sent. Refreshing list...", 2000),
                    Err(e) => {
                        toast_manager.show(&format!("Error restarting container: {}", e), 3000)
                    }
                }
            }
        }
        KeyCode::Char('t') => {
            app_state.tabular_mode = !app_state.tabular_mode;
            let mode_text = if app_state.tabular_mode {
                "tabular"
            } else {
                "normal"
            };
            toast_manager.show(&format!("Switched to {} view", mode_text), 1500);
        }
        KeyCode::Char('p') => {
            app_state.compose_view_mode = !app_state.compose_view_mode;
            let mode_text = if app_state.compose_view_mode {
                "compose projects"
            } else {
                "containers"
            };
            // Reset selection when toggling view
            app_state.list_state.select(Some(0));
            app_state.table_state.select(Some(0));
            toast_manager.show(&format!("Switched to {} view", mode_text), 1500);
        }

        // Filter
        KeyCode::Char('f') => app_state.enter_filter_mode(),

        // Container filter toggles
        KeyCode::Char('+') => {
            app_state.toggle_recent();
            let filter_name = app_state.container_filter.display_name();
            toast_manager.show(&format!("Switched to {} containers", filter_name), 1500);
        }
        KeyCode::Char('!') => {
            app_state.toggle_all();
            let filter_name = app_state.container_filter.display_name();
            toast_manager.show(&format!("Switched to {} containers", filter_name), 1500);
        }

        KeyCode::Esc => {
            if !app_state.filter_text.is_empty() {
                app_state.clear_filter();
                toast_manager.show("Filter cleared", 1500);
            } else if !app_state.search_state.matches.is_empty() {
                app_state.search_state.clear();
                toast_manager.show("Search cleared", 1500);
            }
        }

        // Legacy support for old filter mode
        _ => {
            if app_state.filter_mode {
                handle_filter_input(key, app_state);
            }
        }
    }
}

fn handle_visual_mode(
    key: crossterm::event::KeyEvent,
    app_state: &mut AppState,
    toast_manager: &mut ToastManager,
    config: &mut Config,
) {
    use crossterm::event::KeyCode;

    match key.code {
        KeyCode::Char('j') | KeyCode::Down => {
            app_state.next();
            if let Some(ref mut selection) = app_state.visual_selection {
                if let Some(current) = app_state.list_state.selected() {
                    selection.extend_to(current);
                }
            }
        }
        KeyCode::Char('k') | KeyCode::Up => {
            app_state.previous();
            if let Some(ref mut selection) = app_state.visual_selection {
                if let Some(current) = app_state.list_state.selected() {
                    selection.extend_to(current);
                }
            }
        }
        KeyCode::Char('G') => {
            app_state.go_to_last();
        }
        KeyCode::Char('g') => {
            app_state.go_to_first();
        }
        KeyCode::Char('s') => {
            if app_state.compose_view_mode {
                match actions::stop_selected_compose_projects(app_state, &*config) {
                    Ok(_) => {
                        let count = app_state.get_selected_indices().len();
                        toast_manager.show(
                            &format!(
                                "Stopped {} project{}",
                                count,
                                if count == 1 { "" } else { "s" }
                            ),
                            2000,
                        );
                    }
                    Err(e) => toast_manager.show(&format!("Error stopping projects: {}", e), 3000),
                }
            } else {
                match actions::stop_selected_containers(app_state, &*config) {
                    Ok(_) => {
                        let count = app_state.get_selected_indices().len();
                        toast_manager.show(
                            &format!(
                                "Stopped {} container{}",
                                count,
                                if count == 1 { "" } else { "s" }
                            ),
                            2000,
                        );
                    }
                    Err(e) => {
                        toast_manager.show(&format!("Error stopping containers: {}", e), 3000)
                    }
                }
            }
            app_state.enter_normal_mode();
        }
        KeyCode::Char('r') => {
            if app_state.compose_view_mode {
                match actions::restart_selected_compose_projects(app_state, &*config) {
                    Ok(_) => {
                        let count = app_state.get_selected_indices().len();
                        toast_manager.show(
                            &format!(
                                "Restarted {} project{}",
                                count,
                                if count == 1 { "" } else { "s" }
                            ),
                            2000,
                        );
                    }
                    Err(e) => {
                        toast_manager.show(&format!("Error restarting projects: {}", e), 3000)
                    }
                }
            } else {
                match actions::restart_selected_containers(app_state, &*config) {
                    Ok(_) => {
                        let count = app_state.get_selected_indices().len();
                        toast_manager.show(
                            &format!(
                                "Restarted {} container{}",
                                count,
                                if count == 1 { "" } else { "s" }
                            ),
                            2000,
                        );
                    }
                    Err(e) => {
                        toast_manager.show(&format!("Error restarting containers: {}", e), 3000)
                    }
                }
            }
            app_state.enter_normal_mode();
        }
        KeyCode::Esc => app_state.enter_normal_mode(),
        _ => {}
    }
}

fn handle_command_mode(
    key: crossterm::event::KeyEvent,
    app_state: &mut AppState,
    command_executor: &mut CommandExecutor,
    toast_manager: &mut ToastManager,
    config: &mut Config,
) {
    use crossterm::event::KeyCode;

    match key.code {
        KeyCode::Enter => {
            let command = app_state.command_state.input.clone();
            match command_executor.execute(&command, app_state) {
                CommandResult::Success(msg) => {
                    toast_manager.show(&msg, 2000);
                    app_state.command_state.add_to_history(command);
                }
                CommandResult::Error(msg) => {
                    toast_manager.show(&format!("Error: {}", msg), 3000);
                }
                CommandResult::Navigation(line) => {
                    app_state.list_state.select(Some(line));
                    app_state.table_state.select(Some(line));
                    toast_manager.show(&format!("Jumped to line {}", line + 1), 1500);
                }
                CommandResult::Quit => {
                    app_state.request_exit();
                }
                CommandResult::ConfigReload(new_config) => {
                    *config = *new_config;
                    toast_manager.show("Configuration reloaded", 2000);
                    app_state.command_state.add_to_history(command);
                }
            }
            app_state.enter_normal_mode();
        }
        KeyCode::Esc => {
            app_state.enter_normal_mode();
        }
        _ => {
            app_state.command_state.handle_key(key);
        }
    }
}

fn handle_search_mode(
    key: crossterm::event::KeyEvent,
    app_state: &mut AppState,
    toast_manager: &mut ToastManager,
) {
    use crossterm::event::KeyCode;

    match key.code {
        KeyCode::Enter => {
            let query = app_state.search_state.query.clone();
            app_state.perform_search(&query);

            let matches_count = app_state.search_state.matches.len();
            if matches_count > 0 {
                app_state.next_search_result();
                toast_manager.show(&format!("Found {} matches", matches_count), 2000);
            } else {
                toast_manager.show("No matches found", 2000);
            }
            app_state.enter_normal_mode();
        }
        KeyCode::Esc => {
            app_state.enter_normal_mode();
        }
        KeyCode::Char(c) => {
            app_state.search_state.query.push(c);
            // Perform incremental search
            let query = app_state.search_state.query.clone();
            app_state.perform_search(&query);
        }
        KeyCode::Backspace => {
            app_state.search_state.query.pop();
            let query = app_state.search_state.query.clone();
            if !query.is_empty() {
                app_state.perform_search(&query);
            } else {
                app_state.search_state.clear();
            }
        }
        _ => {}
    }
}

fn handle_filter_input(key: crossterm::event::KeyEvent, app_state: &mut AppState) {
    use crossterm::event::KeyCode;

    match key.code {
        KeyCode::Enter => app_state.exit_filter_mode(),
        KeyCode::Esc => {
            app_state.exit_filter_mode();
            app_state.clear_filter();
        }
        KeyCode::Backspace => {
            let mut text = app_state.filter_text.clone();
            text.pop();
            app_state.update_filter(text);
        }
        KeyCode::Char(c) => {
            let mut text = app_state.filter_text.clone();
            text.push(c);
            app_state.update_filter(text);
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn basic_startup_test() {
        let result = std::panic::catch_unwind(|| {
            let _app_state = dprs::dprs::app::AppState::new();
            let _toast_manager = dprs::dprs::display::toast::ToastManager::new();
        });
        assert!(result.is_ok());
    }
}

// Copyright (c) 2025 Durable Programming, LLC. All rights reserved.