radio-cli 0.8.2

A lightweight terminal radio with local favorites and recordings
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
//! Display-cell text helpers that never split a grapheme cluster.
//!
//! Widths follow `ratatui::buffer::CellWidth`, the measure used when the
//! buffer is painted, so what is computed here is what the terminal receives.
//!
//! These helpers measure text; they do not sanitize it. Clean external text
//! with `crate::clean_text` before measuring or painting it. Split structural
//! newlines and label/value tabs before measuring individual display lines.

use ratatui::buffer::CellWidth;
use unicode_segmentation::UnicodeSegmentation;

/// Cells occupied by one grapheme cluster.
fn cells(grapheme: &str) -> usize {
    if grapheme.chars().next().is_none_or(char::is_control) {
        0
    } else {
        usize::from(grapheme.cell_width())
    }
}

pub(super) fn width(text: &str) -> usize {
    text.graphemes(true).map(cells).sum()
}

/// Longest prefix made of whole graphemes that fits in `budget` cells.
pub(super) fn prefix(text: &str, budget: usize) -> &str {
    let mut used = 0;
    for (index, grapheme) in text.grapheme_indices(true) {
        let size = cells(grapheme);
        if used + size > budget {
            return &text[..index];
        }
        used += size;
    }
    text
}

/// Exactly `width` cells: padded when short, ellipsized when long.
pub(super) fn fit(text: &str, width: usize) -> String {
    let text_width = self::width(text);
    if text_width <= width {
        return format!("{text}{}", " ".repeat(width - text_width));
    }
    if width == 0 {
        return String::new();
    }
    let head = prefix(text, width - 1);
    format!("{head}…{}", " ".repeat(width - self::width(head) - 1))
}

/// Hard-wrap by cells. Every line holds at least one grapheme, so a cluster
/// wider than `width` still gets a line of its own instead of being dropped.
pub(super) fn wrap(text: &str, width: usize) -> Vec<&str> {
    let width = width.max(1);
    let mut lines = Vec::new();
    let mut start = 0;
    let mut used = 0;
    for (index, grapheme) in text.grapheme_indices(true) {
        let size = cells(grapheme);
        if used + size > width && index > start {
            lines.push(&text[start..index]);
            start = index;
            used = 0;
        }
        used += size;
    }
    lines.push(&text[start..]);
    lines
}

/// Wrap at spaces, falling back to a hard break inside a word wider than the
/// line. Spaces at a break and at the start of the text are dropped.
pub(super) fn wrap_words(text: &str, width: usize) -> Vec<&str> {
    let width = width.max(1);
    let mut lines = Vec::new();
    let mut start = 0;
    let mut used = 0;
    // Where the latest run of spaces in the current line ends its text and where the next word resumes.
    let mut gap: Option<(usize, usize)> = None;
    let mut after_space = false;
    for (index, grapheme) in text.grapheme_indices(true) {
        if grapheme == " " {
            if index == start {
                start += 1;
                continue;
            }
            gap = match gap {
                Some((end, _)) if after_space => Some((end, index + 1)),
                _ => Some((index, index + 1)),
            };
            used += 1;
            after_space = true;
            continue;
        }
        after_space = false;
        let size = cells(grapheme);
        if used + size > width && index > start {
            match gap.take() {
                Some((end, resume)) => {
                    lines.push(&text[start..end]);
                    start = resume;
                    used = self::width(&text[start..index]);
                }
                None => {
                    lines.push(&text[start..index]);
                    start = index;
                    used = 0;
                }
            }
        }
        used += size;
    }
    lines.push(text[start..].trim_end_matches(' '));
    lines
}

/// Prefer URL separators near the end of a line, retaining every byte so
/// continuation lines can be joined back into the original address.
pub(super) fn wrap_url(mut text: &str, width: usize) -> Vec<&str> {
    let width = width.max(1);
    let mut lines = Vec::new();
    while self::width(text) > width {
        let head = prefix(text, width);
        if head.is_empty() {
            let end = next_boundary(text, 0);
            lines.push(&text[..end]);
            text = &text[end..];
            continue;
        }
        let end = head
            .char_indices()
            .rev()
            .find(|(at, c)| {
                matches!(c, '/' | '?' | '&' | '=' | '#' | '-')
                    && self::width(&head[..*at]) >= width / 2
            })
            .map_or(head.len(), |(at, c)| at + c.len_utf8());
        lines.push(&text[..end]);
        text = &text[end..];
    }
    lines.push(text);
    lines
}

/// Longest prefix of whole graphemes that fits in `limit` bytes.
pub(super) fn prefix_bytes(text: &str, limit: usize) -> &str {
    let mut end = 0;
    for (index, grapheme) in text.grapheme_indices(true) {
        if index + grapheme.len() > limit {
            break;
        }
        end = index + grapheme.len();
    }
    &text[..end]
}

