Skip to main content

radio_cli/
lib.rs

1mod artwork;
2pub mod cli;
3pub mod config;
4pub mod directory;
5pub mod engine;
6pub mod error;
7pub mod geo;
8pub mod messages;
9pub mod model;
10pub mod player;
11pub mod podcast;
12pub mod process;
13pub mod recorder;
14mod redact;
15pub mod service;
16pub mod speech;
17pub mod store;
18mod territories;
19pub mod tui;
20
21use unicode_segmentation::UnicodeSegmentation;
22pub const USER_AGENT: &str = concat!("radio-cli/", env!("CARGO_PKG_VERSION"));
23
24const CLEAN_TEXT_LIMIT: usize = 1024;
25
26/// External text must never introduce terminal escape sequences or new lines.
27///
28/// The length limit counts scalar values but never cuts through a grapheme
29/// cluster, so a joiner or combining mark is not left dangling at the end.
30pub fn clean_text(text: &str) -> String {
31    let mut cleaned = String::new();
32    let mut count = 0;
33    for grapheme in text.graphemes(true) {
34        // Control characters always form their own cluster, including CR LF.
35        if grapheme.chars().next().is_none_or(char::is_control) {
36            continue;
37        }
38        count += grapheme.chars().count();
39        if count > CLEAN_TEXT_LIMIT {
40            break;
41        }
42        cleaned.push_str(grapheme);
43    }
44    cleaned
45}
46
47#[cfg(test)]
48mod clean_text_tests {
49    use super::*;
50
51    #[test]
52    fn clean_text_strips_controls_and_caps_on_a_grapheme_boundary() {
53        assert_eq!(clean_text("a\u{1b}[31mb\r\nc\u{7f}"), "a[31mbc");
54        assert_eq!(clean_text("你好 ✓"), "你好 ✓");
55        let long = "x".repeat(CLEAN_TEXT_LIMIT + 10);
56        assert_eq!(clean_text(&long).chars().count(), CLEAN_TEXT_LIMIT);
57
58        // The cluster is five scalar values and would cross the limit: drop it whole.
59        let family = "👨\u{200d}👩\u{200d}👧";
60        let text = format!("{}{family}tail", "x".repeat(CLEAN_TEXT_LIMIT - 2));
61        assert_eq!(clean_text(&text), "x".repeat(CLEAN_TEXT_LIMIT - 2));
62
63        // Exactly at the limit the cluster is kept intact.
64        let text = format!("{}{family}tail", "x".repeat(CLEAN_TEXT_LIMIT - 5));
65        assert_eq!(
66            clean_text(&text),
67            format!("{}{family}", "x".repeat(CLEAN_TEXT_LIMIT - 5))
68        );
69
70        let accents = format!("e{}", "\u{301}".repeat(CLEAN_TEXT_LIMIT));
71        assert_eq!(clean_text(&accents), "");
72    }
73}
74
75#[cfg(test)]
76extern crate self as radio_cli;
77#[cfg(test)]
78#[path = "../tests/support/mod.rs"]
79mod test_support;