purple-ssh 1.27.0

Manage SSH configs and launch connections from the terminal. TUI host manager with search, tags, tunnels, command snippets, password management (keychain, 1Password, Bitwarden, pass, Vault), cloud sync (AWS EC2, DigitalOcean, Vultr, Linode, Hetzner, UpCloud, Proxmox VE, Scaleway, GCP), self-update and round-trip fidelity for ~/.ssh/config.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::time::{SystemTime, UNIX_EPOCH};

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Padding, Paragraph};

use super::theme;
use crate::app::{App, PingStatus};
use crate::history::ConnectionHistory;
use crate::ssh_config::model::ConfigElement;

const LABEL_WIDTH: usize = 14;

pub fn render(frame: &mut Frame, app: &App, area: Rect) {
    let host = match app.selected_host() {
        Some(h) => h,
        None => {
            let block = Block::bordered()
                .border_type(BorderType::Rounded)
                .padding(Padding::horizontal(1))
                .border_style(theme::border());
            let empty = Paragraph::new(" Select a host to see details.")
                .style(theme::muted())
                .block(block);
            frame.render_widget(empty, area);
            return;
        }
    };

    let title = format!(" {} ", host.alias);
    let block = Block::bordered()
        .border_type(BorderType::Rounded)
        .padding(Padding::horizontal(1))
        .title(Span::styled(title, theme::brand()))
        .border_style(theme::border());

    let inner_width = (area.width as usize).saturating_sub(4); // minus borders + padding
    let max_value_width = inner_width.saturating_sub(LABEL_WIDTH); // minus label

    let mut lines: Vec<Line<'static>> = Vec::new();

    // Connection section
    lines.push(Line::from(""));
    lines.push(section_header("Connection"));

    push_field(&mut lines, "Host", &host.hostname, max_value_width);

    if !host.user.is_empty() {
        push_field(&mut lines, "User", &host.user, max_value_width);
    }

    if host.port != 22 {
        push_field(&mut lines, "Port", &host.port.to_string(), max_value_width);
    }

    if !host.proxy_jump.is_empty() {
        push_field(&mut lines, "ProxyJump", &host.proxy_jump, max_value_width);
    }

    if !host.identity_file.is_empty() {
        let key_display = host
            .identity_file
            .rsplit('/')
            .next()
            .unwrap_or(&host.identity_file);
        push_field(&mut lines, "Key", key_display, max_value_width);
    }

    if let Some(ref askpass) = host.askpass {
        push_field(&mut lines, "Password", askpass, max_value_width);
    }

    // Activity section
    let history_entry = app.history.entries.get(&host.alias);
    let ping = app.ping_status.get(&host.alias);

    if history_entry.is_some() || ping.is_some() {
        lines.push(Line::from(""));
        lines.push(section_header("Activity"));

        if let Some(entry) = history_entry {
            let ago = ConnectionHistory::format_time_ago(entry.last_connected);
            if !ago.is_empty() {
                push_field(&mut lines, "Last SSH", &ago, max_value_width);
            }
            push_field(
                &mut lines,
                "Connections",
                &entry.count.to_string(),
                max_value_width,
            );

            if !entry.timestamps.is_empty() && inner_width >= 10 {
                let chart_lines = activity_sparkline(&entry.timestamps, inner_width);
                if !chart_lines.is_empty() {
                    lines.push(Line::from(""));
                    lines.extend(chart_lines);
                }
            }
        }

        if let Some(status) = ping {
            let (text, style) = match status {
                PingStatus::Checking => ("checking...", theme::muted()),
                PingStatus::Reachable => ("reachable", theme::success()),
                PingStatus::Unreachable => ("unreachable", theme::error()),
                PingStatus::Skipped => ("skipped", theme::muted()),
            };
            lines.push(Line::from(vec![
                Span::styled(
                    format!("{:<width$}", "Status", width = LABEL_WIDTH),
                    theme::muted(),
                ),
                Span::styled(text, style),
            ]));
        }
    }

    // Tags section
    if !host.tags.is_empty() || host.provider.is_some() {
        lines.push(Line::from(""));
        lines.push(section_header("Tags"));

        let mut tag_spans = Vec::new();
        for tag in &host.tags {
            tag_spans.push(Span::styled(format!("#{}", tag), theme::accent()));
            tag_spans.push(Span::raw("  "));
        }
        if let Some(ref provider) = host.provider {
            tag_spans.push(Span::styled(format!("#{}", provider), theme::accent()));
        }
        lines.push(Line::from(tag_spans));
    }

    // Provider metadata section
    if !host.provider_meta.is_empty() {
        lines.push(Line::from(""));
        let header = match host.provider.as_deref() {
            Some(name) => crate::providers::provider_display_name(name).to_string(),
            None => "Provider".to_string(),
        };
        lines.push(section_header(&header));

        for (key, value) in &host.provider_meta {
            let label = meta_label(key);
            push_field(&mut lines, &label, value, max_value_width);
        }
    }

    // Tunnels section
    let tunnel_active = app.active_tunnels.contains_key(&host.alias);
    if host.tunnel_count > 0 {
        lines.push(Line::from(""));
        let tunnel_label = if tunnel_active {
            "Tunnels (active)"
        } else {
            "Tunnels"
        };
        lines.push(section_header(tunnel_label));

        let rules = find_tunnel_rules(&app.config.elements, &host.alias);
        let style = if tunnel_active {
            theme::bold()
        } else {
            theme::muted()
        };
        for rule in rules.iter().take(5) {
            lines.push(Line::from(Span::styled(rule.to_string(), style)));
        }
        if rules.len() > 5 {
            lines.push(Line::from(Span::styled(
                format!("(and {} more...)", rules.len() - 5),
                theme::muted(),
            )));
        }
    }

    // Snippets hint
    let snippet_count = app.snippet_store.snippets.len();
    if snippet_count > 0 {
        lines.push(Line::from(""));
        lines.push(section_header("Snippets"));
        lines.push(Line::from(Span::styled(
            format!("{} available (r to run)", snippet_count),
            theme::muted(),
        )));
    }

    // Source section (for included hosts)
    if let Some(ref source) = host.source_file {
        lines.push(Line::from(""));
        lines.push(Line::from(vec![
            Span::styled(
                format!("{:<width$}", "Source", width = LABEL_WIDTH),
                theme::muted(),
            ),
            Span::styled(source.display().to_string(), theme::muted()),
        ]));
    }

    lines.push(Line::from(""));

    let paragraph = Paragraph::new(lines)
        .block(block)
        .scroll((app.ui.detail_scroll, 0));
    frame.render_widget(paragraph, area);
}

