omnyssh 1.0.3

TUI SSH dashboard & server manager
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
//! Application root: terminal setup, the shared/UI state split, and the main
//! event loop.
//!
//! Feature-specific state types and the matching `App` methods live in the
//! submodules declared below; this file keeps the core types (`App`,
//! `AppState`, `ViewState`, `Screen`, `SortOrder`) and the run / main-loop
//! plumbing. Every public item of the submodules is re-exported here so the
//! rest of the crate keeps using the flat `crate::app::Type` paths.

use std::collections::HashMap;
use std::io::Stdout;
use std::sync::Arc;
use std::time::Duration;

use ratatui::{backend::CrosstermBackend, Terminal};
use tokio::sync::{mpsc, RwLock};

use crate::config;
use crate::config::app_config::{AppConfig, ParsedKeybindings};
use crate::config::snippets::Snippet;
use crate::event::{spawn_event_thread, AppEvent, Metrics, ServiceKind, TransferId};
use crate::ssh::client::{ConnectionStatus, Host};
use crate::ssh::pool::PollManager;
use crate::ssh::pty::PtyManager;
use crate::ssh::sftp::{SftpCommand, SftpManager};
use crate::ui;
use crate::ui::theme::Theme;

mod action;
mod actions;
mod file_manager;
mod host;
mod input;
mod snippets;
mod terminal;
mod update;

pub use action::*;
pub use file_manager::*;
pub use host::*;
pub use snippets::*;
pub use terminal::*;
pub use update::*;

// ---------------------------------------------------------------------------
// Screen
// ---------------------------------------------------------------------------

/// The active top-level screen shown to the user.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Screen {
    #[default]
    Dashboard,
    /// Detail View — shows comprehensive server information.
    DetailView,
    FileManager,
    Snippets,
    /// PTY-backed multi-session terminal.
    Terminal,
}

impl Screen {
    /// Human-readable title used in the status bar.
    pub fn title(&self) -> &'static str {
        match self {
            Screen::Dashboard => "Dashboard",
            Screen::DetailView => "Server Details",
            Screen::FileManager => "File Manager",
            Screen::Snippets => "Snippets",
            Screen::Terminal => "Terminal",
        }
    }
}

// ---------------------------------------------------------------------------
// Sort order
// ---------------------------------------------------------------------------

/// How dashboard cards are sorted.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum SortOrder {
    /// Alphabetical by host name.
    #[default]
    Name,
    /// Descending CPU usage (highest first).
    Cpu,
    /// Descending RAM usage (highest first).
    Ram,
    /// By connection status: Connected → Connecting → Unknown → Failed.
    Status,
}

impl SortOrder {
    /// Cycle to the next sort order.
    pub fn next(&self) -> Self {
        match self {
            SortOrder::Name => SortOrder::Cpu,
            SortOrder::Cpu => SortOrder::Ram,
            SortOrder::Ram => SortOrder::Status,
            SortOrder::Status => SortOrder::Name,
        }
    }

    /// Human-readable label for display in the dashboard header.
    pub fn label(&self) -> &'static str {
        match self {
            SortOrder::Name => "name",
            SortOrder::Cpu => "cpu",
            SortOrder::Ram => "ram",
            SortOrder::Status => "status",
        }
    }
}

// ---------------------------------------------------------------------------
// AppState — shared between UI and background tasks
// ---------------------------------------------------------------------------

/// Application data shared between the UI thread and background SSH tasks.
/// Wrapped in `Arc<RwLock<_>>` — multiple readers, rare writers.
#[derive(Debug, Default)]
pub struct AppState {
    /// Currently visible screen.
    pub screen: Screen,
    /// Full host list (manual + SSH-config imports).
    pub hosts: Vec<Host>,
    /// Per-host runtime connection status (keyed by `host.name`).
    pub connection_statuses: HashMap<String, ConnectionStatus>,
    /// Live metrics per host, keyed by `host.name`.
    pub metrics: HashMap<String, Metrics>,
    /// Saved command snippets.
    pub snippets: Vec<Snippet>,
    /// Detected services per host.
    pub services: HashMap<String, Vec<crate::event::DetectedService>>,
    /// Active alerts per host.
    pub alerts: HashMap<String, Vec<crate::event::Alert>>,
    /// Discovery status per host.
    pub discovery_status: HashMap<String, crate::event::DiscoveryStatus>,
}

