essh 0.3.2

Enhanced SSH client with concurrent sessions, real-time host diagnostics, and a Netwatch-inspired TUI
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
use ratatui::{
    prelude::*,
    widgets::{Block, Clear, Paragraph},
};

use crate::design as d;
use crate::theme::Theme;
use crate::tui::meta_key_hint;

use super::{AppView, DashboardTab, HostDisplay, HostStatus};
use crate::session::Session;

// ---------------------------------------------------------------------------
// Palette action — what happens when you select an entry
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
pub enum PaletteAction {
    ConnectHost(usize),   // index into app.hosts
    SwitchSession(usize), // index into session_manager.sessions
    SetView(AppView),
    SetDashboardTab(DashboardTab),
    ToggleSplitPane,
    ToggleHelp,
}

// ---------------------------------------------------------------------------
// Palette entry — one row in the list
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
pub struct PaletteEntry {
    pub icon: &'static str,
    pub label: String,
    pub detail: String,
    pub action: PaletteAction,
    pub score: i32, // higher = better match
}

// ---------------------------------------------------------------------------
// Command palette state
// ---------------------------------------------------------------------------

pub struct CommandPalette {
    pub query: String,
    pub entries: Vec<PaletteEntry>,
    pub selected: usize,
}

impl CommandPalette {
    pub fn new() -> Self {
        Self {
            query: String::new(),
            entries: Vec::new(),
            selected: 0,
        }
    }

    /// Rebuild the entry list based on current query, hosts, and sessions.
    pub fn update(&mut self, hosts: &[HostDisplay], sessions: &[Session], has_sessions: bool) {
        let split_hint = meta_key_hint("s");
        let monitor_hint = meta_key_hint("m");
        let portfwd_hint = meta_key_hint("p");
        let files_hint = meta_key_hint("f");
        let mut entries = Vec::new();

        // Hosts — "connect <name>"
        for (i, host) in hosts.iter().enumerate() {
            let status = match host.status {
                HostStatus::Online => "",
                HostStatus::Offline => "",
                HostStatus::NeverProbed => "?",
            };
            entries.push(PaletteEntry {
                icon: "›_",
                label: format!(
                    "Connect: {}",
                    if host.name.is_empty() {
                        &host.hostname
                    } else {
                        &host.name
                    }
                ),
                detail: format!(
                    "{} {}@{}:{} {}",
                    status, host.user, host.hostname, host.port, host.tags
                ),
                action: PaletteAction::ConnectHost(i),
                score: 0,
            });
        }

        // Active sessions — "switch to <label>"
        for (i, session) in sessions.iter().enumerate() {
            entries.push(PaletteEntry {
                icon: "›_",
                label: format!("Session {}: {}", i + 1, session.label),
                detail: format!(
                    "{}@{}:{}{}",
                    session.username, session.hostname, session.port, session.state
                ),
                action: PaletteAction::SwitchSession(i),
                score: 0,
            });
        }

        // Navigation commands
        entries.push(PaletteEntry {
            icon: "",
            label: "Dashboard: Sessions".to_string(),
            detail: "View active sessions".to_string(),
            action: PaletteAction::SetDashboardTab(DashboardTab::Sessions),
            score: 0,
        });
        entries.push(PaletteEntry {
            icon: "",
            label: "Dashboard: Hosts".to_string(),
            detail: "Browse and connect to hosts".to_string(),
            action: PaletteAction::SetDashboardTab(DashboardTab::Hosts),
            score: 0,
        });
        entries.push(PaletteEntry {
            icon: "",
            label: "Dashboard: Fleet".to_string(),
            detail: "Fleet health overview".to_string(),
            action: PaletteAction::SetDashboardTab(DashboardTab::Fleet),
            score: 0,
        });
        entries.push(PaletteEntry {
            icon: "",
            label: "Dashboard: Config".to_string(),
            detail: "Configuration overview".to_string(),
            action: PaletteAction::SetDashboardTab(DashboardTab::Config),
            score: 0,
        });

        if has_sessions {
            entries.push(PaletteEntry {
                icon: "",
                label: "Toggle: Split Pane".to_string(),
                detail: format!("Terminal + monitor side-by-side ({})", split_hint),
                action: PaletteAction::ToggleSplitPane,
                score: 0,
            });
            entries.push(PaletteEntry {
                icon: "",
                label: "View: Host Monitor".to_string(),
                detail: format!("Full-screen host metrics ({})", monitor_hint),
                action: PaletteAction::SetView(AppView::Monitor),
                score: 0,
            });
            entries.push(PaletteEntry {
                icon: "",
                label: "View: Port Forwarding".to_string(),
                detail: format!("Manage port forwards ({})", portfwd_hint),
                action: PaletteAction::SetView(AppView::PortForwarding),
                score: 0,
            });
            entries.push(PaletteEntry {
                icon: "",
                label: "View: File Browser".to_string(),
                detail: format!("Upload/download files ({})", files_hint),
                action: PaletteAction::SetView(AppView::FileBrowser),
                score: 0,
            });
        }

        entries.push(PaletteEntry {
            icon: "?",
            label: "Help".to_string(),
            detail: "Show keyboard shortcuts (?)".to_string(),
            action: PaletteAction::ToggleHelp,
            score: 0,
        });

        // Score and filter by query
        if !self.query.is_empty() {
            let q = self.query.to_lowercase();
            for entry in &mut entries {
                entry.score = fuzzy_score(&q, &entry.label, &entry.detail);
            }
            entries.retain(|e| e.score > 0);
            entries.sort_by_key(|e| std::cmp::Reverse(e.score));
        }

        self.entries = entries;
        // Clamp selection
        if self.selected >= self.entries.len() {
            self.selected = 0;
        }
    }

