devpane 0.3.0

A terminal-first multi-process workspace and development dashboard
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use std::io;
use std::time::{Duration, Instant};

use crossterm::{
    event::{self, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{
    Terminal,
    backend::CrosstermBackend,
    layout::{Constraint, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Tabs},
};

use devpane::process::session::{ProcessSession, ProcessStatus};
use devpane::service_log_view::ServiceLogView;
use devpane::storage;
use devpane::workspace::TerminalPane;

pub fn run<F>(mut fetch_sessions: F) -> Result<(), Box<dyn std::error::Error>>
where
    F: FnMut() -> Result<Vec<ProcessSession>, String>,
{
    enable_raw_mode()?;

    let mut stdout = io::stdout();
    if let Err(error) = execute!(stdout, EnterAlternateScreen, event::EnableMouseCapture) {
        let _ = disable_raw_mode();
        return Err(error.into());
    }

    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let result = run_app(&mut terminal, &mut fetch_sessions);

    disable_raw_mode()?;
    let _ = execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        event::DisableMouseCapture
    );
    terminal.show_cursor()?;

    result
}

fn run_app<F>(
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
    fetch_sessions: &mut F,
) -> Result<(), Box<dyn std::error::Error>>
where
    F: FnMut() -> Result<Vec<ProcessSession>, String>,
{
    let mut service_views: Vec<ServiceLogView> = Vec::new();
    let mut shell_tabs: Vec<TerminalPane> = Vec::new();
    let mut closed_session_ids = std::collections::HashSet::<String>::new();
    let mut active_tab: usize = 0;
    let mut is_searching = false;
    let mut search_input = String::new();
    let mut error_message: Option<String> = None;
    let mut last_refresh = Instant::now() - Duration::from_secs(1);

    // Initial load of services
    match fetch_sessions() {
        Ok(sessions) => {
            for session in sessions {
                match storage::get_log_file(&session.id) {
                    Ok(log_path) => {
                        match ServiceLogView::new(session.clone(), log_path) {
                            Ok(view) => service_views.push(view),
                            Err(e) => {
                                eprintln!("Failed to create log view for {}: {}", session.name, e);
                            }
                        }
                    }
                    Err(e) => {
                        eprintln!("Failed to get log path for {}: {}", session.name, e);
                    }
                }
            }

            if service_views.is_empty() {
                error_message = Some(
                    "No services running.\n\nTry:\n  1. Check devbox.yaml has enabled services\n  2. Run 'devbox list' to see all sessions\n  3. Press Ctrl+T to create a shell".to_string()
                );
            }
        }
        Err(e) => {
            error_message = Some(format!(
                "Failed to connect to daemon: {}\n\nTry:\n  1. Check if daemon is running: devbox list\n  2. Restart daemon: devbox shutdown && devbox up",
                e
            ));
        }
    }

    loop {
        // Refresh session list and update logs periodically
        if last_refresh.elapsed() >= Duration::from_millis(500) {
            match fetch_sessions() {
                Ok(sessions) => {
                    // Update existing service views
                    for view in &mut service_views {
                        if let Err(e) = view.update() {
                            eprintln!("Failed to update log for {}: {}", view.name(), e);
                        }

                        // Update session status
                        if let Some(updated_session) = sessions.iter().find(|s| s.id == view.session.id) {
                            view.session = updated_session.clone();
                        }
                    }

                    // Remove service views whose session no longer exists in the daemon
                    let prev_services = service_views.len();
                    service_views.retain(|v| sessions.iter().any(|s| s.id == v.session.id));
                    if service_views.len() < prev_services {
                        let total = service_views.len() + shell_tabs.len();
                        if total > 0 && active_tab >= total {
                            active_tab = total - 1;
                        }
                    }

                    // Add new services that appeared (and were not explicitly closed by user)
                    for session in sessions {
                        if !closed_session_ids.contains(&session.id)
                            && !service_views.iter().any(|v| v.session.id == session.id)
                        {
                            match storage::get_log_file(&session.id) {
                                Ok(log_path) => {
                                    match ServiceLogView::new(session.clone(), log_path) {
                                        Ok(view) => service_views.push(view),
                                        Err(e) => {
                                            eprintln!("Failed to create log view for {}: {}", session.name, e);
                                        }
                                    }
                                }
                                Err(e) => {
                                    eprintln!("Failed to get log path for {}: {}", session.name, e);
                                }
                            }
                        }
                    }

                    error_message = None;
                }
                Err(error) => {
                    error_message = Some(error);
                }
            }

            last_refresh = Instant::now();
        }

        // Update shell tabs and auto-remove any whose process exited
        let prev_shells = shell_tabs.len();
        shell_tabs.retain_mut(|shell| {
            shell.update();
            match shell.session.try_wait() {
                Ok(Some(_)) => false, // Child process exited (e.g. exit command)
                _ => true,
            }
        });
        if shell_tabs.len() < prev_shells {
            let total = service_views.len() + shell_tabs.len();
            if total > 0 && active_tab >= total {
                active_tab = total - 1;
            }
        }

        // Render UI
        terminal.draw(|frame| {
            let areas = Layout::vertical([
                Constraint::Length(3), // Tab bar
                Constraint::Min(0),     // Content
                Constraint::Length(2),  // Footer
            ])
            .split(frame.area());

            // Build tab titles with colored status indicators
            let mut tab_titles: Vec<Line> = service_views
                .iter()
                .map(|view| {
                    let (status_icon, status_color) = match view.status() {
                        ProcessStatus::Running => ("●", Color::Green),
                        ProcessStatus::Exited => ("○", Color::Red),
                    };
                    Line::from(vec![
                        Span::styled(status_icon, Style::default().fg(status_color)),
                        Span::raw(" "),
                        Span::raw(view.name()),
                    ])
                })
                .collect();

            // Add shell tabs with different icon
            for shell in shell_tabs.iter() {
                tab_titles.push(Line::from(vec![
                    Span::styled("🐚", Style::default().fg(Color::Cyan)),
                    Span::raw(" "),
                    Span::raw(shell.name.as_str()),
                ]));
            }

            let tabs = Tabs::new(tab_titles)
                .block(Block::default().title(" Devbox Workspace ").borders(Borders::ALL))
                .select(active_tab)
                .style(Style::default().fg(Color::White))
                .highlight_style(
                    Style::default()
                        .fg(Color::Yellow)
                        .add_modifier(Modifier::BOLD)
                        .bg(Color::DarkGray),
                );

            frame.render_widget(tabs, areas[0]);

            // Render current tab's content
            let total_tabs = service_views.len() + shell_tabs.len();

            if total_tabs == 0 {
                // Show helpful empty state
                let help_text = vec![
                    Line::from(""),
                    Line::from(Span::styled(
                        "Welcome to Devbox!",
                        Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD),
                    )),
                    Line::from(""),
                    Line::from("No services are currently running."),
                    Line::from(""),
                    Line::from(vec![
                        Span::raw("To get started:"),
                    ]),
                    Line::from(vec![
                        Span::raw("  1. Press "),
                        Span::styled("Ctrl+T", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
                        Span::raw(" to create an interactive shell"),
                    ]),
                    Line::from(vec![
                        Span::raw("  2. Edit "),
                        Span::styled("devbox.yaml", Style::default().fg(Color::Green)),
                        Span::raw(" to define your services"),
                    ]),
                    Line::from(vec![
                        Span::raw("  3. Run "),
                        Span::styled("devbox up", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
                        Span::raw(" to start services"),
                    ]),
                    Line::from(""),
                    Line::from(vec![
                        Span::raw("Press "),
                        Span::styled("q", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
                        Span::raw(" to quit"),
                    ]),
                ];

                let help_pane = Paragraph::new(help_text)
                    .block(Block::default().title(" Getting Started ").borders(Borders::ALL))
                    .alignment(ratatui::layout::Alignment::Center);

                frame.render_widget(help_pane, areas[1]);
            } else if active_tab < service_views.len() {
                // Service log view
                let view = &service_views[active_tab];
                let content = view.filtered_content();
                let total_lines = content.lines().count();
                let viewport_height = areas[1].height.saturating_sub(2) as usize;
                let max_scroll = total_lines.saturating_sub(viewport_height);

                let scroll_y = if view.scroll_offset == 0 {
                    max_scroll
                } else {
                    max_scroll.saturating_sub(view.scroll_offset)
                };

                let (status_text, status_color) = match view.status() {
                    ProcessStatus::Running => ("Running", Color::Green),
                    ProcessStatus::Exited => {
                        if let Some(code) = view.session.exit_code {
                            if code == 0 {
                                ("Exited (success)", Color::Yellow)
                            } else {
                                ("Exited (failed)", Color::Red)
                            }
                        } else {
                            ("Exited", Color::Red)
                        }
                    }
                };

                let mut title = match view.detected_url() {
                    Some(url) => format!(
                        " {} - {} (PID: {}) | 🌐 {} ",
                        view.name(),
                        status_text,
                        view.session.pid,
                        url
                    ),
                    None => format!(
                        " {} - {} (PID: {}) ",
                        view.name(),
                        status_text,
                        view.session.pid
                    ),
                };

                if let Some(count) = view.match_count() {
                    title.push_str(&format!(
                        "[🔍 \"{}\": {} matches] ",
                        view.search_query.as_deref().unwrap_or(""),
                        count
                    ));
                }

                if view.scroll_offset > 0 {
                    title.push_str(&format!("[↑ {} lines] ", view.scroll_offset));
                }

                let content_pane = Paragraph::new(content)
                    .scroll((scroll_y as u16, 0))
                    .block(
                        Block::default()
                            .title(Span::styled(title, Style::default().fg(status_color)))
                            .borders(Borders::ALL),
                    );

                frame.render_widget(content_pane, areas[1]);
            } else if active_tab < total_tabs {
                // Shell tab
                let shell_index = active_tab - service_views.len();
                if let Some(shell) = shell_tabs.get(shell_index) {
                    let terminal_text = shell.screen_contents();

                    let mut title = format!(" {} ", shell.name);
                    if shell.scroll_offset() > 0 {
                        title = format!(" {} [↑ {} lines] ", shell.name, shell.scroll_offset());
                    }

                    let terminal_pane = Paragraph::new(terminal_text).block(
                        Block::default()
                            .title(Span::styled(
                                title,
                                if shell.scroll_offset() > 0 {
                                    Style::default().fg(Color::Yellow)
                                } else {
                                    Style::default()
                                }
                            ))
                            .borders(Borders::ALL),
                    );

                    frame.render_widget(terminal_pane, areas[1]);
                }
            }

            // Render footer with help text and status summary
            let footer_lines = match &error_message {
                Some(error) => {
                    // Split multi-line errors for better display
                    error.lines().map(|line| {
                        if line.starts_with("Try:") || line.starts_with("  ") {
                            Line::from(Span::styled(line, Style::default().fg(Color::Yellow)))
                        } else {
                            Line::from(vec![
                                Span::styled("⚠ ", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
                                Span::styled(line, Style::default().fg(Color::Red)),
                            ])
                        }
                    }).collect()
                }
                None => {
                    if is_searching {
                        vec![Line::from(vec![
                            Span::styled("Search: /", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
                            Span::styled(&search_input, Style::default().fg(Color::White).add_modifier(Modifier::BOLD)),
                            Span::styled("█", Style::default().fg(Color::Yellow)),
                            Span::raw("  "),
                            Span::styled("(Enter: Confirm • Esc: Clear • Type to filter)", Style::default().fg(Color::DarkGray)),
                        ])]
                    } else {
                        let running_count = service_views.iter().filter(|v| matches!(v.status(), ProcessStatus::Running)).count();
                        let exited_count = service_views.iter().filter(|v| matches!(v.status(), ProcessStatus::Exited)).count();

                        // Show scroll shortcuts if viewing a shell tab
                        let is_shell_tab = active_tab >= service_views.len();

                        if is_shell_tab {
                            vec![Line::from(vec![
                                Span::raw("Tab: Switch • "),
                                Span::raw("Ctrl+T: New shell • "),
                                Span::raw("Ctrl+W: Close tab • "),
                                Span::styled("PgUp/PgDn: Scroll • ", Style::default().fg(Color::Cyan)),
                                Span::styled("End: Bottom • ", Style::default().fg(Color::Cyan)),
                                Span::raw("Ctrl+Q: Quit • "),
                                Span::styled(
                                    format!("{} running", running_count),
                                    Style::default().fg(Color::Green),
                                ),
                                Span::raw(" • "),
                                Span::styled(
                                    format!("{} exited", exited_count),
                                    Style::default().fg(Color::Red),
                                ),
                            ])]
                        } else {
                            let has_filter = service_views.get(active_tab).and_then(|v| v.search_query.as_ref()).is_some();
                            let mut shortcuts = vec![
                                Span::raw("Tab/Shift+Tab: Switch • "),
                                Span::styled("/: Search • ", Style::default().fg(Color::Cyan)),
                            ];
                            if has_filter {
                                shortcuts.push(Span::styled("Esc: Clear filter • ", Style::default().fg(Color::Yellow)));
                            }
                            shortcuts.extend(vec![
                                Span::raw("Ctrl+T: New shell • "),
                                Span::raw("Ctrl+W: Close tab • "),
                                Span::raw("Ctrl+R: Refresh • "),
                                Span::raw("q / Ctrl+C: Stop & Quit • "),
                                Span::styled(
                                    format!("{} running", running_count),
                                    Style::default().fg(Color::Green),
                                ),
                                Span::raw(" • "),
                                Span::styled(
                                    format!("{} exited", exited_count),
                                    Style::default().fg(Color::Red),
                                ),
                            ]);
                            vec![Line::from(shortcuts)]
                        }
                    }
                }
            };

            frame.render_widget(Paragraph::new(footer_lines), areas[2]);
        })?;

        // Handle input
        if event::poll(Duration::from_millis(100))? {
            match event::read()? {
                Event::Resize(cols, rows) => {
                    // Resize all shell tabs to match new terminal size
                    for shell in &mut shell_tabs {
                        if let Err(e) = shell.resize(rows.saturating_sub(6), cols) {
                            eprintln!("Failed to resize shell {}: {}", shell.name, e);
                        }
                    }
                }
                Event::Key(key) => {
                    if key.kind == event::KeyEventKind::Press {
                        // Handle search input mode
                        if is_searching {
                            match key.code {
                                KeyCode::Esc => {
                                    is_searching = false;
                                    search_input.clear();
                                    if active_tab < service_views.len() {
                                        service_views[active_tab].set_search(None);
                                    }
                                }
                                KeyCode::Enter => {
                                    is_searching = false;
                                }
                                KeyCode::Backspace => {
                                    search_input.pop();
                                    if active_tab < service_views.len() {
                                        let q = if search_input.is_empty() {
                                            None
                                        } else {
                                            Some(search_input.clone())
                                        };
                                        service_views[active_tab].set_search(q);
                                    }
                                }
                                KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                                    search_input.push(c);
                                    if active_tab < service_views.len() {
                                        service_views[active_tab].set_search(Some(search_input.clone()));
                                    }
                                }
                                _ => {}
                            }
                            continue;
                        }

                        let total_tabs = service_views.len() + shell_tabs.len();

                        match (key.code, key.modifiers) {
                        // Search in active service tab
                        (KeyCode::Char('/'), KeyModifiers::NONE) if active_tab < service_views.len() => {
                            is_searching = true;
                            search_input = service_views[active_tab]
                                .search_query
                                .clone()
                                .unwrap_or_default();
                            continue;
                        }

                        // Clear search filter with Esc if active
                        (KeyCode::Esc, KeyModifiers::NONE) if active_tab < service_views.len() => {
                            if service_views[active_tab].search_query.is_some() {
                                service_views[active_tab].set_search(None);
                                search_input.clear();
                                continue;
                            }
                        }
                        // Quit: Ctrl+C or Ctrl+Q always quits; 'q' quits on read-only service views
                        (KeyCode::Char('c'), KeyModifiers::CONTROL)
                        | (KeyCode::Char('\x03'), _) => break,
                        (KeyCode::Char('q'), KeyModifiers::CONTROL)
                        | (KeyCode::Char('\x11'), _) => break,
                        (KeyCode::Char('q'), KeyModifiers::NONE) if active_tab < service_views.len() => break,

                        // Tab navigation
                        (KeyCode::Tab, KeyModifiers::NONE) => {
                            if total_tabs > 0 {
                                active_tab = (active_tab + 1) % total_tabs;
                            }
                            continue;
                        }
                        (KeyCode::BackTab, _) => {
                            if total_tabs > 0 {
                                active_tab = if active_tab == 0 {
                                    total_tabs - 1
                                } else {
                                    active_tab - 1
                                };
                            }
                            continue;
                        }

                        // New shell tab (Ctrl+T)
                        (KeyCode::Char('t'), KeyModifiers::CONTROL)
                        | (KeyCode::Char('\x14'), _) => {
                            let (program, args) = devpane::terminal::default_shell();
                            let shell_number = shell_tabs.len() + 1;

                            // Get current terminal size
                            let size = terminal.size()?;
                            let rows = size.height.saturating_sub(6); // Account for tab bar and footer
                            let cols = size.width;

                            match TerminalPane::new(
                                format!("Shell {}", shell_number),
                                program,
                                &args,
                                std::env::current_dir()?,
                                rows,
                                cols,
                            ) {
                                Ok(pane) => {
                                    shell_tabs.push(pane);
                                    active_tab = service_views.len() + shell_tabs.len() - 1;
                                }
                                Err(e) => {
                                    error_message = Some(format!("Failed to create shell: {}", e));
                                }
                            }
                            continue;
                        }

                        // Close tab (Ctrl+W) - closes service tab (stops service) or closes shell tab
                        (KeyCode::Char('w'), KeyModifiers::CONTROL)
                        | (KeyCode::Char('W'), KeyModifiers::CONTROL)
                        | (KeyCode::Char('\x17'), _) => {
                            let total_tabs = service_views.len() + shell_tabs.len();
                            if total_tabs > 0 {
                                if active_tab < service_views.len() {
                                    let service_view = service_views.remove(active_tab);
                                    let session_id = service_view.session.id.clone();
                                    closed_session_ids.insert(session_id.clone());
                                    if let Ok(mut stop_stream) = devpane::ipc::connect() {
                                        let _ = devpane::protocol::write_message(
                                            &mut stop_stream,
                                            &devpane::protocol::Request::Stop {
                                                id: session_id,
                                            },
                                        );
                                        let _ = devpane::protocol::read_message::<_, devpane::protocol::Response>(
                                            &mut stop_stream,
                                        );
                                    }
                                } else {
                                    let shell_index = active_tab - service_views.len();
                                    if shell_index < shell_tabs.len() {
                                        shell_tabs.remove(shell_index);
                                    }
                                }

                                let new_total = service_views.len() + shell_tabs.len();
                                if new_total == 0 {
                                    break;
                                } else if active_tab >= new_total {
                                    active_tab = new_total - 1;
                                }
                            }
                            continue;
                        }

                        // Force refresh (Ctrl+R)
                        (KeyCode::Char('r'), KeyModifiers::CONTROL)
                        | (KeyCode::Char('\x12'), _) => {
                            last_refresh = Instant::now() - Duration::from_secs(1);
                            continue;
                        }

                        // Scroll up (Page Up, Shift+Up, or Up arrow on service view)
                        (KeyCode::PageUp, _) | (KeyCode::Up, KeyModifiers::SHIFT) => {
                            if active_tab < service_views.len() {
                                service_views[active_tab].scroll_up(10);
                            } else {
                                let shell_index = active_tab - service_views.len();
                                if let Some(shell) = shell_tabs.get_mut(shell_index) {
                                    shell.scroll_up(10);
                                }
                            }
                            continue;
                        }
                        (KeyCode::Up, KeyModifiers::NONE) if active_tab < service_views.len() => {
                            service_views[active_tab].scroll_up(1);
                            continue;
                        }

                        // Scroll down (Page Down, Shift+Down, or Down arrow on service view)
                        (KeyCode::PageDown, _) | (KeyCode::Down, KeyModifiers::SHIFT) => {
                            if active_tab < service_views.len() {
                                service_views[active_tab].scroll_down(10);
                            } else {
                                let shell_index = active_tab - service_views.len();
                                if let Some(shell) = shell_tabs.get_mut(shell_index) {
                                    shell.scroll_down(10);
                                }
                            }
                            continue;
                        }
                        (KeyCode::Down, KeyModifiers::NONE) if active_tab < service_views.len() => {
                            service_views[active_tab].scroll_down(1);
                            continue;
                        }

                        // Scroll to bottom (End)
                        (KeyCode::End, _) => {
                            if active_tab < service_views.len() {
                                service_views[active_tab].scroll_to_bottom();
                            } else {
                                let shell_index = active_tab - service_views.len();
                                if let Some(shell) = shell_tabs.get_mut(shell_index) {
                                    shell.scroll_to_bottom();
                                }
                            }
                            continue;
                        }

                        _ => {}
                    }

                    // Send input to shell tabs only
                    if active_tab >= service_views.len() {
                        let shell_index = active_tab - service_views.len();
                        if let Some(shell) = shell_tabs.get_mut(shell_index) {
                            let input: Option<Vec<u8>> = match key.code {
                                KeyCode::Char(character) => {
                                    if key.modifiers.contains(KeyModifiers::CONTROL) {
                                        None // Already handled above
                                    } else {
                                        Some(character.to_string().into_bytes())
                                    }
                                }
                                KeyCode::Enter => Some(vec![b'\r']),
                                KeyCode::Backspace => Some(vec![0x7f]),
                                KeyCode::Esc => Some(vec![0x1b]),
                                KeyCode::Up => Some(vec![0x1b, b'[', b'A']),
                                KeyCode::Down => Some(vec![0x1b, b'[', b'B']),
                                KeyCode::Right => Some(vec![0x1b, b'[', b'C']),
                                KeyCode::Left => Some(vec![0x1b, b'[', b'D']),
                                _ => None,
                            };

                            if let Some(input) = input {
                                let _ = shell.send_input(&input);
                            }
                        }
                    }
                }
                }
                Event::Mouse(mouse_event) => {
                    match mouse_event.kind {
                        event::MouseEventKind::ScrollUp => {
                            if active_tab < service_views.len() {
                                service_views[active_tab].scroll_up(3);
                            } else {
                                let shell_index = active_tab - service_views.len();
                                if let Some(shell) = shell_tabs.get_mut(shell_index) {
                                    shell.scroll_up(3);
                                }
                            }
                        }
                        event::MouseEventKind::ScrollDown => {
                            if active_tab < service_views.len() {
                                service_views[active_tab].scroll_down(3);
                            } else {
                                let shell_index = active_tab - service_views.len();
                                if let Some(shell) = shell_tabs.get_mut(shell_index) {
                                    shell.scroll_down(3);
                                }
                            }
                        }
                        _ => {}
                    }
                }
                _ => {}
            }
        }
    }

    Ok(())
}