// ---------------------------------------------------------------------------
// ViewState — UI-only, not shared
// ---------------------------------------------------------------------------

/// UI-specific state that lives only on the main thread.
pub struct ViewState {
    /// Whether the help popup is currently shown.
    pub show_help: bool,
    /// Scroll offset for help popup (0 = top).
    pub help_scroll: usize,
    /// Transient status message shown in the status bar (overrides hints).
    pub status_message: Option<String>,
    /// State for the host-list / Dashboard screen.
    pub host_list: HostListView,
    /// State for the Snippets screen.
    pub snippets_view: SnippetsView,
    /// State for the File Manager screen.
    pub file_manager: FileManagerView,
    /// State for the Terminal multi-session screen.
    pub terminal_view: TerminalView,
    /// Active colour theme — loaded from config on startup.
    pub theme: Theme,
    /// Parsed keybindings — loaded from config on startup.
    pub keybindings: ParsedKeybindings,
    /// Monotonically-incrementing tick counter for animations (e.g. spinner).
    pub tick_count: u64,
    /// Quick View popup state for Detail View service quick views.
    /// Contains the service kind if a Quick View is currently open.
    pub quick_view: Option<ServiceKind>,
    /// Scroll offset for Quick View popup content.
    pub quick_view_scroll: usize,
    /// Startup update-notification popup, shown when a newer release exists.
    pub update_popup: Option<UpdatePopup>,
}

impl ViewState {
    /// Constructs a `ViewState` where `theme` and `keybindings` are filled
    /// with defaults so that struct-update syntax can supply them later.
    fn default_inner() -> Self {
        Self {
            show_help: false,
            help_scroll: 0,
            status_message: None,
            host_list: HostListView::default(),
            snippets_view: SnippetsView::default(),
            file_manager: FileManagerView::default(),
            terminal_view: TerminalView::default(),
            theme: Theme::default(),
            keybindings: ParsedKeybindings::default(),
            tick_count: 0,
            quick_view: None,
            quick_view_scroll: 0,
            update_popup: None,
        }
    }
}

impl Default for ViewState {
    fn default() -> Self {
        Self::default_inner()
    }
}

// ---------------------------------------------------------------------------
// App
// ---------------------------------------------------------------------------

/// Root application struct — owns the terminal, state, and event channel.
pub struct App {
    /// Shared state readable by background tokio tasks.
    pub state: Arc<RwLock<AppState>>,
    /// UI-only state, main thread only.
    pub view: ViewState,
    /// Sender half — given to the event thread and background tasks.
    event_tx: mpsc::Sender<AppEvent>,
    /// Receiver half — consumed by the main loop.
    event_rx: mpsc::Receiver<AppEvent>,
    /// Persistent SFTP session manager for the File Manager.
    sftp_manager: Option<SftpManager>,
    /// Monotone counter for assigning unique [`TransferId`] values.
    next_transfer_id: TransferId,
    /// Background metrics polling manager. Stored in `App` (not in
    /// `AppState`) to avoid a reference cycle through `Arc`. Dropping it
    /// signals all per-host tasks to stop.
    poll_manager: Option<PollManager>,
    /// PTY session manager for the Terminal multi-session screen.
    pty_manager: Option<PtyManager>,
    /// One heavyweight event (Key, etc.) that was pulled from the channel
    /// during a lightweight-event drain but could not be handled inline.
    /// Consumed at the top of the next main-loop iteration before blocking
    /// on `event_rx.recv()`.
    pending_event: Option<AppEvent>,
    /// Application config — retained so update preferences can be persisted.
    config: AppConfig,
}

impl App {
    /// Creates a new `App` with the provided [`AppConfig`].
    ///
    /// The config is used to set the active theme and keybindings at startup.
    /// Call [`App::default`] to use a default config without loading a file.
    pub fn new(config: AppConfig) -> Self {
        let (tx, rx) = mpsc::channel(256);
        let theme = Theme::from_name(&config.ui.theme);
        let keybindings = ParsedKeybindings::from_config(&config.keybindings);
        Self {
            state: Arc::new(RwLock::new(AppState::default())),
            view: ViewState {
                theme,
                keybindings,
                ..ViewState::default_inner()
            },
            event_tx: tx,
            event_rx: rx,
            sftp_manager: None,
            next_transfer_id: 0,
            poll_manager: None,
            pty_manager: None,
            pending_event: None,
            config,
        }
    }
}