    pub fn move_down(&mut self) {
        if !self.entries.is_empty() {
            self.selected = (self.selected + 1) % self.entries.len();
        }
    }

    pub fn move_up(&mut self) {
        if !self.entries.is_empty() {
            if self.selected == 0 {
                self.selected = self.entries.len() - 1;
            } else {
                self.selected -= 1;
            }
        }
    }

    pub fn selected_action(&self) -> Option<&PaletteAction> {
        self.entries.get(self.selected).map(|e| &e.action)
    }
}

// ---------------------------------------------------------------------------
// Fuzzy scoring — simple substring matching with bonus for prefix/word starts
// ---------------------------------------------------------------------------

fn fuzzy_score(query: &str, label: &str, detail: &str) -> i32 {
    let label_lower = label.to_lowercase();
    let detail_lower = detail.to_lowercase();

    let mut score = 0i32;

    // Check each query word independently
    for word in query.split_whitespace() {
        let mut word_matched = false;

        if let Some(pos) = label_lower.find(word) {
            score += 10;
            if pos == 0 {
                score += 5; // prefix bonus
            }
            // Bonus for matching at word boundary
            if pos > 0 && !label.as_bytes()[pos - 1].is_ascii_alphanumeric() {
                score += 3;
            }
            word_matched = true;
        }

        if detail_lower.find(word).is_some() {
            score += 3;
            word_matched = true;
        }

        if !word_matched {
            return 0; // all query words must match somewhere
        }
    }

    score
}

// ---------------------------------------------------------------------------
// Rendering — centered overlay popup
// ---------------------------------------------------------------------------

