properly handles unknown ansi escapes by adding them as plain text

This commit is contained in:
David Bittner
2019-05-02 23:04:41 -04:00
parent c1764f73ab
commit b1c17325f7
4 changed files with 30 additions and 65 deletions

56
Cargo.lock generated
View File

@@ -2,11 +2,9 @@
# It is not intended for manual editing.
[[package]]
name = "ansi-parser"
version = "0.4.0"
version = "0.5.0"
dependencies = [
"nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -23,52 +21,6 @@ dependencies = [
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-derive"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.28 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.28 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.15.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.28 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
@@ -77,10 +29,4 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39"
"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
"checksum num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "eafd0b45c5537c3ba526f79d3e75120036502bebacbb3f3220914067ce39dbf2"
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
"checksum proc-macro2 0.4.28 (registry+https://github.com/rust-lang/crates.io-index)" = "ba92c84f814b3f9a44c5cfca7d2ad77fa10710867d2bbb1b3d175ab5f47daa12"
"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
"checksum syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)" = "ec52cd796e5f01d0067225a5392e70084acc4c0013fa71d55166d38a8b307836"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"

View File

@@ -11,5 +11,3 @@ edition = "2018"
[dependencies]
nom = "4.2.3"
num-traits = "0.2"
num-derive = "0.2"

View File

@@ -207,8 +207,11 @@ impl<'a> Iterator for ParserIterator<'a> {
self.dat = &ret.0;
Some(ret.1)
}else{
let pos = self.dat[loc..].find('\x1b');
let pos = self.dat[(loc+1)..].find('\u{1b}');
if let Some(loc) = pos {
//Added to because it's based one character ahead
let loc = loc+1;
let temp = &self.dat[..loc];
self.dat = &self.dat[loc..];

View File

@@ -1,9 +1,20 @@
use super::*;
macro_rules! test_parser {
($func:expr, $string:expr) => ($func($string));
}
#[test]
fn test_invalid_escapes() {
let parse = "4;31;1;5m";
let temp = parse_escape(parse);
assert!(!temp.is_ok());
}
#[test]
fn test_graphics_mode() {
let parse = "4;31;42m";
let temp = graphics_mode(parse);
let temp = test_parser!(graphics_mode, "4;31;42m");
assert!(temp.is_ok());
assert_eq!(AnsiSequence::SetGraphicsMode(
@@ -12,8 +23,7 @@ fn test_graphics_mode() {
temp.unwrap().1
);
let parse = "4m";
let temp = graphics_mode(parse);
let temp = test_parser!(graphics_mode, "4m");
assert!(temp.is_ok());
assert_eq!(AnsiSequence::SetGraphicsMode(vec![4]),
@@ -23,9 +33,7 @@ fn test_graphics_mode() {
#[test]
fn test_set_mode() {
let parse = "=7h";
let temp = set_mode(parse);
let temp = test_parser!(set_mode, "=7h");
assert_eq!(AnsiSequence::SetMode(7), temp.unwrap().1);
}
@@ -46,3 +54,13 @@ fn test_parser_iterator() {
assert_eq!(strings.len(), 6);
}
#[test]
fn test_parser_iterator_failure() {
let parse_str = "\x1b[=25l\x1b[=7l\x1b[0m\x1b[36;1;15;2m\x1b[1m-`";
let strings: Vec<Output> = ParserIterator::new(parse_str)
.collect();
println!("{:#?}", strings);
assert_eq!(strings.len(), 6);
}