impl Default for App {
    fn default() -> Self {
        Self::new(AppConfig::default())
    }
}

impl App {
    /// Runs the application: sets up the terminal, starts the event thread,
    /// spawns the host-loading task, then enters the main render/event loop.
    pub async fn run(&mut self) -> anyhow::Result<()> {
        // Terminal setup.
        crossterm::terminal::enable_raw_mode()?;
        let mut stdout = std::io::stdout();
        crossterm::execute!(
            stdout,
            crossterm::terminal::EnterAlternateScreen,
            crate::utils::mouse::EnableMinimalMouseCapture,
            crossterm::event::EnableBracketedPaste,
        )?;
        let backend = CrosstermBackend::new(stdout);
        let mut terminal = Terminal::new(backend)?;

        // Background event thread (keyboard + tick).
        spawn_event_thread(self.event_tx.clone())?;

        // Load hosts in a background task.
        {
            let tx = self.event_tx.clone();
            tokio::spawn(async move {
                match config::load_all_hosts() {
                    Ok(hosts) => {
                        let _ = tx.send(AppEvent::HostsLoaded(hosts)).await;
                    }
                    Err(e) => tracing::warn!("Failed to load hosts: {}", e),
                }
            });
        }

        // Load snippets in a background task.
        {
            let tx = self.event_tx.clone();
            tokio::spawn(async move {
                match config::snippets::load_snippets() {
                    Ok(snippets) => {
                        let _ = tx.send(AppEvent::SnippetsLoaded(snippets)).await;
                    }
                    Err(e) => tracing::warn!("Failed to load snippets: {}", e),
                }
            });
        }

        // Check GitHub for a newer release in a background task. A failed or
        // slow check never delays startup; a skipped version is dropped here.
        if self.config.update.check_on_startup {
            let tx = self.event_tx.clone();
            let skip_version = self.config.update.skip_version.clone();
            tokio::spawn(async move {
                if let Some(info) = crate::update::check().await {
                    if info.latest != skip_version {
                        let _ = tx.send(AppEvent::UpdateAvailable(info)).await;
                    }
                }
            });
        }

        // Main loop.
        let result = self.main_loop(&mut terminal).await;

        // Gracefully shut down all metric polling tasks.
        if let Some(mgr) = self.poll_manager.take() {
            mgr.shutdown();
        }
        // Gracefully shut down the SFTP session.
        if let Some(sftp) = self.sftp_manager.take() {
            sftp.disconnect();
        }
        // Gracefully shut down all PTY sessions.
        if let Some(mgr) = self.pty_manager.take() {
            mgr.shutdown();
        }

        // Terminal restore — always runs even if main_loop returned Err.
        crossterm::terminal::disable_raw_mode()?;
        crossterm::execute!(
            terminal.backend_mut(),
            crossterm::terminal::LeaveAlternateScreen,
            crate::utils::mouse::DisableMinimalMouseCapture,
            crossterm::event::DisableBracketedPaste,
        )?;
        terminal.show_cursor()?;

        result
    }

