mod fuzzy;
mod tables;
pub use fuzzy::{Fuzzy, fuzzy, highlight};
pub use crate::term::content_width;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Token<'a> {
Escape(&'a str),
Text(&'a str, usize),
}
pub(crate) fn tokens(s: &str) -> Tokens<'_> {
Tokens { s, at: 0 }
}
pub(crate) struct Tokens<'a> {
s: &'a str,
at: usize,
}
impl<'a> Iterator for Tokens<'a> {
type Item = Token<'a>;
fn next(&mut self) -> Option<Token<'a>> {
let rest = &self.s[self.at..];
if rest.is_empty() {
return None;
}
if rest.starts_with('\x1b') {
let len = escape_len(rest);
self.at += len;
return Some(Token::Escape(&rest[..len]));
}
let (len, width) = cluster(rest);
self.at += len;
Some(Token::Text(&rest[..len], width))
}
}
fn escape_len(s: &str) -> usize {
let bytes = s.as_bytes();
match bytes.get(1) {
Some(b'[') => bytes[2..]
.iter()
.position(|b| (b'@'..=b'~').contains(b))
.map_or(s.len(), |i| i + 3),
Some(b']') => {
let mut i = 2;
while i < bytes.len() {
match bytes[i] {
0x07 => return i + 1,
0x1b if bytes.get(i + 1) == Some(&b'\\') => return i + 2,
_ => i += 1,
}
}
s.len()
}
Some(_) => 1 + s[1..].chars().next().map_or(0, char::len_utf8),
None => 1,
}
}
const ZWJ: char = '\u{200D}';
const VS16: char = '\u{FE0F}';
fn cluster(s: &str) -> (usize, usize) {
let mut chars = s.chars();
let Some(first) = chars.next() else { return (0, 0) };
if first == '\t' {
return (1, 1);
}
if first.is_control() {
return (first.len_utf8(), 0);
}
let mut len = first.len_utf8();
let mut width = char_width(first);
if is_regional(first) {
if let Some(second) = chars.next().filter(|&c| is_regional(c)) {
return (len + second.len_utf8(), 2);
}
return (len, width);
}
let emoji = is_pictographic(first);
let mut joined = false;
for c in chars {
if joined {
joined = false;
if emoji && is_pictographic(c) {
len += c.len_utf8();
continue;
}
break;
}
if c == ZWJ {
joined = true;
} else if c == VS16 {
width = width.max(2);
} else if emoji && is_skin_tone(c) {
} else if !is_zero_width(c) {
break;
}
len += c.len_utf8();
}
(len, width)
}
fn is_regional(c: char) -> bool {
('\u{1F1E6}'..='\u{1F1FF}').contains(&c)
}
fn is_skin_tone(c: char) -> bool {
('\u{1F3FB}'..='\u{1F3FF}').contains(&c)
}
fn is_pictographic(c: char) -> bool {
matches!(c as u32,
0x00A9 | 0x00AE | 0x203C | 0x2049 | 0x2122 | 0x2139 | 0x2194..=0x21AA | 0x231A..=0x23FF
| 0x24C2 | 0x25AA..=0x25FE | 0x2600..=0x27BF | 0x2934 | 0x2935 | 0x2B05..=0x2B55
| 0x3030 | 0x303D | 0x3297 | 0x3299 | 0x1F000..=0x1F1E5 | 0x1F200..=0x1FAFF
| 0x1FC00..=0x1FFFD)
}
fn in_table(table: &[(u32, u32)], c: char) -> bool {
let c = c as u32;
table
.binary_search_by(|&(lo, hi)| {
if hi < c {
std::cmp::Ordering::Less
} else if lo > c {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
})
.is_ok()
}
fn is_zero_width(c: char) -> bool {
(c as u32) >= 0x300 && in_table(tables::ZERO, c)
}
pub fn strip_ansi(s: &str) -> String {
tokens(s)
.filter_map(|t| match t {
Token::Text(t, _) => Some(t),
Token::Escape(_) => None,
})
.collect()
}
pub fn char_width(c: char) -> usize {
let n = c as u32;
if c == '\t' {
1
} else if n < 0x20 || (0x7F..0xA0).contains(&n) {
0
} else if n < 0x300 {
1
} else if in_table(tables::ZERO, c) {
0
} else if in_table(tables::WIDE, c) {
2
} else {
1
}
}
pub fn width(s: &str) -> usize {
tokens(s)
.map(|t| match t {
Token::Text(_, w) => w,
Token::Escape(_) => 0,
})
.sum()
}
pub fn pad(s: &str, cols: usize) -> String {
format!("{s}{}", " ".repeat(cols.saturating_sub(width(s))))
}
pub fn pad_left(s: &str, cols: usize) -> String {
format!("{}{s}", " ".repeat(cols.saturating_sub(width(s))))
}
pub fn truncate(s: &str, cols: usize) -> String {
if cols == 0 {
return String::new();
}
if width(s) <= cols {
return s.to_string();
}
let mut out = String::new();
let (mut used, mut cut) = (0, false);
for token in tokens(s) {
match token {
Token::Escape(e) => out += e,
Token::Text(_, _) if cut => {}
Token::Text(t, w) if used + w < cols => {
out += t;
used += w;
}
Token::Text(_, _) => {
out += "β¦";
cut = true;
}
}
}
out
}
pub fn truncate_start(s: &str, cols: usize) -> String {
if cols == 0 {
return String::new();
}
if width(s) <= cols {
return s.to_string();
}
let all: Vec<Token<'_>> = tokens(s).collect();
let mut used = 0;
let mut start = all.len();
for (i, token) in all.iter().enumerate().rev() {
match *token {
Token::Escape(_) => start = i,
Token::Text(_, w) if used + w < cols => {
used += w;
start = i;
}
Token::Text(..) => break,
}
}
let mut out: String = all[..start]
.iter()
.filter_map(|t| match t {
Token::Escape(e) => Some(*e),
Token::Text(..) => None,
})
.collect();
out.push('β¦');
for token in &all[start..] {
match token {
Token::Escape(t) | Token::Text(t, _) => out.push_str(t),
}
}
out
}
pub fn char_width_at(c: char, column: usize, tab_stop: usize) -> usize {
if c == '\t' {
let stop = tab_stop.max(1);
stop - column % stop
} else {
char_width(c)
}
}
pub fn width_with_tabs(s: &str, tab_stop: usize) -> usize {
tokens(s).fold(0, |at, token| match token {
Token::Text("\t", _) => at + char_width_at('\t', at, tab_stop),
Token::Text(_, w) => at + w,
Token::Escape(_) => at,
})
}
pub(crate) fn expand_tabs(s: &str) -> String {
if !s.contains('\t') {
return s.to_string();
}
let mut out = String::with_capacity(s.len());
let mut col = 0;
for token in tokens(s) {
match token {
Token::Text("\t", _) => {
let n = 8 - col % 8;
out.extend(std::iter::repeat_n(' ', n));
col += n;
}
Token::Text(t, w) => {
out += t;
col = if t == "\n" { 0 } else { col + w };
}
Token::Escape(e) => out += e,
}
}
out
}
pub fn wrap(s: &str, cols: usize) -> Vec<String> {
let cols = cols.max(1);
let mut lines = Vec::new();
for para in s.split('\n') {
let (mut line, mut used) = (String::new(), 0);
for word in para.split_whitespace() {
let w = width(word);
if w > cols {
if !line.is_empty() {
lines.push(std::mem::take(&mut line));
}
used = 0;
for token in tokens(word) {
match token {
Token::Escape(e) => line += e,
Token::Text(t, w) => {
if used + w > cols && used > 0 {
lines.push(std::mem::take(&mut line));
used = 0;
}
line += t;
used += w;
}
}
}
continue;
}
if !line.is_empty() && used + 1 + w > cols {
lines.push(std::mem::take(&mut line));
used = 0;
}
if !line.is_empty() {
line.push(' ');
used += 1;
}
line += word;
used += w;
}
lines.push(line);
}
lines
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn measures_without_escapes() {
assert_eq!(width("\x1b[38;2;1;2;3mhi\x1b[39m"), 2);
assert_eq!(width("\x1b]8;;https://x.y\x1b\\link\x1b]8;;\x1b\\"), 4);
assert_eq!(width("ζ₯ζ¬"), 4);
assert_eq!(width("core Β· server"), 13);
}
#[test]
fn truncates_with_ellipsis() {
assert_eq!(truncate("hello world", 8), "hello wβ¦");
assert_eq!(truncate("short", 8), "short");
}
#[test]
fn wraps_words_and_breaks_long_ones() {
assert_eq!(wrap("the quick brown fox", 9), ["the quick", "brown fox"]);
assert_eq!(wrap("abcdefghij", 4), ["abcd", "efgh", "ij"]);
assert_eq!(wrap("a\nb", 10), ["a", "b"]);
}
#[test]
fn pads_styled_text_by_visible_width() {
assert_eq!(width(&pad("\x1b[1mab\x1b[22m", 5)), 5);
assert_eq!(pad_left("7", 3), " 7");
}
#[test]
fn strips_every_kind_of_escape() {
assert_eq!(strip_ansi("\x1b[1;31mred\x1b[0m"), "red");
assert_eq!(strip_ansi("\x1bcreset"), "reset");
assert_eq!(strip_ansi("\x1b]0;title\x07after"), "after");
}
#[test]
fn emoji_and_their_sequences_are_two_columns() {
for s in ["π", "β
", "π« ", "β€οΈ", "π¨βπ©βπ§", "ππ½", "π¬π§", "1οΈβ£", "ζ₯"] {
assert_eq!(width(s), 2, "{s:?}");
}
assert_eq!(width("β€"), 1);
assert_eq!(width("π¬π§π«π·"), 4);
}
#[test]
fn marks_and_controls_take_no_columns() {
assert_eq!(width("ΰΈΰΈ±"), 1, "Thai vowel sign");
assert_eq!(width("e\u{301}"), 1);
assert_eq!(width("a\u{200B}b"), 2);
assert_eq!(width("ν\u{1161}"), 2);
assert_eq!(char_width('\u{7}'), 0);
assert_eq!(width("a\u{7}b"), 2);
}
#[test]
fn tabs_are_a_column_until_expanded() {
assert_eq!(width("ab\tc"), 4);
assert_eq!(expand_tabs("ab\tc"), "ab c");
assert_eq!(expand_tabs("ζ₯\t\x1b[1mx\tz"), "ζ₯ \x1b[1mx z");
assert_eq!(expand_tabs("a\nb\tc"), "a\nb c");
}
#[test]
fn tokens_split_escapes_from_clusters() {
let found: Vec<Token> = tokens("\x1b[1me\u{301}\x1b]8;;u\x07π¨βπ©βπ§").collect();
assert_eq!(
found,
[
Token::Escape("\x1b[1m"),
Token::Text("e\u{301}", 1),
Token::Escape("\x1b]8;;u\x07"),
Token::Text("π¨βπ©βπ§", 2),
]
);
assert_eq!(tokens("\x1b").collect::<Vec<_>>(), [Token::Escape("\x1b")]);
assert_eq!(tokens("\x1b[31").collect::<Vec<_>>(), [Token::Escape("\x1b[31")]);
}
#[test]
fn truncating_keeps_escapes_and_whole_characters() {
assert_eq!(truncate("\x1b[31mhello\x1b[0m", 3), "\x1b[31mheβ¦\x1b[0m");
assert_eq!(truncate("hello", 0), "");
assert_eq!(truncate("ζ₯ζ¬θͺ", 4), "ζ₯β¦");
assert_eq!(truncate("e\u{301}e\u{301}e\u{301}", 2), "e\u{301}β¦");
assert_eq!(truncate("π¬π§π«π·", 3), "π¬π§β¦");
}
#[test]
fn wrapping_counts_columns_and_keeps_characters_whole() {
assert_eq!(wrap("ζ₯ζ¬θͺζ₯ζ¬θͺ", 4), ["ζ₯ζ¬", "θͺζ₯", "ζ¬θͺ"]);
assert_eq!(wrap("e\u{301}e\u{301}e\u{301}", 2), ["e\u{301}e\u{301}", "e\u{301}"]);
assert!(wrap("ζ₯ζ¬θͺ ζ₯ζ¬θͺ ζ₯", 7).iter().all(|l| width(l) <= 7));
}
#[test]
fn a_long_word_starts_its_own_line() {
assert_eq!(wrap("ab abcdefghij", 4), ["ab", "abcd", "efgh", "ij"]);
}
#[test]
fn cutting_from_the_front_keeps_the_end_and_any_colour() {
assert_eq!(truncate_start("ζζΈ/γ‘γ’.md", 8), "β¦γ‘γ’.md");
assert_eq!(truncate_start("ζζΈ/γ‘γ’.md", 6), "β¦γ’.md");
assert_eq!(truncate_start("ζζΈ/γ‘γ’.md", 5), "β¦.md");
assert_eq!(truncate_start("abc", 1), "β¦");
assert_eq!(truncate_start("abc", 0), "");
let styled = "\x1b[31mred\x1b[0m tail";
let cut = truncate_start(styled, 7);
assert_eq!(strip_ansi(&cut), "β¦d tail");
assert!(cut.starts_with("\x1b[31mβ¦") && cut.contains("\x1b[0m"), "{cut:?}");
}
#[test]
fn tabs_reach_the_next_stop() {
assert_eq!(width_with_tabs("ab\tc", 4), 5);
assert_eq!(width_with_tabs("\x1b[1m\t\x1b[0mx", 4), 5);
assert_eq!(width_with_tabs("\t", 0), 1, "a stop of 0 is taken as 1");
assert_eq!(char_width_at('a', 3, 4), 1);
}
}