/// Screen 6 — the command palette.
///
/// A floating surface on a scrim, not a bordered box with a title on the
/// rule: *"No box-drawing needed when you can composite."* A terminal cannot
/// blur, but it can dim the backdrop and lift the card off it with its own
/// background, which is the part that carries the effect.
pub fn render(frame: &mut Frame, palette: &CommandPalette, theme: &Theme) {
    let _ = theme;
    let area = frame.area();

    // ── the scrim: dim everything behind the card
    frame.render_widget(
        Block::default().style(Style::default().bg(d::BG).fg(d::FAINT)),
        area,
    );

    let width = 66u16.min(area.width.saturating_sub(6));
    let max_visible = 8usize;
    let rows = max_visible.min(palette.entries.len().max(1)) as u16;
    let height = (rows + 3).min(area.height.saturating_sub(4));
    let x = (area.width.saturating_sub(width)) / 2;
    // The design floats the card slightly above centre.
    let y = (area.height.saturating_sub(height)) * 46 / 100;
    let card = Rect::new(x, y, width, height);

    frame.render_widget(Clear, card);
    // The card's own surface, a shade above the background so it reads as
    // lifted rather than as a hole.
    frame.render_widget(
        Block::default().style(Style::default().bg(d::RULE).fg(d::FG)),
        card,
    );

    let inner = Rect {
        x: card.x + 2,
        y: card.y + 1,
        width: card.width.saturating_sub(4),
        height: card.height.saturating_sub(2),
    };
    if inner.height == 0 || inner.width == 0 {
        return;
    }

    // ── query line: `› host ▌`
    frame.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled("", Style::default().fg(d::CYAN)),
            Span::styled(
                palette.query.clone(),
                Style::default().fg(d::FG).add_modifier(Modifier::BOLD),
            ),
            Span::styled("", Style::default().fg(d::CYAN)),
        ])),
        Rect::new(inner.x, inner.y, inner.width, 1),
    );

    if palette.entries.is_empty() {
        frame.render_widget(
            Paragraph::new(d::none_line("nothing matches")),
            Rect::new(inner.x, inner.y + 2, inner.width, 1),
        );
        return;
    }

    let list_top = inner.y + 2;
    let list_height = inner.height.saturating_sub(2) as usize;
    let scroll = palette
        .selected
        .saturating_sub(list_height.saturating_sub(1));

    for (vi, ei) in (scroll..scroll + list_height.min(palette.entries.len())).enumerate() {
        let Some(entry) = palette.entries.get(ei) else {
            break;
        };
        let row_y = list_top + vi as u16;
        if row_y >= inner.y + inner.height {
            break;
        }
        let row = Rect::new(inner.x, row_y, inner.width, 1);
        let selected = ei == palette.selected;

        if selected {
            frame.render_widget(
                Block::default().style(Style::default().bg(d::ROW_SELECTED_BG)),
                row,
            );
        }

        let label_style = if selected {
            Style::default().fg(d::WHITE).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(d::FG)
        };

        // Results carry consequence — "3 facets differ", "10 · 2 reachable" —
        // so a choice is made by outcome rather than by guessing the verb.
        let left = format!("{} {}", entry.icon, entry.label);
        let hint = entry.detail.clone();
        let gap = (inner.width as usize)
            .saturating_sub(left.chars().count() + hint.chars().count() + 1)
            .max(1);

        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled(
                    format!("{} ", entry.icon),
                    Style::default().fg(if selected { d::CYAN } else { d::DIM }),
                ),
                Span::styled(entry.label.clone(), label_style),
                Span::raw(" ".repeat(gap)),
                Span::styled(hint, Style::default().fg(d::FAINT)),
            ])),
            row,
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fuzzy_score_exact_match() {
        assert!(fuzzy_score("connect", "Connect: bastion", "ops@bastion:22") > 0);
    }

    #[test]
    fn test_fuzzy_score_no_match() {
        assert_eq!(
            fuzzy_score("zzzzz", "Connect: bastion", "ops@bastion:22"),
            0
        );
    }

    #[test]
    fn test_fuzzy_score_multi_word() {
        let s = fuzzy_score("connect bastion", "Connect: bastion-east", "ops@bastion:22");
        assert!(s > 0);
    }

    #[test]
    fn test_fuzzy_score_multi_word_no_match() {
        assert_eq!(
            fuzzy_score("connect zzz", "Connect: bastion", "ops@bastion:22"),
            0
        );
    }

    #[test]
    fn test_fuzzy_score_prefix_bonus() {
        let prefix = fuzzy_score("con", "Connect: bastion", "");
        let mid = fuzzy_score("bas", "Connect: bastion", "");
        assert!(prefix > mid);
    }

    #[test]
    fn test_fuzzy_score_detail_match() {
        let s = fuzzy_score("prod", "Connect: bastion", "env=prod");
        assert!(s > 0);
    }

    #[test]
    fn test_palette_update_no_query() {
        let mut p = CommandPalette::new();
        p.update(&[], &[], false);
        // Should have at least the navigation + help entries
        assert!(p.entries.len() >= 5);
    }

    #[test]
    fn test_palette_update_filters() {
        let mut p = CommandPalette::new();
        p.query = "help".to_string();
        p.update(&[], &[], false);
        assert!(p.entries.iter().any(|e| e.label.contains("Help")));
    }

    #[test]
    fn test_palette_move_down_wraps() {
        let mut p = CommandPalette::new();
        p.update(&[], &[], false);
        let len = p.entries.len();
        for _ in 0..len {
            p.move_down();
        }
        assert_eq!(p.selected, 0);
    }

    #[test]
    fn test_palette_move_up_wraps() {
        let mut p = CommandPalette::new();
        p.update(&[], &[], false);
        p.move_up();
        assert_eq!(p.selected, p.entries.len() - 1);
    }
}