    /// Inner loop — separated from `run` so terminal restore always happens.
    async fn main_loop(
        &mut self,
        terminal: &mut Terminal<CrosstermBackend<Stdout>>,
    ) -> anyhow::Result<()> {
        loop {
            // ----------------------------------------------------------------
            // Handle a pending SSH connection *before* the next render so the
            // terminal is fully restored while SSH runs.
            // ----------------------------------------------------------------
            if let Some(host) = self.view.host_list.pending_connect.take() {
                self.connect_system_ssh(terminal, &host).await?;
                continue;
            }

            // ----------------------------------------------------------------
            // Render.
            // ----------------------------------------------------------------
            {
                let state = self.state.read().await;
                terminal.draw(|frame| ui::render(frame, &state, &self.view))?;
            }

            // ----------------------------------------------------------------
            // Sync scroll_offset to the actual scrollback depth that vt100
            // applied.  vt100's set_scrollback() clamps the requested value to
            // the number of lines actually stored in the scrollback buffer
            // (which grows up to 1000 as output arrives).  Without this sync
            // the user's scroll_offset can be 1000 while only 80 lines of
            // history exist, forcing them to scroll down ~330 times to return
            // to the live view.
            // ----------------------------------------------------------------
            for tab in &mut self.view.terminal_view.tabs {
                if tab.scroll_offset > 0 {
                    // Non-blocking: skip sync if the reader thread holds the
                    // lock; we'll pick up the correct value on the next frame.
                    if let Ok(p) = tab.parser.try_lock() {
                        // The render already called set_scrollback(tab.scroll_offset);
                        // read back what vt100 actually clamped it to.
                        let actual = p.screen().scrollback();
                        tab.scroll_offset = actual;
                    }
                }
            }

            // ----------------------------------------------------------------
            // Wait for the next event.
            // Check the pending-event slot first (populated by the drain
            // loop below when a heavyweight event is found mid-drain).
            // ----------------------------------------------------------------
            let event = if let Some(e) = self.pending_event.take() {
                e
            } else {
                match self.event_rx.recv().await {
                    Some(e) => e,
                    None => break,
                }
            };

            // ----------------------------------------------------------------
            // Handle event.
            // ----------------------------------------------------------------
            match event {
                AppEvent::Key(key) => {
                    let action = self.handle_key(key).await?;
                    if matches!(action, Some(AppAction::Quit)) {
                        break;
                    }
                    self.process_action(action).await?;
                }

                AppEvent::Paste(text) => {
                    // On the terminal screen a paste is forwarded to the PTY as
                    // one block; elsewhere it is replayed as key events so other
                    // input widgets keep handling it as before.
                    let on_terminal = {
                        let state = self.state.read().await;
                        state.screen == crate::app::Screen::Terminal
                    };
                    if on_terminal {
                        self.handle_term_paste(&text);
                    } else {
                        for key in crate::utils::paste::paste_to_keys(&text) {
                            let action = self.handle_key(key).await?;
                            if matches!(action, Some(AppAction::Quit)) {
                                break;
                            }
                            self.process_action(action).await?;
                        }
                    }
                }

                AppEvent::HostsLoaded(hosts) => {
                    let n = hosts.len();
                    {
                        let mut state = self.state.write().await;
                        state.hosts = hosts;
                    }
                    // Rebuild filter with the new host list.
                    {
                        let state = self.state.read().await;
                        self.view.host_list.rebuild_filter(
                            &state.hosts,
                            &state.metrics,
                            &state.connection_statuses,
                        );
                        self.view.host_list.rebuild_tags(&state.hosts);

                        // Start (or restart) the metrics polling manager.
                        if let Some(old) = self.poll_manager.take() {
                            old.shutdown();
                        }
                        self.poll_manager = Some(PollManager::start(
                            state.hosts.clone(),
                            self.event_tx.clone(),
                            Duration::from_secs(30),
                        ));
                    }
                    tracing::info!("Loaded {} host(s)", n);
                }

                AppEvent::Tick => {
                    // Increment tick counter for spinner animation.
                    self.view.tick_count = self.view.tick_count.wrapping_add(1);
                }

                AppEvent::MetricsUpdate(host_name, new_metrics) => {
                    let mut state = self.state.write().await;
                    // Merge new metrics with existing ones to avoid overwriting fields
                    let merged = if let Some(existing) = state.metrics.get(&host_name) {
                        Metrics {
                            cpu_percent: new_metrics.cpu_percent.or(existing.cpu_percent),
                            ram_percent: new_metrics.ram_percent.or(existing.ram_percent),
                            disk_percent: new_metrics.disk_percent.or(existing.disk_percent),
                            uptime: new_metrics
                                .uptime
                                .clone()
                                .or_else(|| existing.uptime.clone()),
                            load_avg: new_metrics
                                .load_avg
                                .clone()
                                .or_else(|| existing.load_avg.clone()),
                            os_info: new_metrics
                                .os_info
                                .clone()
                                .or_else(|| existing.os_info.clone()),
                            top_processes: new_metrics
                                .top_processes
                                .clone()
                                .or_else(|| existing.top_processes.clone()),
                            last_updated: new_metrics.last_updated,
                        }
                    } else {
                        new_metrics
                    };
                    state.metrics.insert(host_name, merged);
                    // Clear the "Refreshing metrics…" banner once data arrives.
                    if matches!(
                        self.view.status_message.as_deref(),
                        Some("Refreshing metrics…")
                    ) {
                        self.view.status_message = None;
                    }
                }

                AppEvent::HostStatusChanged(host_name, status) => {
                    {
                        let mut state = self.state.write().await;
                        state
                            .connection_statuses
                            .insert(host_name.clone(), status.clone());
                    }

                    // Show notification for connection/metrics failures (extract essential message)
                    if let ConnectionStatus::Failed(ref error) = status {
                        let short_error =
                            error.split(':').next().unwrap_or(error).trim().to_string();

                        self.view.status_message = Some(format!(
                            "Connection failed for '{}': {}",
                            host_name, short_error
                        ));
                    }

                    // Re-sort if sorting by status.
                    if self.view.host_list.sort_order == SortOrder::Status {
                        let state = self.state.read().await;
                        self.view.host_list.rebuild_filter(
                            &state.hosts,
                            &state.metrics,
                            &state.connection_statuses,
                        );
                    }
                }

                // ----------------------------------------------------------------
                // Smart Server Context discovery events
                // ----------------------------------------------------------------
                AppEvent::DiscoveryQuickScanDone(host_name, services) => {
                    let mut state = self.state.write().await;
                    state.services.insert(host_name.clone(), services);
                    state
                        .discovery_status
                        .insert(host_name, crate::event::DiscoveryStatus::QuickScanDone);
                }

                AppEvent::DiscoveryDeepProbeDone(host_name, services) => {
                    let mut state = self.state.write().await;
                    state.services.insert(host_name.clone(), services);
                    state
                        .discovery_status
                        .insert(host_name, crate::event::DiscoveryStatus::DeepProbeDone);
                }

                AppEvent::DiscoveryFailed(host_name, error) => {
                    let mut state = self.state.write().await;
                    state.discovery_status.insert(
                        host_name.clone(),
                        crate::event::DiscoveryStatus::Failed(error.clone()),
                    );
                    tracing::debug!(host = %host_name, error = %error, "discovery failed");

                    // Show error notification in status bar (extract only the essential error message)
                    // Discovery errors often contain the full command after a colon, so extract just the first part
                    let short_error = error.split(':').next().unwrap_or(&error).trim().to_string();

                    self.view.status_message = Some(format!(
                        "Discovery failed for '{}': {}",
                        host_name, short_error
                    ));
                }

                AppEvent::AlertNew(host_name, alert) => {
                    let mut state = self.state.write().await;
                    state
                        .alerts
                        .entry(host_name)
                        .or_insert_with(Vec::new)
                        .push(alert);
                }

                // ----------------------------------------------------------------
                // Auto SSH Key Setup events
                // ----------------------------------------------------------------
                AppEvent::KeySetupProgress(host_name, step) => {
                    tracing::debug!(host = %host_name, step = ?step, "key setup progress");
                    // Update the progress popup's current step.
                    if let Some(HostPopup::KeySetupProgress {
                        current_step,
                        host_name: popup_host,
                        ..
                    }) = &mut self.view.host_list.popup
                    {
                        if *popup_host == host_name {
                            *current_step = Some(step);
                        }
                    }
                }

                AppEvent::KeySetupComplete(host_name, key_path) => {
                    tracing::info!(
                        host = %host_name,
                        key = %key_path.display(),
                        "key setup complete"
                    );

                    // Update host config: set identity_file, key_setup_date,
                    // password_auth_disabled.
                    {
                        let mut state = self.state.write().await;
                        if let Some(host) = state.hosts.iter_mut().find(|h| h.name == host_name) {
                            host.identity_file = Some(key_path.to_string_lossy().to_string());
                            host.key_setup_date = Some(chrono::Utc::now().to_rfc3339());
                            host.password_auth_disabled = Some(true);
                            // Clear password since key auth is now configured.
                            host.password = None;
                        }
                        // Persist updated hosts to disk.
                        if let Err(e) = config::save_hosts(&state.hosts) {
                            tracing::warn!("Failed to save hosts after key setup: {}", e);
                        }
                    }

                    // Close popup and show success.
                    self.view.host_list.popup = None;
                    self.view.status_message = Some(format!(
                        "✓ SSH key setup complete for '{}'. Key: {}",
                        host_name,
                        key_path.display()
                    ));
                }

                AppEvent::KeySetupFailed(host_name, error) => {
                    tracing::error!(host = %host_name, error = %error, "key setup failed");
                    // Close popup and show error.
                    self.view.host_list.popup = None;
                    self.view.status_message =
                        Some(format!("✗ Key setup failed for '{}': {}", host_name, error));
                }

                AppEvent::KeySetupRollback(host_name, result) => {
                    tracing::warn!(host = %host_name, result = %result, "key setup rollback");
                    // Close popup and show rollback result.
                    self.view.host_list.popup = None;
                    self.view.status_message = Some(format!(
                        "⚠ Key setup rolled back for '{}': {}",
                        host_name, result
                    ));
                }

                // ----------------------------------------------------------------
                // Update checker events
                // ----------------------------------------------------------------
                AppEvent::UpdateAvailable(info) => {
                    // Show the popup only if one is not already visible.
                    if self.view.update_popup.is_none() {
                        self.view.update_popup = Some(UpdatePopup {
                            info,
                            phase: UpdatePopupPhase::Prompt { selected: 0 },
                        });
                    }
                }

                AppEvent::UpdateInstalled(result) => {
                    if let Some(popup) = &mut self.view.update_popup {
                        popup.phase = match result {
                            Ok(()) => UpdatePopupPhase::Done {
                                message: "Update installed. Restart omny to use \
                                          the new version."
                                    .to_string(),
                                ok: true,
                            },
                            Err(err) => UpdatePopupPhase::Done {
                                message: format!("Update failed: {}", err),
                                ok: false,
                            },
                        };
                    }
                }

                // ----------------------------------------------------------------
                // PTY terminal events
                // ----------------------------------------------------------------
                AppEvent::PtyOutput(session_id) => {
                    // Data already processed into the vt100 parser by the reader
                    // thread. Mark the tab as having unread activity if it is not
                    // the currently focused tab.
                    let active_id = self.view.terminal_view.active_session_id();
                    if active_id != Some(session_id) {
                        if let Some(tab) = self
                            .view
                            .terminal_view
                            .tabs
                            .iter_mut()
                            .find(|t| t.session_id == session_id)
                        {
                            tab.has_activity = true;
                        }
                    }
                }

                AppEvent::TermScroll(delta) => {
                    // Only act when the Terminal screen is focused. The wheel
                    // either scrolls the tab's local scrollback or is forwarded
                    // to the foreground application (see `handle_term_scroll`).
                    let on_terminal = {
                        let state = self.state.read().await;
                        state.screen == crate::app::Screen::Terminal
                    };
                    if on_terminal {
                        self.handle_term_scroll(delta);
                    }
                }

                AppEvent::PtyExited(session_id) => {
                    // Remove the session from the manager and the tab bar.
                    if let Some(mgr) = &mut self.pty_manager {
                        mgr.close(session_id);
                    }
                    let tv = &mut self.view.terminal_view;
                    // Remove the tab.
                    if let Some(pos) = tv.tabs.iter().position(|t| t.session_id == session_id) {
                        tv.tabs.remove(pos);
                        // Collapse any split that referenced this tab.
                        tv.split = None;
                        tv.split_focus = SplitFocus::Primary;
                        if tv.tabs.is_empty() {
                            self.state.write().await.screen = Screen::Dashboard;
                            self.view.status_message = Some("SSH session closed.".to_string());
                        } else {
                            tv.active_tab = tv.active_tab.min(tv.tabs.len().saturating_sub(1));
                        }
                    }
                }

                AppEvent::TerminalResized(cols, rows) => {
                    // Reserve rows: status bar (1) + tab bar (1) + pane border top+bottom (2) = 4.
                    // Reserve cols: pane border left+right (2) = 2.
                    let pty_rows = rows.saturating_sub(4);
                    let pty_cols = cols.saturating_sub(2);
                    if let Some(mgr) = &mut self.pty_manager {
                        for tab in &self.view.terminal_view.tabs {
                            let _ = mgr.resize(tab.session_id, pty_cols, pty_rows);
                        }
                    }
                    // Also update the vt100 parsers so the screen dimensions are
                    // consistent with the render area.
                    for tab in &self.view.terminal_view.tabs {
                        if let Ok(mut p) = tab.parser.lock() {
                            p.set_size(pty_rows, pty_cols);
                        }
                    }
                }

                AppEvent::Error(_, msg) => {
                    self.view.status_message = Some(msg);
                }

                // ----------------------------------------------------------------
                // File Manager events
                // ----------------------------------------------------------------
                AppEvent::FileTransferProgress(tid, done, total) => {
                    if let Some(FileManagerPopup::TransferProgress {
                        transfer_id,
                        done: d,
                        total: t,
                        ..
                    }) = &mut self.view.file_manager.popup
                    {
                        // Accept progress from any tid >= the popup's current tid
                        // so multi-file queues display sequential file progress.
                        if tid >= *transfer_id {
                            *transfer_id = tid;
                            *d = done;
                            *t = total;
                        }
                    }
                }

                AppEvent::SftpConnected { host_name } => {
                    self.view.file_manager.connected_host = Some(host_name);
                    self.view.file_manager.sftp_connecting = false;
                    // Close the host-picker popup now that we're connected.
                    if matches!(
                        self.view.file_manager.popup,
                        Some(FileManagerPopup::HostPicker { .. })
                    ) {
                        self.view.file_manager.popup = None;
                    }
                    // List the remote home directory.
                    if let Some(mgr) = &self.sftp_manager {
                        mgr.send(SftpCommand::ListDir("/".to_string()));
                    }
                }

                AppEvent::SftpManagerReady { host_name, manager } => {
                    self.sftp_manager = Some(*manager);
                    self.view.file_manager.connected_host = Some(host_name.clone());
                    self.view.file_manager.sftp_connecting = false;
                    // Close the host-picker popup now that we're connected.
                    if matches!(
                        self.view.file_manager.popup,
                        Some(FileManagerPopup::HostPicker { .. })
                    ) {
                        self.view.file_manager.popup = None;
                    }
                    // List the remote home directory.
                    if let Some(mgr) = &self.sftp_manager {
                        mgr.send(SftpCommand::ListDir("/".to_string()));
                    }
                    self.view.status_message = Some(format!("Connected to '{}'", host_name));
                }

                AppEvent::SftpDisconnected {
                    host_name: _,
                    reason,
                } => {
                    self.sftp_manager = None;
                    self.view.file_manager.connected_host = None;
                    self.view.file_manager.sftp_connecting = false;
                    self.view.file_manager.remote = FilePanelView::default();
                    self.view.file_manager.popup = None;
                    self.view.status_message = Some(format!("SFTP: {reason}"));
                }

                AppEvent::FileDirListed { path, entries } => {
                    let rp = &mut self.view.file_manager.remote;
                    rp.cwd = path;
                    rp.entries = entries;
                    rp.cursor = 0;
                    rp.scroll.set(0);
                    rp.marked.clear();
                    self.request_preview_for_active();
                }

                AppEvent::LocalDirListed { path, entries } => {
                    let lp = &mut self.view.file_manager.local;
                    lp.cwd = path;
                    lp.entries = entries;
                    lp.cursor = 0;
                    lp.scroll.set(0);
                    lp.marked.clear();
                    self.request_preview_for_active();
                }

                AppEvent::FilePreviewReady { path, content } => {
                    self.view.file_manager.preview_content = Some(content);
                    self.view.file_manager.preview_path = Some(path);
                }

                AppEvent::SftpOpDone { kind: _, result } => {
                    self.view.file_manager.pending_ops =
                        self.view.file_manager.pending_ops.saturating_sub(1);
                    let remaining = self.view.file_manager.pending_ops;

                    match result {
                        Ok(()) => {
                            if remaining == 0 {
                                // All queued operations finished — close popup and refresh.
                                self.view.file_manager.popup = None;
                                self.view.file_manager.active_transfer = None;
                                self.view.status_message = None;
                                self.refresh_active_panels().await;
                            } else {
                                self.view.status_message =
                                    Some(format!("{remaining} file(s) remaining…"));
                            }
                        }
                        Err(e) => {
                            // Abort remaining: clear popup, show error, refresh.
                            self.view.file_manager.popup = None;
                            self.view.file_manager.active_transfer = None;
                            self.view.file_manager.pending_ops = 0;
                            self.view.status_message = Some(format!("Transfer failed: {e}"));
                            self.refresh_active_panels().await;
                        }
                    }
                }

                AppEvent::SnippetsLoaded(snippets) => {
                    let n = snippets.len();
                    {
                        let mut state = self.state.write().await;
                        state.snippets = snippets;
                    }
                    let state = self.state.read().await;
                    let q = self.view.snippets_view.search_query.clone();
                    self.view.snippets_view.rebuild_filter(&state.snippets, &q);
                    tracing::info!("Loaded {} snippet(s)", n);
                }

                AppEvent::SnippetResult {
                    host_name,
                    snippet_name,
                    output,
                } => {
                    // Show error notification for failed snippet execution (extract essential message)
                    if let Err(ref error) = output {
                        let short_error =
                            error.split(':').next().unwrap_or(error).trim().to_string();

                        self.view.status_message = Some(format!(
                            "Snippet '{}' failed on '{}': {}",
                            snippet_name, host_name, short_error
                        ));
                    }

                    if let Some(SnippetPopup::Results { entries, .. }) =
                        &mut self.view.snippets_view.popup
                    {
                        for entry in entries.iter_mut() {
                            if entry.host_name == host_name
                                && entry.snippet_name == snippet_name
                                && entry.pending
                            {
                                entry.output = output;
                                entry.pending = false;
                                break;
                            }
                        }
                    }
                }
            }

            // ----------------------------------------------------------------
            // Lightweight-event drain — batch before next render.
            //
            // The main loop renders once per event.  During rapid mouse-wheel
            // scrolling the event queue can hold hundreds of TermScroll events,
            // causing one expensive render (+ parser-lock attempt) per tick and
            // freezing the UI for 1-2 s.  After handling the primary event we
            // consume all remaining lightweight events synchronously so they
            // collapse into a single render on the next iteration.
            //
            // Heavyweight events (Key, etc.) need `await` and must be handled
            // by the main loop; we store the first one in `pending_event` so it
            // is picked up at the top of the next iteration without blocking on
            // `recv()`.
            // ----------------------------------------------------------------
            {
                // Cache whether Terminal screen is active once — avoids an
                // async read inside the tight drain loop.
                let on_terminal = {
                    let st = self.state.read().await;
                    st.screen == crate::app::Screen::Terminal
                };

                loop {
                    match self.event_rx.try_recv() {
                        Ok(AppEvent::Tick) => {
                            self.view.tick_count = self.view.tick_count.wrapping_add(1);
                        }
                        Ok(AppEvent::PtyOutput(sid)) => {
                            // Mark activity exactly like the primary handler.
                            let active_id = self.view.terminal_view.active_session_id();
                            if active_id != Some(sid) {
                                if let Some(tab) = self
                                    .view
                                    .terminal_view
                                    .tabs
                                    .iter_mut()
                                    .find(|t| t.session_id == sid)
                                {
                                    tab.has_activity = true;
                                }
                            }
                        }
                        Ok(AppEvent::TermScroll(delta)) => {
                            // Handle help popup scrolling first.
                            if self.view.show_help {
                                if delta > 0 {
                                    // Scroll down (wheel down)
                                    self.view.help_scroll =
                                        self.view.help_scroll.saturating_add(delta as usize);
                                } else {
                                    // Scroll up (wheel up)
                                    self.view.help_scroll =
                                        self.view.help_scroll.saturating_sub((-delta) as usize);
                                }
                            } else if on_terminal {
                                self.handle_term_scroll(delta);
                            }
                        }
                        Ok(heavyweight) => {
                            // Can't handle async events here; buffer for next iter.
                            self.pending_event = Some(heavyweight);
                            break;
                        }
                        Err(_) => break, // channel empty
                    }
                }
            }
        }
        Ok(())
    }
}