pub mod ascii;
pub mod c0;
pub mod c1;
pub mod charset;
pub mod clipboard;
pub mod color;
pub mod cost;
pub mod ctrl;
pub mod cursor;
pub mod cwd;
pub mod finalterm;
pub mod focus;
pub mod graphics;
pub mod hyperlink;
pub mod inband;
pub mod iterm2;
pub mod keypad;
pub mod kitty;
pub mod mode;
pub mod notification;
pub mod palette;
pub mod params;
pub mod passthrough;
pub mod paste;
pub mod program_status;
pub mod progress;
pub mod screen;
pub mod sgr;
pub mod status;
pub mod strip;
pub mod termcap;
pub mod text;
pub mod title;
pub mod truncate;
pub mod urxvt;
pub mod winop;
pub mod wrap;
pub mod xterm;
#[cfg(uncurses_bench)]
mod bench;
#[cfg(test)]
mod utf8_boundaries {
use super::{strip::strip, truncate, wrap};
const INPUTS: &[&str] = &[
"\x1b]0;\u{2705}\x07visible",
"\x1b]0;a\u{9c}b\x07visible",
"\x1b]0;a\u{9d}b\x07visible",
"\x1b]8;;https://example.com/\u{2705}\x07visible\x1b]8;;\x07",
"\x1bP1$r\u{2705}\x1b\\visible",
"\x1b_G\u{2705}\x1b\\visible",
"\x1b^\u{2705}\x1b\\visible",
"\x1bX\u{2705}\x1b\\visible",
"\x1b#\u{2705}visible",
"\x1b\u{2705}visible",
"\x1b]0;caf\u{e9} \u{2705} \u{4e00} \u{1f600}",
];
#[test]
fn the_text_utilities_never_see_a_split_character() {
for input in INPUTS {
for limit in [0usize, 1, 3, 7, 100] {
wrap::hardwrap(input, limit, false);
wrap::hardwrap(input, limit, true);
wrap::wordwrap(input, limit, wrap::DEFAULT_BREAKPOINTS);
wrap::wrap(input, limit, wrap::DEFAULT_BREAKPOINTS);
truncate::truncate(input, limit, "…");
truncate::truncate_left(input, limit, "…");
truncate::cut(input, limit / 2, limit);
strip(input);
}
}
}
#[test]
fn the_text_after_a_utf8_payload_survives() {
for input in &INPUTS[..INPUTS.len() - 1] {
assert_eq!(
strip(input),
"visible",
"strip lost the text after {input:?}"
);
assert_eq!(
truncate::truncate(input, 7, ""),
*input,
"truncate at the full width should keep {input:?} intact"
);
assert_eq!(
strip(&wrap::hardwrap(input, 100, false)),
"visible",
"hardwrap lost the text after {input:?}"
);
}
}
}
#[cfg(test)]
mod fuzz {
use super::{
strip::strip,
text::{WidthMode, string_width},
truncate, wrap,
};
const PLAIN: &[&str] = &[
"a",
"bb",
"hello",
" ",
" ",
"\t",
"\n",
"-",
",",
".",
";",
":",
"supercalifragilistic",
"\u{4e00}",
"\u{4e00}\u{4e01}\u{4e02}",
"\u{1f600}",
"\u{1f1fa}\u{1f1f8}",
"e\u{301}",
"a\u{200d}b",
"\u{2705}",
"\u{9c}",
"\u{9d}",
];
const SEQUENCES: &[&str] = &[
"\x07",
"\x1b[31m",
"\x1b[0m",
"\x1b[1;2;3m",
"\x1b]8;;https://example.com/\u{2705}\x1b\\",
"\x1b]0;title\x07",
"\x1bP1$r\u{2705}\x1b\\",
"\x1b_G\u{2705}\x1b\\",
"\x1b]0;unterminated",
"\x1b_",
"\x1b",
];
fn next(state: &mut u64) -> u64 {
let mut x = *state;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
*state = x;
x
}
fn build(state: &mut u64, alphabets: &[&[&str]]) -> String {
let n = (next(state) % 12) as usize + 1;
let mut s = String::new();
for _ in 0..n {
let a = alphabets[next(state) as usize % alphabets.len()];
s.push_str(a[next(state) as usize % a.len()]);
}
s
}
fn unconditional_wrap(s: &str, limit: usize, mode: WidthMode, eaw_wide: bool) -> String {
if limit == 0 {
return s.to_string();
}
let wrapped = wrap::wordwrap_mode(s, limit, wrap::DEFAULT_BREAKPOINTS, mode, eaw_wide);
let mut out = String::with_capacity(wrapped.len());
for (i, line) in wrapped.split('\n').enumerate() {
if i > 0 {
out.push('\n');
}
out.push_str(&wrap::hardwrap_mode(line, limit, false, mode, eaw_wide));
}
out
}
#[test]
fn skipping_the_hard_wrap_matches_never_skipping_it() {
let mut state = 0x9e37_79b9_7f4a_7c15u64;
for _ in 0..20_000 {
let s = build(&mut state, &[PLAIN]);
let limit = (next(&mut state) % 14) as usize;
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
for eaw_wide in [false, true] {
assert_eq!(
wrap::wrap_mode(&s, limit, wrap::DEFAULT_BREAKPOINTS, mode, eaw_wide),
unconditional_wrap(&s, limit, mode, eaw_wide),
"wrap skipped a hard wrap it needed\n input={s:?}\n limit={limit} mode={mode:?} eaw_wide={eaw_wide}"
);
}
}
}
}
#[test]
fn the_text_utilities_hold_on_input_containing_sequences() {
let mut state = 0x2545_f491_4f6c_dd1du64;
for _ in 0..20_000 {
let s = build(&mut state, &[PLAIN, SEQUENCES]);
let limit = (next(&mut state) % 14) as usize;
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
for eaw_wide in [false, true] {
let cut = truncate::truncate_mode(&s, limit, "", mode, eaw_wide);
assert!(
string_width(cut.as_bytes(), mode, eaw_wide) <= limit,
"truncate exceeded its limit\n input={s:?} -> {cut:?}\n limit={limit} mode={mode:?} eaw_wide={eaw_wide}"
);
truncate::truncate_left_mode(&s, limit, "", mode, eaw_wide);
truncate::cut_mode(&s, limit / 2, limit, mode, eaw_wide);
wrap::hardwrap_mode(&s, limit, true, mode, eaw_wide);
wrap::wrap_mode(&s, limit, wrap::DEFAULT_BREAKPOINTS, mode, eaw_wide);
wrap::wordwrap_mode(&s, limit, wrap::DEFAULT_BREAKPOINTS, mode, eaw_wide);
}
}
let plain = strip(&s);
assert!(
!plain.contains('\x1b'),
"strip left an escape behind: {s:?} -> {plain:?}"
);
}
}
}