/// Byte offset of the grapheme boundary before `index`, or 0.
pub(super) fn prev_boundary(text: &str, index: usize) -> usize {
    text.grapheme_indices(true)
        .map(|(start, _)| start)
        .take_while(|&start| start < index)
        .last()
        .unwrap_or(0)
}

/// Byte offset of the grapheme boundary after `index`, or the text length.
pub(super) fn next_boundary(text: &str, index: usize) -> usize {
    text.grapheme_indices(true)
        .map(|(start, grapheme)| start + grapheme.len())
        .find(|&end| end > index)
        .unwrap_or(text.len())
}

/// Horizontally scroll `text` so the cursor (a byte index on a grapheme
/// boundary) stays visible. Returns the visible slice and the cursor column.
/// One cell must remain for the cursor, so its column is strictly below
/// `width` when the input area is nonempty.
pub(super) fn window(text: &str, cursor: usize, width: usize) -> (&str, usize) {
    if width == 0 {
        return ("", 0);
    }
    let mut start = 0;
    let mut column = self::width(&text[..cursor]);
    for grapheme in text[..cursor].graphemes(true) {
        if column < width {
            break;
        }
        start += grapheme.len();
        column = column.saturating_sub(cells(grapheme));
    }
    let visible = prefix(&text[start..], width);
    (visible, column)
}

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

    const FAMILY: &str = "👨\u{200d}👩\u{200d}👧";
    const THUMB: &str = "👍\u{1f3fd}";
    const HEART: &str = "❤\u{fe0f}";
    const FLAG: &str = "🇫🇷";
    const ACCENT: &str = "e\u{301}";

    #[test]
    fn width_counts_grapheme_clusters_as_the_terminal_paints_them() {
        for (text, expected) in [
            ("", 0),
            ("abc", 3),
            ("你好", 4),
            (ACCENT, 1),
            ("a\u{301}\u{302}", 1),
            (FAMILY, 2),
            (THUMB, 2),
            (HEART, 2),
            (FLAG, 2),
            ("🇫🇷🇩🇪", 4),
            ("\u{200b}", 0),
            ("a\tb", 2),
            ("ガ", 2),
        ] {
            assert_eq!(width(text), expected, "{text:?}");
        }
    }

    #[test]
    fn prefix_never_splits_a_cluster() {
        for (text, budget, expected) in [
            ("abc", 0, ""),
            ("abc", 2, "ab"),
            ("abc", 9, "abc"),
            ("你好", 1, ""),
            ("你好", 3, "你"),
            (FAMILY, 1, ""),
            (FAMILY, 2, FAMILY),
            ("a👨\u{200d}👩\u{200d}👧b", 2, "a"),
            ("a👨\u{200d}👩\u{200d}👧b", 3, "a👨\u{200d}👩\u{200d}👧"),
            (&format!("{ACCENT}x"), 1, ACCENT),
            (&format!("{FLAG}{FLAG}"), 3, FLAG),
        ] {
            assert_eq!(prefix(text, budget), expected, "{text:?} / {budget}");
        }
    }

    #[test]
    fn fit_pads_or_ellipsizes_to_the_exact_width() {
        for (text, cells, expected) in [
            ("abc", 5, "abc  "),
            ("abc", 3, "abc"),
            ("abcdef", 4, "abc…"),
            ("abcdef", 1, "…"),
            ("abcdef", 0, ""),
            ("你好世界", 5, "你好…"),
            ("你好世界", 4, "你… "),
            ("你好世界", 2, "… "),
            ("你", 1, "…"),
            (&format!("{FAMILY} hello"), 5, &format!("{FAMILY} h…")),
            (&format!("{FAMILY}{FAMILY}"), 3, &format!("{FAMILY}…")),
            (&format!("{FAMILY}{FAMILY}"), 2, "… "),
            (
                &format!("{ACCENT}{ACCENT}{ACCENT}"),
                2,
                &format!("{ACCENT}…"),
            ),
        ] {
            let result = fit(text, cells);
            assert_eq!(result, expected, "{text:?} / {cells}");
            assert_eq!(width(&result), cells, "{text:?} / {cells}");
        }
    }

    #[test]
    fn wrap_keeps_clusters_whole_and_lines_within_the_width() {
        for (text, cells, expected) in [
            ("", 4, vec![""]),
            ("abcdef", 4, vec!["abcd", "ef"]),
            ("abcd", 4, vec!["abcd"]),
            ("你好世", 4, vec!["你好", "世"]),
            ("你好世", 3, vec!["你", "好", "世"]),
            // A cluster wider than the line still gets a line of its own.
            ("你好", 1, vec!["你", "好"]),
            ("abcdef", 0, vec!["a", "b", "c", "d", "e", "f"]),
        ] {
            assert_eq!(wrap(text, cells), expected, "{text:?} / {cells}");
        }
        let text = format!("a{FAMILY}b{FAMILY}{ACCENT}{FLAG}");
        let lines = wrap(&text, 3);
        assert_eq!(lines.concat(), text);
        assert_eq!(
            lines,
            vec![
                format!("a{FAMILY}"),
                format!("b{FAMILY}"),
                format!("{ACCENT}{FLAG}")
            ]
        );
        assert!(lines.iter().all(|line| width(line) <= 3));
    }

    #[test]
    fn wrap_words_breaks_at_spaces_and_hard_wraps_long_words() {
        for (text, cells, expected) in [
            ("", 4, vec![""]),
            ("hello world", 20, vec!["hello world"]),
            ("hello world", 8, vec!["hello", "world"]),
            ("one two three", 7, vec!["one two", "three"]),
            ("one two three", 8, vec!["one two", "three"]),
            ("abcde fgh", 5, vec!["abcde", "fgh"]),
            ("a  b", 2, vec!["a", "b"]),
            ("abcd efgh", 4, vec!["abcd", "efgh"]),
            ("abcdefghij", 4, vec!["abcd", "efgh", "ij"]),
            ("ab cdefghij", 4, vec!["ab", "cdef", "ghij"]),
            ("abc ", 3, vec!["abc"]),
            ("  abc", 5, vec!["abc"]),
            ("你好世界", 4, vec!["你好", "世界"]),
            ("你好 世界 ok", 5, vec!["你好", "世界", "ok"]),
            ("abc", 0, vec!["a", "b", "c"]),
        ] {
            assert_eq!(wrap_words(text, cells), expected, "{text:?} / {cells}");
        }
        let text = format!("{FAMILY} {FAMILY}{FAMILY} {ACCENT}x{FLAG}");
        let lines = wrap_words(&text, 4);
        assert_eq!(
            lines,
            vec![
                FAMILY.to_owned(),
                format!("{FAMILY}{FAMILY}"),
                format!("{ACCENT}x{FLAG}")
            ]
        );
        assert!(lines.iter().all(|line| width(line) <= 4));
    }

    #[test]
    fn boundaries_move_over_whole_clusters_from_any_offset() {
        let text = format!("a{FAMILY}{ACCENT}{FLAG}你");
        let starts = [
            0,
            1,
            1 + FAMILY.len(),
            1 + FAMILY.len() + ACCENT.len(),
            1 + FAMILY.len() + ACCENT.len() + FLAG.len(),
            text.len(),
        ];
        for pair in starts.windows(2) {
            assert_eq!(next_boundary(&text, pair[0]), pair[1]);
            assert_eq!(prev_boundary(&text, pair[1]), pair[0]);
        }
        // A cursor left inside a cluster still resolves to its edges.
        let inside = 1 + '👨'.len_utf8();
        assert_eq!(prev_boundary(&text, inside), 1);
        assert_eq!(next_boundary(&text, inside), 1 + FAMILY.len());
        assert_eq!(prev_boundary(&text, 0), 0);
        assert_eq!(next_boundary(&text, text.len()), text.len());
        assert_eq!(prev_boundary("", 0), 0);
        assert_eq!(next_boundary("", 0), 0);
    }

    #[test]
    fn window_scrolls_by_cells_and_starts_on_a_cluster() {
        assert_eq!(window("abc", 3, 0), ("", 0));
        assert_eq!(window("abc", 3, 10), ("abc", 3));
        assert_eq!(window("abcdef", 6, 4), ("def", 3));
        assert_eq!(window("abcdef", 2, 4), ("abcd", 2));
        assert_eq!(window("你好世界", "你好世界".len(), 5), ("世界", 4));
        let text = format!("{FAMILY}{FAMILY}x");
        let cursor = text.len();
        let (visible, column) = window(&text, cursor, 4);
        assert_eq!(visible, format!("{FAMILY}x"));
        assert_eq!(column, 3);
        let text = format!("ab{ACCENT}cd");
        let cursor = text.len();
        let (visible, column) = window(&text, cursor, 4);
        assert_eq!(visible, format!("{ACCENT}cd"));
        assert_eq!(column, 3);
    }

    #[test]
    fn window_reserves_a_cursor_cell_at_mixed_width_boundaries() {
        for (text, cursor, budget, expected) in [
            ("a你b", 5, 3, ("b", 1)),
            ("a你b", 4, 3, ("你b", 2)),
            ("a你b", 5, 4, ("你b", 3)),
            ("a你b", 4, 2, ("b", 0)),
            ("a你b", 1, 3, ("a你", 1)),
            ("你a", 4, 3, ("a", 1)),
        ] {
            assert_eq!(window(text, cursor, budget), expected);
        }
        for text in ["a你b", "你a你", "a👨\u{200d}👩\u{200d}👧b", "ae\u{301}你"] {
            for cursor in text
                .grapheme_indices(true)
                .map(|(at, _)| at)
                .chain([text.len()])
            {
                for budget in 1..=6 {
                    let (visible, column) = window(text, cursor, budget);
                    assert!(width(visible) <= budget, "{text:?}, {cursor}, {budget}");
                    assert!(column < budget, "{text:?}, {cursor}, {budget}");
                }
            }
        }
    }
}