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    // Printable ASCII bytes (0x20..=0x7E) are always standalone 1-scalar
32    // non-control grapheme clusters under UAX #29.
33    if text.bytes().all(|b| (0x20..=0x7E).contains(&b)) {
34        return text[..text.len().min(CLEAN_TEXT_LIMIT)].to_owned();
35    }
36    let mut cleaned = String::with_capacity(text.len().min(CLEAN_TEXT_LIMIT));
37    let mut count = 0;
38    for grapheme in text.graphemes(true) {
39        // Control characters always form their own cluster, including CR LF.
40        if grapheme.chars().next().is_none_or(char::is_control) {
41            continue;
42        }
43        count += grapheme.chars().count();
44        if count > CLEAN_TEXT_LIMIT {
45            break;
46        }
47        cleaned.push_str(grapheme);
48    }
49    cleaned
50}
51
52#[cfg(test)]
53mod clean_text_tests {
54    use super::*;
55
56    #[test]
57    fn clean_text_strips_controls_and_caps_on_a_grapheme_boundary() {
58        assert_eq!(clean_text("a\u{1b}[31mb\r\nc\u{7f}"), "a[31mbc");
59        assert_eq!(clean_text("你好 ✓"), "你好 ✓");
60        let long = "x".repeat(CLEAN_TEXT_LIMIT + 10);
61        assert_eq!(clean_text(&long).chars().count(), CLEAN_TEXT_LIMIT);
62
63        // The cluster is five scalar values and would cross the limit: drop it whole.
64        let family = "👨\u{200d}👩\u{200d}👧";
65        let text = format!("{}{family}tail", "x".repeat(CLEAN_TEXT_LIMIT - 2));
66        assert_eq!(clean_text(&text), "x".repeat(CLEAN_TEXT_LIMIT - 2));
67
68        // Exactly at the limit the cluster is kept intact.
69        let text = format!("{}{family}tail", "x".repeat(CLEAN_TEXT_LIMIT - 5));
70        assert_eq!(
71            clean_text(&text),
72            format!("{}{family}", "x".repeat(CLEAN_TEXT_LIMIT - 5))
73        );
74
75        let accents = format!("e{}", "\u{301}".repeat(CLEAN_TEXT_LIMIT));
76        assert_eq!(clean_text(&accents), "");
77    }
78}
79
80#[cfg(test)]
81extern crate self as radio_cli;
82#[cfg(test)]
83#[path = "../tests/support/prop.rs"]
84pub(crate) mod prop;
85#[cfg(test)]
86mod prop_tests;
87#[cfg(test)]
88#[path = "../tests/support/mod.rs"]
89mod test_support;
90
91mod files;
92mod timestamp;