Switch from ansi-parser to cansi, down to a single version of nom!

This commit is contained in:
Imbris 2023-04-05 02:38:39 -04:00
parent 6ed75fa3aa
commit 9dd1b6c785
3 changed files with 68 additions and 150 deletions

117
Cargo.lock generated
View File

@ -62,7 +62,7 @@ checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [ dependencies = [
"getrandom 0.2.7", "getrandom 0.2.7",
"once_cell", "once_cell",
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -74,7 +74,7 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"getrandom 0.2.7", "getrandom 0.2.7",
"once_cell", "once_cell",
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -130,16 +130,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "ansi-parser"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcb2392079bf27198570d6af79ecbd9ec7d8f16d3ec6b60933922fdb66287127"
dependencies = [
"heapless 0.5.6",
"nom 4.2.3",
]
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.12.1" version = "0.12.1"
@ -236,18 +226,6 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "as-slice"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45403b49e3954a4b8428a0ac21a4b7afadccf92bfd96273f1a58cd4812496ae0"
dependencies = [
"generic-array 0.12.4",
"generic-array 0.13.3",
"generic-array 0.14.6",
"stable_deref_trait",
]
[[package]] [[package]]
name = "ash" name = "ash"
version = "0.32.1" version = "0.32.1"
@ -504,7 +482,7 @@ version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e"
dependencies = [ dependencies = [
"generic-array 0.14.6", "generic-array",
] ]
[[package]] [[package]]
@ -594,6 +572,12 @@ dependencies = [
"nix 0.18.0", "nix 0.18.0",
] ]
[[package]]
name = "cansi"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bdcae87153686017415ce77e48c53e6818a0a058f0e21b56640d1e944967ef8"
[[package]] [[package]]
name = "cassowary" name = "cassowary"
version = "0.3.0" version = "0.3.0"
@ -645,7 +629,7 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [ dependencies = [
"nom 7.1.3", "nom",
] ]
[[package]] [[package]]
@ -1460,7 +1444,7 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [ dependencies = [
"generic-array 0.14.6", "generic-array",
"typenum", "typenum",
] ]
@ -1731,7 +1715,7 @@ dependencies = [
"ahash 0.8.0", "ahash 0.8.0",
"lazy_static", "lazy_static",
"log", "log",
"nom 7.1.3", "nom",
] ]
[[package]] [[package]]
@ -2256,24 +2240,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "generic-array"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd"
dependencies = [
"typenum",
]
[[package]]
name = "generic-array"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f797e67af32588215eaaab8327027ee8e71b9dd0b2b26996aedf20c030fce309"
dependencies = [
"typenum",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.6" version = "0.14.6"
@ -2281,7 +2247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
dependencies = [ dependencies = [
"typenum", "typenum",
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -2518,7 +2484,7 @@ version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579160312273c954cc51bd440f059dde741029ac8daf8c84fece76cb77f62c15" checksum = "579160312273c954cc51bd440f059dde741029ac8daf8c84fece76cb77f62c15"
dependencies = [ dependencies = [
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -2652,15 +2618,6 @@ version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hash32"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "hash32" name = "hash32"
version = "0.2.1" version = "0.2.1"
@ -2708,18 +2665,6 @@ dependencies = [
"hashbrown 0.9.1", "hashbrown 0.9.1",
] ]
[[package]]
name = "heapless"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74911a68a1658cfcfb61bc0ccfbd536e3b6e906f8c2f7883ee50157e3e2184f1"
dependencies = [
"as-slice",
"generic-array 0.13.3",
"hash32 0.1.1",
"stable_deref_trait",
]
[[package]] [[package]]
name = "heapless" name = "heapless"
version = "0.7.16" version = "0.7.16"
@ -2727,7 +2672,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db04bc24a18b9ea980628ecf00e6c0264f3c1426dac36c00cb49b6fbad8b0743" checksum = "db04bc24a18b9ea980628ecf00e6c0264f3c1426dac36c00cb49b6fbad8b0743"
dependencies = [ dependencies = [
"atomic-polyfill", "atomic-polyfill",
"hash32 0.2.1", "hash32",
"rustc_version 0.4.0", "rustc_version 0.4.0",
"spin 0.9.4", "spin 0.9.4",
"stable_deref_trait", "stable_deref_trait",
@ -3867,16 +3812,6 @@ dependencies = [
"rand_xorshift", "rand_xorshift",
] ]
[[package]]
name = "nom"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
dependencies = [
"memchr",
"version_check 0.1.5",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.3" version = "7.1.3"
@ -4574,7 +4509,7 @@ dependencies = [
"proc-macro2 1.0.43", "proc-macro2 1.0.43",
"quote 1.0.21", "quote 1.0.21",
"syn 1.0.100", "syn 1.0.100",
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -4585,7 +4520,7 @@ checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [ dependencies = [
"proc-macro2 1.0.43", "proc-macro2 1.0.43",
"quote 1.0.21", "quote 1.0.21",
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -5207,7 +5142,7 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b40f1bfe5acdab44bc63e6699c28b74f75ec43afb59f3eda01e145aff86a25fa" checksum = "b40f1bfe5acdab44bc63e6699c28b74f75ec43afb59f3eda01e145aff86a25fa"
dependencies = [ dependencies = [
"heapless 0.7.16", "heapless",
"num-traits", "num-traits",
"smallvec", "smallvec",
] ]
@ -5774,7 +5709,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1e08e261d0e8f5c43123b7adf3e4ca1690d655377ac93a03b2c9d3e98de1342" checksum = "e1e08e261d0e8f5c43123b7adf3e4ca1690d655377ac93a03b2c9d3e98de1342"
dependencies = [ dependencies = [
"serde", "serde",
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -6473,7 +6408,7 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd59ecde1e694c0495c8b92bdd0f9a70e3b489cd8180fc6a18f6ea1c1026f3b2" checksum = "cd59ecde1e694c0495c8b92bdd0f9a70e3b489cd8180fc6a18f6ea1c1026f3b2"
dependencies = [ dependencies = [
"nom 7.1.3", "nom",
] ]
[[package]] [[package]]
@ -6497,7 +6432,7 @@ version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09b01702b0fd0b3fadcf98e098780badda8742d4f4a7676615cad90e8ac73622" checksum = "09b01702b0fd0b3fadcf98e098780badda8742d4f4a7676615cad90e8ac73622"
dependencies = [ dependencies = [
"version_check 0.9.4", "version_check",
] ]
[[package]] [[package]]
@ -7016,7 +6951,7 @@ dependencies = [
name = "veloren-server-cli" name = "veloren-server-cli"
version = "0.14.0" version = "0.14.0"
dependencies = [ dependencies = [
"ansi-parser", "cansi",
"clap 3.2.22", "clap 3.2.22",
"crossterm 0.25.0", "crossterm 0.25.0",
"lazy_static", "lazy_static",
@ -7209,12 +7144,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73" checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
@ -8256,7 +8185,7 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "463705a63313cd4301184381c5e8042f0a7e9b4bb63653f216311d4ae74690b7" checksum = "463705a63313cd4301184381c5e8042f0a7e9b4bb63653f216311d4ae74690b7"
dependencies = [ dependencies = [
"nom 7.1.3", "nom",
] ]
[[package]] [[package]]

View File

@ -37,7 +37,7 @@ world = { package = "veloren-world", path = "../world", optional = true }
tokio = { version = "1.14", default-features = false, features = ["rt-multi-thread"] } tokio = { version = "1.14", default-features = false, features = ["rt-multi-thread"] }
num_cpus = "1.0" num_cpus = "1.0"
ansi-parser = "0.8" cansi = "2.2.1"
clap = { version = "3.1.8", features = ["derive"] } clap = { version = "3.1.8", features = ["derive"] }
crossterm = "0.25" crossterm = "0.25"
lazy_static = "1" lazy_static = "1"

View File

@ -21,81 +21,70 @@ impl<'a> TuiLog<'a> {
impl<'a> Write for TuiLog<'a> { impl<'a> Write for TuiLog<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
use ansi_parser::{AnsiParser, AnsiSequence, Output}; // TODO: this processing can probably occur in the consumer of the log lines
// (and instead of having a TuiLog::resize the consumer can take
// ownership of the lines and manage them itself).
// Not super confident this is the ideal parser but it works for now and doesn't
// depend on an old version of nom. Alternatives to consider may include
// `vte`, `anstyle-parse`, `vt100`, or others.
use cansi::v3::categorise_text;
use tui::{ use tui::{
style::{Color, Modifier}, style::{Color, Modifier},
text::{Span, Spans}, text::{Span, Spans},
}; };
let line = String::from_utf8(buf.into()) let line =
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; core::str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let mut spans = Vec::new(); let mut spans = Vec::new();
let mut span = Span::raw("");
let mut lines = Vec::new(); let mut lines = Vec::new();
for out in line.ansi_parse() { for out in categorise_text(line) {
match out { let mut style = tui::style::Style::default();
Output::TextBlock(text) => { // NOTE: There are other values returned from cansi that we don't bother to use
// search for newlines // for now including background color, italics, blinking, etc.
for t in text.split_inclusive('\n') { style.fg = match out.fg {
span.content.to_mut().push_str(t); Some(cansi::Color::Black) => Some(Color::Black),
if t.ends_with('\n') && span.content.len() != 0 { Some(cansi::Color::Red) => Some(Color::Red),
spans.push(std::mem::replace(&mut span, Span::raw(""))); Some(cansi::Color::Green) => Some(Color::Green),
lines.push(std::mem::take(&mut spans)); Some(cansi::Color::Yellow) => Some(Color::Yellow),
} Some(cansi::Color::Blue) => Some(Color::Blue),
} Some(cansi::Color::Magenta) => Some(Color::Magenta),
Some(cansi::Color::Cyan) => Some(Color::Cyan),
Some(cansi::Color::White) => Some(Color::White),
// "Bright" versions currently not handled
Some(c) => {
warn!("Unknown color {:#?}", c);
style.fg
}, },
Output::Escape(seq) => { None => style.fg,
if span.content.len() != 0 { };
spans.push(span); match out.intensity {
Some(cansi::Intensity::Normal) | None => {},
Some(cansi::Intensity::Bold) => style.add_modifier = Modifier::BOLD,
Some(cansi::Intensity::Faint) => style.add_modifier = Modifier::DIM,
}
span = Span::raw(""); // search for newlines
} for t in out.text.split_inclusive('\n') {
if !t.is_empty() {
match seq { spans.push(Span::styled(t.to_owned(), style));
AnsiSequence::SetGraphicsMode(values) => { }
const COLOR_TABLE: [Color; 8] = [ if t.ends_with('\n') {
Color::Black, lines.push(Spans(core::mem::take(&mut spans)));
Color::Red, }
Color::Green,
Color::Yellow,
Color::Blue,
Color::Magenta,
Color::Cyan,
Color::White,
];
let mut iter = values.iter();
match iter.next().unwrap() {
0 => {},
1 => span.style.add_modifier = Modifier::BOLD,
2 => span.style.add_modifier = Modifier::DIM,
idx @ 30..=37 => {
span.style.fg = Some(COLOR_TABLE[(idx - 30) as usize])
},
_ => warn!("Unknown color {:#?}", values),
}
},
_ => warn!("Unknown sequence {:#?}", seq),
}
},
} }
} }
if span.content.len() != 0 {
spans.push(span);
}
if !spans.is_empty() { if !spans.is_empty() {
lines.push(spans); lines.push(Spans(spans));
} }
let mut lines = lines.into_iter().map(Spans).collect::<Vec<_>>();
self.inner.lock().unwrap().lines.append(&mut lines); self.inner.lock().unwrap().lines.append(&mut lines);
Ok(buf.len()) Ok(buf.len())
} }
// We can potentially use this to reduce locking frequency?
fn flush(&mut self) -> io::Result<()> { Ok(()) } fn flush(&mut self) -> io::Result<()> { Ok(()) }
} }