#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Style {
Text,
Dim,
Success,
Error,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Span {
pub style: Style,
pub text: String,
}
pub fn spans(line: &str) -> Vec<Span> {
let mut out: Vec<Span> = Vec::new();
let mut style = Style::Text;
let mut chars = line.chars();
while let Some(c) = chars.next() {
match c {
'\r' => out.clear(),
'\u{1b}' => match chars.next() {
Some('[') => {
let mut params = String::new();
for c in chars.by_ref() {
if ('@'..='~').contains(&c) {
if c == 'm' {
style = sgr(¶ms, style);
}
break;
}
params.push(c);
}
}
Some(']') => {
while let Some(c) = chars.next() {
if c == '\u{7}' {
break;
}
if c == '\u{1b}' {
chars.next();
break;
}
}
}
_ => {}
},
c => match out.last_mut() {
Some(last) if last.style == style => last.text.push(c),
_ => out.push(Span {
style,
text: c.to_string(),
}),
},
}
}
out
}
pub fn plain(line: &str) -> String {
spans(line).into_iter().map(|span| span.text).collect()
}
const JSON_DEPTH: usize = 32;
pub fn json_lines(line: &str) -> Option<Vec<String>> {
let text = line.trim();
if !(text.starts_with('{') || text.starts_with('['))
|| serde_json::from_str::<serde::de::IgnoredAny>(text).is_err()
{
return None;
}
let mut lines = Vec::new();
let mut current = String::new();
let mut depth = 0usize;
let mut in_string = false;
let mut escaped = false;
let mut chars = text.chars().peekable();
let break_line = |lines: &mut Vec<String>, current: &mut String, depth: usize| {
lines.push(std::mem::take(current));
current.push_str(&" ".repeat(depth));
};
while let Some(c) = chars.next() {
if in_string {
current.push(c);
if escaped {
escaped = false;
} else if c == '\\' {
escaped = true;
} else if c == '"' {
in_string = false;
}
continue;
}
match c {
'"' => {
in_string = true;
current.push(c);
}
'{' | '[' => {
while chars.next_if(|next| next.is_whitespace()).is_some() {}
current.push(c);
if chars.next_if(|next| matches!(next, '}' | ']')).is_some() {
current.push(if c == '{' { '}' } else { ']' });
} else {
depth += 1;
if depth > JSON_DEPTH {
return None;
}
break_line(&mut lines, &mut current, depth);
}
}
'}' | ']' => {
depth -= 1;
break_line(&mut lines, &mut current, depth);
current.push(c);
}
',' => {
current.push(c);
break_line(&mut lines, &mut current, depth);
}
':' => current.push_str(": "),
c if c.is_whitespace() => {}
c => current.push(c),
}
}
lines.push(current);
(lines.len() > 1).then_some(lines)
}
fn sgr(params: &str, mut style: Style) -> Style {
let mut codes = params.split(';');
while let Some(code) = codes.next() {
match code {
"" | "0" | "22" | "39" | "49" => style = Style::Text,
"31" | "91" | "41" | "101" => style = Style::Error,
"32" | "92" | "42" | "102" => style = Style::Success,
"2" | "33" | "93" | "43" | "103" | "90" => style = Style::Dim,
"38" | "48" => {
let skip = match codes.next() {
Some("5") => 1,
Some("2") => 3,
_ => 0,
};
codes.by_ref().take(skip).for_each(drop);
}
_ => {}
}
}
style
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spans_follow_the_table() {
use Style::*;
type Row<'a> = (&'a str, &'a [(Style, &'a str)], &'a str);
let table: &[Row] = &[
("plain", &[(Text, "plain")], "plain"),
(
"\x1b[31mbad\x1b[0m ok",
&[(Error, "bad"), (Text, " ok")],
"bad ok",
),
(
"\x1b[102mdone\x1b[39m!",
&[(Success, "done"), (Text, "!")],
"done!",
),
("\x1b[33mRUNNING\x1b[m", &[(Dim, "RUNNING")], "RUNNING"),
("\x1b[90ma\x1b[22mb", &[(Dim, "a"), (Text, "b")], "ab"),
("\x1b[31;1;32mx", &[(Success, "x")], "x"),
("\x1b[31m\x1b[38;5;31ma", &[(Error, "a")], "a"),
("\x1b[2m\x1b[48;2;32;31;2mb", &[(Dim, "b")], "b"),
("\x1b[31ma\x1b[41mb\x1b[0m\x1b[0m", &[(Error, "ab")], "ab"),
("a\x1b[2Kb\x1b[1;5Hc", &[(Text, "abc")], "abc"),
(
"\x1b]8;;http://x\x07link\x1b]8;;\x1b\\ end",
&[(Text, "link end")],
"link end",
),
("a\x1bMb", &[(Text, "ab")], "ab"),
("10%\r50%\r\x1b[32m100%", &[(Success, "100%")], "100%"),
("", &[], ""),
];
for (line, want, text) in table {
let want: Vec<Span> = want
.iter()
.map(|(style, text)| Span {
style: *style,
text: text.to_string(),
})
.collect();
assert_eq!(spans(line), want, "{line:?}");
assert_eq!(plain(line), *text, "{line:?}");
}
}
#[test]
fn json_lines_indent_a_json_line_in_its_order() {
let line = r#" {"b":[1, {"x":"a,b:{\"}"}],"a":{},"c":[ ]}"#;
assert_eq!(
json_lines(line).unwrap(),
[
"{",
r#" "b": ["#,
" 1,",
" {",
r#" "x": "a,b:{\"}""#,
" }",
" ],",
r#" "a": {},"#,
r#" "c": []"#,
"}",
]
);
for other in [
"INFO Checking",
"{not json",
"[]",
"\"text\"",
"{\"a\":1} tail",
&format!("{}1{}", "[".repeat(33), "]".repeat(33)),
] {
assert_eq!(json_lines(other), None, "{other:?}");
}
}
}