fn push_field(lines: &mut Vec<Line<'static>>, label: &str, value: &str, max_value_width: usize) {
    let display = if max_value_width > 0 {
        super::truncate(value, max_value_width)
    } else {
        value.to_string()
    };
    lines.push(Line::from(vec![
        Span::styled(
            format!("{:<width$}", label, width = LABEL_WIDTH),
            theme::muted(),
        ),
        Span::styled(display, theme::bold()),
    ]));
}

/// Map metadata keys to human-readable labels.
fn meta_label(key: &str) -> String {
    match key {
        "region" => "Region".to_string(),
        "plan" => "Plan".to_string(),
        "os" => "OS".to_string(),
        "node" => "Node".to_string(),
        "type" => "Type".to_string(),
        "status" => "State".to_string(),
        other => {
            // Capitalize first letter
            let mut chars = other.chars();
            match chars.next() {
                Some(c) => c.to_uppercase().to_string() + chars.as_str(),
                None => String::new(),
            }
        }
    }
}

fn section_header(label: &str) -> Line<'static> {
    Line::from(Span::styled(label.to_string(), theme::section_header()))
}

// Block sparkline using lower block elements (▁▂▃▄▅▆▇█).
// 2 rows tall = 16 height levels. Each character = ~2 days over 12 weeks.
// History retains 90 days of timestamps; chart shows 84 (12 clean weeks).
const BLOCKS: [char; 9] = [' ', '\u{2581}', '\u{2582}', '\u{2583}', '\u{2584}', '\u{2585}', '\u{2586}', '\u{2587}', '\u{2588}'];
const CHART_DAYS: u64 = 84;

