Merge branch 'master' into 'master'

Default cursor position handling

See merge request davidbittner/ansi-parser!2
This commit is contained in:
David Bittner
2020-09-11 17:35:46 +00:00
3 changed files with 73 additions and 27 deletions

2
Cargo.lock generated
View File

@@ -2,7 +2,7 @@
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] [[package]]
name = "ansi-parser" name = "ansi-parser"
version = "0.6.4" version = "0.6.5"
dependencies = [ dependencies = [
"nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View File

@@ -26,15 +26,25 @@ named!(
) )
); );
// TODO kind of ugly, would prefer to pass in the default so we could use it for
// all escapes with defaults (not just those that default to 1).
named!(
parse_def_cursor_int<&str, u32>,
map!(
nom::digit0,
|s: &str| s.parse::<u32>().unwrap_or(1)
)
);
named!( named!(
cursor_pos<&str, AnsiSequence>, cursor_pos<&str, AnsiSequence>,
do_parse!( do_parse!(
tag!("[") >> tag!("[") >>
x: parse_int >> x: parse_def_cursor_int >>
tag!(";") >> opt!(tag!(";")) >>
y: parse_int >> y: parse_def_cursor_int >>
alt!( alt!(
tag!("H") | tag!("H") |
tag!("f") tag!("f")
) >> ) >>
(AnsiSequence::CursorPos(x, y)) (AnsiSequence::CursorPos(x, y))
@@ -44,9 +54,9 @@ named!(
named!( named!(
cursor_up<&str, AnsiSequence>, cursor_up<&str, AnsiSequence>,
do_parse!( do_parse!(
tag!("[") >> tag!("[") >>
am: parse_int >> am: parse_def_cursor_int >>
tag!("A") >> tag!("A") >>
(AnsiSequence::CursorUp(am)) (AnsiSequence::CursorUp(am))
) )
); );
@@ -54,9 +64,9 @@ named!(
named!( named!(
cursor_down<&str, AnsiSequence>, cursor_down<&str, AnsiSequence>,
do_parse!( do_parse!(
tag!("[") >> tag!("[") >>
am: parse_int >> am: parse_def_cursor_int >>
tag!("B") >> tag!("B") >>
(AnsiSequence::CursorDown(am)) (AnsiSequence::CursorDown(am))
) )
); );
@@ -64,9 +74,9 @@ named!(
named!( named!(
cursor_forward<&str, AnsiSequence>, cursor_forward<&str, AnsiSequence>,
do_parse!( do_parse!(
tag!("[") >> tag!("[") >>
am: parse_int >> am: parse_def_cursor_int >>
tag!("C") >> tag!("C") >>
(AnsiSequence::CursorForward(am)) (AnsiSequence::CursorForward(am))
) )
); );
@@ -74,9 +84,9 @@ named!(
named!( named!(
cursor_backward<&str, AnsiSequence>, cursor_backward<&str, AnsiSequence>,
do_parse!( do_parse!(
tag!("[") >> tag!("[") >>
am: parse_int >> am: parse_def_cursor_int >>
tag!("D") >> tag!("D") >>
(AnsiSequence::CursorBackward(am)) (AnsiSequence::CursorBackward(am))
) )
); );
@@ -217,7 +227,7 @@ named!(
| cursor_up | cursor_up
| cursor_down | cursor_down
| cursor_forward | cursor_forward
| cursor_backward | cursor_backward
| cursor_save | cursor_save
| cursor_restore | cursor_restore
| erase_display | erase_display
@@ -272,4 +282,3 @@ named!(
(Output::Escape(seq)) (Output::Escape(seq))
) )
); );

View File

@@ -21,13 +21,37 @@ macro_rules! test_parser {
} }
} }
test_parser!(cursor_pos, "\u{1b}[10;5H"); macro_rules! test_def_val_parser {
test_parser!(cursor_up, "\u{1b}[5A"); ($name:ident, $string:expr) => {
test_parser!(cursor_down, "\u{1b}[5B"); #[test]
test_parser!(cursor_forward, "\u{1b}[5C"); fn $name() {
test_parser!(cursor_backward,"\u{1b}[5D"); let mut buff = String::new();
test_parser!(cursor_save, "\u{1b}[s"); let ret = parse_escape($string);
test_parser!(cursor_restore, "\u{1b}[u");
assert!(ret.is_ok());
let ret = ret.unwrap().1;
write!(&mut buff, "{}", ret)
.unwrap();
let ret2 = parse_escape(&buff);
assert!(ret2.is_ok());
let ret2 = ret2.unwrap().1;
assert_eq!(ret, ret2);
}
}
}
test_def_val_parser!(cursor_pos_default, "\u{1b}[H");
test_def_val_parser!(cursor_pos, "\u{1b}[10;5H");
test_def_val_parser!(cursor_up_default, "\u{1b}[A");
test_def_val_parser!(cursor_up, "\u{1b}[5A");
test_def_val_parser!(cursor_down, "\u{1b}[5B");
test_def_val_parser!(cursor_forward, "\u{1b}[5C");
test_def_val_parser!(cursor_backward, "\u{1b}[5D");
test_parser!(cursor_save, "\u{1b}[s");
test_parser!(cursor_restore, "\u{1b}[u");
test_parser!(erase_display, "\u{1b}[2J"); test_parser!(erase_display, "\u{1b}[2J");
test_parser!(erase_line, "\u{1b}[K"); test_parser!(erase_line, "\u{1b}[K");
@@ -96,3 +120,16 @@ fn test_parser_iterator_failure() {
assert_eq!(strings.len(), 6); assert_eq!(strings.len(), 6);
} }
#[test]
fn test_default_value() {
let strings: Vec<_> = "\x1b[H\x1b[123456H\x1b[;123456H\x1b[7asd;1234H\x1b[a;sd7H"
.ansi_parse()
.collect();
assert_eq!(strings.len(), 5);
assert_eq!(strings[0], Output::Escape(AnsiSequence::CursorPos(1,1)));
assert_eq!(strings[1], Output::Escape(AnsiSequence::CursorPos(123456,1)));
assert_eq!(strings[2], Output::Escape(AnsiSequence::CursorPos(1,123456)));
assert_eq!(strings[3], Output::TextBlock("\x1b[7asd;1234H"));
assert_eq!(strings[4], Output::TextBlock("\x1b[a;sd7H"));
}