shw 0.1.0

Blazing fast terminal viewer for images, videos and GitHub-flavored Markdown using the kitty graphics protocol
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
//! Colours, styled text spans and status bar rendering.

use std::rc::Rc;

use unicode_width::UnicodeWidthStr;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rgb(pub u8, pub u8, pub u8);

impl Rgb {
    pub const fn hex(v: u32) -> Rgb {
        Rgb((v >> 16) as u8, (v >> 8) as u8, v as u8)
    }
}

/// GitHub Primer inspired palette for dark and light backgrounds.
#[derive(Clone, Debug)]
pub struct Theme {
    pub dark: bool,
    pub muted: Rgb,
    pub border: Rgb,
    pub link: Rgb,
    pub code_bg: Rgb,
    pub inline_code_bg: Rgb,
    pub bar_bg: Rgb,
    pub bar_fg: Rgb,
    pub accent: Rgb,
    pub note: Rgb,
    pub tip: Rgb,
    pub important: Rgb,
    pub warning: Rgb,
    pub caution: Rgb,
    pub search_bg: Rgb,
    pub syntect: &'static str,
}

impl Theme {
    pub fn new(dark: bool) -> Theme {
        if dark {
            Theme {
                dark,
                muted: Rgb::hex(0x9198a1),
                border: Rgb::hex(0x3d444d),
                link: Rgb::hex(0x4493f8),
                code_bg: Rgb::hex(0x151b23),
                inline_code_bg: Rgb::hex(0x2a313c),
                bar_bg: Rgb::hex(0x212830),
                bar_fg: Rgb::hex(0xd1d7e0),
                accent: Rgb::hex(0x4493f8),
                note: Rgb::hex(0x4493f8),
                tip: Rgb::hex(0x3fb950),
                important: Rgb::hex(0xab7df8),
                warning: Rgb::hex(0xd29922),
                caution: Rgb::hex(0xf85149),
                search_bg: Rgb::hex(0x9e6a03),
                syntect: "base16-ocean.dark",
            }
        } else {
            Theme {
                dark,
                muted: Rgb::hex(0x59636e),
                border: Rgb::hex(0xd1d9e0),
                link: Rgb::hex(0x0969da),
                code_bg: Rgb::hex(0xf6f8fa),
                inline_code_bg: Rgb::hex(0xeff1f3),
                bar_bg: Rgb::hex(0xe6eaef),
                bar_fg: Rgb::hex(0x1f2328),
                accent: Rgb::hex(0x0969da),
                note: Rgb::hex(0x0969da),
                tip: Rgb::hex(0x1a7f37),
                important: Rgb::hex(0x8250df),
                warning: Rgb::hex(0x9a6700),
                caution: Rgb::hex(0xd1242f),
                search_bg: Rgb::hex(0xfff8c5),
                syntect: "InspiredGitHub",
            }
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Style {
    pub fg: Option<Rgb>,
    pub bg: Option<Rgb>,
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub strike: bool,
    pub dim: bool,
    pub link: Option<Rc<str>>,
}

impl Style {
    pub fn fg(c: Rgb) -> Style {
        Style {
            fg: Some(c),
            ..Default::default()
        }
    }

    pub fn sgr(&self, out: &mut String) {
        out.push_str("\x1b[0");
        if self.bold {
            out.push_str(";1");
        }
        if self.dim {
            out.push_str(";2");
        }
        if self.italic {
            out.push_str(";3");
        }
        if self.underline {
            out.push_str(";4");
        }
        if self.strike {
            out.push_str(";9");
        }
        if let Some(Rgb(r, g, b)) = self.fg {
            out.push_str(&format!(";38;2;{r};{g};{b}"));
        }
        if let Some(Rgb(r, g, b)) = self.bg {
            out.push_str(&format!(";48;2;{r};{g};{b}"));
        }
        out.push('m');
    }
}

#[derive(Clone, Debug)]
pub struct Span {
    pub text: String,
    pub style: Style,
}

impl Span {
    pub fn new(text: impl Into<String>, style: Style) -> Span {
        Span {
            text: text.into(),
            style,
        }
    }
    pub fn plain(text: impl Into<String>) -> Span {
        Span {
            text: text.into(),
            style: Style::default(),
        }
    }
    pub fn width(&self) -> usize {
        self.text.width()
    }
}

pub fn spans_width(spans: &[Span]) -> usize {
    spans.iter().map(|s| s.width()).sum()
}

/// Write spans as ANSI text. `scale` > 1 uses the kitty text sizing protocol.
pub fn write_spans(out: &mut String, spans: &[Span], scale: u8, highlight: Option<(&str, Rgb)>) {
    let owned;
    let spans = match highlight {
        Some((q, bg)) if !q.is_empty() => {
            owned = highlight_spans(spans, q, bg);
            &owned[..]
        }
        _ => spans,
    };
    let mut cur_link: Option<&str> = None;
    for s in spans {
        let link = s.style.link.as_deref();
        if link != cur_link {
            match link {
                Some(url) => out.push_str(&format!("\x1b]8;;{url}\x1b\\")),
                None => out.push_str("\x1b]8;;\x1b\\"),
            }
            cur_link = link;
        }
        s.style.sgr(out);
        if scale > 1 {
            // OSC 66 payloads are limited to 4096 bytes; split long spans.
            let mut buf = String::new();
            for ch in s.text.chars() {
                buf.push(ch);
                if buf.len() > 3000 {
                    out.push_str(&format!("\x1b]66;s={scale};{buf}\x07"));
                    buf.clear();
                }
            }
            if !buf.is_empty() {
                out.push_str(&format!("\x1b]66;s={scale};{buf}\x07"));
            }
        } else {
            out.push_str(&s.text);
        }
    }
    if cur_link.is_some() {
        out.push_str("\x1b]8;;\x1b\\");
    }
    out.push_str("\x1b[0m");
}

/// Split spans so that ASCII-case-insensitive matches of `q` get a background.
fn highlight_spans(spans: &[Span], q: &str, bg: Rgb) -> Vec<Span> {
    let full: String = spans.iter().map(|s| s.text.as_str()).collect();
    let lower = full.to_ascii_lowercase();
    let ql = q.to_ascii_lowercase();
    let mut marks = Vec::new();
    let mut from = 0;
    while let Some(i) = lower[from..].find(&ql) {
        marks.push((from + i, from + i + ql.len()));
        from += i + ql.len().max(1);
        if from >= lower.len() {
            break;
        }
    }
    if marks.is_empty() {
        return spans.to_vec();
    }
    let mut out = Vec::new();
    let mut pos = 0;
    for s in spans {
        let (start, end) = (pos, pos + s.text.len());
        let mut cut = start;
        for &(a, b) in &marks {
            if b <= cut || a >= end {
                continue;
            }
            let a = a.max(cut);
            let b = b.min(end);
            if a > cut {
                out.push(Span::new(&full[cut..a], s.style.clone()));
            }
            let mut st = s.style.clone();
            st.bg = Some(bg);
            st.bold = true;
            out.push(Span::new(&full[a..b], st));
            cut = b;
        }
        if cut < end {
            out.push(Span::new(&full[cut..end], s.style.clone()));
        }
        pos = end;
    }
    out
}

/// Truncate a string to `max` display columns, adding an ellipsis.
pub fn truncate(s: &str, max: usize) -> String {
    if s.width() <= max {
        return s.to_string();
    }
    let mut out = String::new();
    let mut w = 0;
    for ch in s.chars() {
        let cw = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
        if w + cw + 1 > max {
            break;
        }
        out.push(ch);
        w += cw;
    }
    out.push('…');
    out
}

/// Render the bottom status bar at screen row `row` (1-based).
pub fn status_bar(
    theme: &Theme,
    row: u16,
    cols: u16,
    badge: &str,
    left: &str,
    right: &str,
) -> String {
    let cols = cols as usize;
    let mut s = format!("\x1b[{row};1H\x1b[0m\x1b[2K");
    let badge_txt = format!(" {badge} ");
    let bw = badge_txt.width();
    let rw = right.width();
    let avail = cols.saturating_sub(bw + rw + 3);
    let left = truncate(left, avail);
    let pad = cols.saturating_sub(bw + 1 + left.width() + rw + 1);
    let mut b = String::new();
    Style {
        fg: Some(if theme.dark {
            Rgb::hex(0x0d1117)
        } else {
            Rgb::hex(0xffffff)
        }),
        bg: Some(theme.accent),
        bold: true,
        ..Default::default()
    }
    .sgr(&mut b);
    b.push_str(&badge_txt);
    Style {
        fg: Some(theme.bar_fg),
        bg: Some(theme.bar_bg),
        ..Default::default()
    }
    .sgr(&mut b);
    b.push(' ');
    b.push_str(&left);
    b.push_str(&" ".repeat(pad));
    Style {
        fg: Some(theme.muted),
        bg: Some(theme.bar_bg),
        ..Default::default()
    }
    .sgr(&mut b);
    b.push_str(right);
    b.push(' ');
    b.push_str("\x1b[0m");
    s.push_str(&b);
    s
}

/// Render a centered help box listing key bindings (full screen, no image).
pub fn help_screen(
    theme: &Theme,
    cols: u16,
    rows: u16,
    title: &str,
    keys: &[(&str, &str)],
) -> String {
    let mut s = String::from("\x1b[0m\x1b[2J");
    let kw = keys.iter().map(|(k, _)| k.width()).max().unwrap_or(0);
    let dw = keys.iter().map(|(_, d)| d.width()).max().unwrap_or(0);
    let bw = (kw + dw + 7).max(title.width() + 4);
    let bh = keys.len() + 4;
    let x0 = (cols as usize).saturating_sub(bw) / 2 + 1;
    let y0 = (rows as usize).saturating_sub(bh) / 2 + 1;
    let mut border = String::new();
    Style::fg(theme.border).sgr(&mut border);
    s.push_str(&format!("\x1b[{y0};{x0}H{border}╭{}╮", "─".repeat(bw - 2)));
    let mut t = String::new();
    Style {
        fg: Some(theme.accent),
        bold: true,
        ..Default::default()
    }
    .sgr(&mut t);
    s.push_str(&format!(
        "\x1b[{};{x0}H{border}│{t} {title}{}{border}│",
        y0 + 1,
        " ".repeat(bw - 3 - title.width())
    ));
    s.push_str(&format!(
        "\x1b[{};{x0}H{border}│{}│",
        y0 + 2,
        " ".repeat(bw - 2)
    ));
    for (i, (k, d)) in keys.iter().enumerate() {
        let mut ks = String::new();
        Style {
            fg: Some(theme.link),
            bold: true,
            ..Default::default()
        }
        .sgr(&mut ks);
        let mut ds = String::new();
        Style::default().sgr(&mut ds);
        let line = format!(
            " {ks}{k}{}  {ds}{d}{}",
            " ".repeat(kw - k.width()),
            " ".repeat(bw - 5 - kw - d.width())
        );
        s.push_str(&format!(
            "\x1b[{};{x0}H{border}│{line}{border}│",
            y0 + 3 + i
        ));
    }
    s.push_str(&format!(
        "\x1b[{};{x0}H{border}╰{}╯\x1b[0m",
        y0 + 3 + keys.len(),
        "─".repeat(bw - 2)
    ));
    s
}

pub fn fmt_duration(secs: f64) -> String {
    let t = secs.max(0.0) as u64;
    let (h, m, s) = (t / 3600, (t / 60) % 60, t % 60);
    if h > 0 {
        format!("{h}:{m:02}:{s:02}")
    } else {
        format!("{m}:{s:02}")
    }
}

pub fn fmt_bytes(n: u64) -> String {
    const U: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
    let mut v = n as f64;
    let mut i = 0;
    while v >= 1024.0 && i < U.len() - 1 {
        v /= 1024.0;
        i += 1;
    }
    if i == 0 {
        format!("{n} B")
    } else {
        format!("{v:.1} {}", U[i])
    }
}

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

    #[test]
    fn highlight_splits() {
        let spans = vec![Span::plain("Hello "), Span::plain("World")];
        let h = highlight_spans(&spans, "o w", Rgb(1, 2, 3));
        let texts: Vec<_> = h.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(texts, vec!["Hell", "o ", "W", "orld"]);
    }

    #[test]
    fn durations() {
        assert_eq!(fmt_duration(75.0), "1:15");
        assert_eq!(fmt_duration(3725.0), "1:02:05");
    }
}