fn activity_sparkline(timestamps: &[u64], chart_width: usize) -> Vec<Line<'static>> {
    if chart_width == 0 {
        return Vec::new();
    }

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let range_secs = CHART_DAYS * 86400;
    let bucket_secs = range_secs as f64 / chart_width as f64;
    let cutoff = now.saturating_sub(range_secs);

    let mut buckets = vec![0u64; chart_width];
    for &ts in timestamps {
        if ts < cutoff || ts > now {
            continue;
        }
        let age = now.saturating_sub(ts);
        let idx = chart_width
            - 1
            - ((age as f64 / bucket_secs).floor() as usize).min(chart_width - 1);
        buckets[idx] += 1;
    }

    if buckets.iter().all(|&v| v == 0) {
        return Vec::new();
    }

    let max_val = buckets.iter().copied().max().unwrap_or(1).max(1);
    let total_levels = 16usize; // 2 rows x 8 levels

    let heights: Vec<usize> = buckets
        .iter()
        .map(|&v| {
            if v == 0 {
                0
            } else {
                ((v as f64 / max_val as f64) * total_levels as f64).ceil() as usize
            }
        })
        .collect();

    let mut chart_lines = Vec::new();

    // Top row (only rendered if any bar exceeds half height)
    if heights.iter().any(|&h| h > 8) {
        let mut top = String::with_capacity(chart_width * 3);
        for &h in &heights {
            if h > 8 {
                top.push(BLOCKS[(h - 8).min(8)]);
            } else {
                top.push(' ');
            }
        }
        chart_lines.push(Line::from(Span::styled(top, theme::bold())));
    }

    // Bottom row
    let mut bottom = String::with_capacity(chart_width * 3);
    for &h in &heights {
        if h == 0 {
            bottom.push(' ');
        } else if h >= 8 {
            bottom.push(BLOCKS[8]);
        } else {
            bottom.push(BLOCKS[h]);
        }
    }
    chart_lines.push(Line::from(Span::styled(bottom, theme::bold())));

    // Axis labels
    let left_label = format!("{}w", CHART_DAYS / 7);
    let right_label = "now";
    let gap = chart_width.saturating_sub(left_label.len() + right_label.len());
    chart_lines.push(Line::from(vec![
        Span::styled(left_label, theme::muted()),
        Span::raw(" ".repeat(gap)),
        Span::styled(right_label.to_string(), theme::muted()),
    ]));

    chart_lines
}

fn find_tunnel_rules(elements: &[ConfigElement], alias: &str) -> Vec<String> {
    for element in elements {
        match element {
            ConfigElement::HostBlock(block) if block.host_pattern == alias => {
                return block
                    .directives
                    .iter()
                    .filter(|d| !d.is_non_directive)
                    .filter_map(|d| {
                        let prefix = match d.key.to_lowercase().as_str() {
                            "localforward" => "L",
                            "remoteforward" => "R",
                            "dynamicforward" => "D",
                            _ => return None,
                        };
                        Some(format!("{} {}", prefix, d.value))
                    })
                    .collect();
            }
            ConfigElement::Include(include) => {
                for file in &include.resolved_files {
                    let result = find_tunnel_rules(&file.elements, alias);
                    if !result.is_empty() {
                        return result;
                    }
                }
            }
            _ => {}
        }
    }
    Vec::new()
}

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

    fn now() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }

    #[test]
    fn sparkline_empty_timestamps() {
        let result = activity_sparkline(&[], 40);
        assert!(result.is_empty());
    }

    #[test]
    fn sparkline_all_outside_range() {
        let old = now() - 100 * 86400;
        let result = activity_sparkline(&[old], 40);
        assert!(result.is_empty());
    }

    #[test]
    fn sparkline_single_timestamp() {
        let ts = now() - 86400;
        let lines = activity_sparkline(&[ts], 40);
        assert!(!lines.is_empty());
        // Bottom row + axis = at least 2 lines
        assert!(lines.len() >= 2);
    }

    #[test]
    fn sparkline_multiple_buckets() {
        let n = now();
        let timestamps: Vec<u64> = (0..84).map(|day| n - day * 86400).collect();
        let lines = activity_sparkline(&timestamps, 40);
        assert!(lines.len() >= 2);
    }

    #[test]
    fn sparkline_all_in_one_bucket() {
        let n = now();
        let timestamps: Vec<u64> = (0..10).map(|i| n - i * 60).collect();
        let lines = activity_sparkline(&timestamps, 20);
        assert!(lines.len() >= 2);
    }

    #[test]
    fn sparkline_axis_labels() {
        let ts = now() - 86400;
        let lines = activity_sparkline(&[ts], 30);
        let axis = lines.last().unwrap();
        let text: String = axis.spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(text.contains("12w"));
        assert!(text.contains("now"));
    }

    #[test]
    fn sparkline_narrow_width() {
        let ts = now() - 86400;
        let lines = activity_sparkline(&[ts], 10);
        assert!(lines.len() >= 2);
    }

    #[test]
    fn sparkline_two_rows_for_high_variance() {
        let n = now();
        // One bucket with many hits, rest with few
        let mut timestamps: Vec<u64> = vec![n; 100];
        timestamps.push(n - 40 * 86400);
        let lines = activity_sparkline(&timestamps, 20);
        // Should have top row + bottom row + axis = 3 lines
        assert_eq!(lines.len(), 3);
    }
}