From 8c1408cefefe2a2d6968e27919e502ddcdc51ff3 Mon Sep 17 00:00:00 2001 From: David Bittner Date: Sat, 4 May 2019 11:24:58 -0400 Subject: [PATCH] adding new sequences from the VT100 list --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/enums.rs | 37 ++++++++++++++++++-- src/parsers.rs | 83 +++++++++++++++++++------------------------- src/parsers/tests.rs | 1 - 5 files changed, 73 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7719c7..5203696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ # It is not intended for manual editing. [[package]] name = "ansi-parser" -version = "0.5.1" +version = "0.5.2" dependencies = [ "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 6b551e4..7c13b38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ description = "A library using nom for parsing ANSI Escape Codes" homepage = "https://gitlab.com/DavidBittner/ansi-parser" authors = ["David Bittner "] -version = "0.5.2" +version = "0.5.3" license = "MPL-2.0" edition = "2018" diff --git a/src/enums.rs b/src/enums.rs index bc2f9f0..0fedb06 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -17,7 +17,18 @@ pub enum AnsiSequence { SetMode(u8), ResetMode(u8), HideCursor, - ShowCursor + ShowCursor, + CursorToApp, + SetNewLineMode, + SetCol132, + SetSmoothScroll, + SetReverseVideo, + SetOriginRelative, + SetAutoWrap, + SetAutoRepeat, + SetInterlacing, + SetLineFeedMode, + SetCursorKeyToCursor, } use std::fmt::Display; @@ -61,7 +72,29 @@ impl Display for AnsiSequence { ShowCursor => write!(formatter, "?25h"), HideCursor - => write!(formatter, "?25l") + => write!(formatter, "?25l"), + CursorToApp + => write!(formatter, "?1h"), + SetNewLineMode + => write!(formatter, "20h"), + SetCol132 + => write!(formatter, "?3h"), + SetSmoothScroll + => write!(formatter, "?4h"), + SetReverseVideo + => write!(formatter, "?5h"), + SetOriginRelative + => write!(formatter, "?6h"), + SetAutoWrap + => write!(formatter, "?7h"), + SetAutoRepeat + => write!(formatter, "?8h"), + SetInterlacing + => write!(formatter, "?9h"), + SetLineFeedMode + => write!(formatter, "20l"), + SetCursorKeyToCursor + => write!(formatter, "?1l") } } } diff --git a/src/parsers.rs b/src/parsers.rs index 0a2e609..f73c615 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -6,6 +6,18 @@ use crate::{AnsiSequence, Output}; use std::convert::TryInto; use nom::*; +macro_rules! tag_parser { + ($sig:ident, $tag:expr, $ret:expr) => { + named!( + $sig<&str, AnsiSequence>, + do_parse!( + tag!($tag) >> + ($ret) + ) + ); + } +} + named!( parse_int<&str, u32>, map_res!( @@ -64,38 +76,6 @@ named!( ) ); -named!( - cursor_save<&str, AnsiSequence>, - do_parse!( - tag!("s") >> - (AnsiSequence::CursorSave) - ) -); - -named!( - cursor_restore<&str, AnsiSequence>, - do_parse!( - tag!("u") >> - (AnsiSequence::CursorRestore) - ) -); - -named!( - erase_display<&str, AnsiSequence>, - do_parse!( - tag!("2J") >> - (AnsiSequence::EraseDisplay) - ) -); - -named!( - erase_line<&str, AnsiSequence>, - do_parse!( - tag!("K") >> - (AnsiSequence::EraseDisplay) - ) -); - named!( graphics_mode1<&str, AnsiSequence>, do_parse!( @@ -159,21 +139,21 @@ named!( ) ); -named!( - hide_cursor<&str, AnsiSequence>, - do_parse!( - tag!("?25l") >> - (AnsiSequence::HideCursor) - ) -); - -named!( - show_cursor<&str, AnsiSequence>, - do_parse!( - tag!("?25h") >> - (AnsiSequence::ShowCursor) - ) -); +tag_parser!(cursor_save, "s", AnsiSequence::CursorSave); +tag_parser!(cursor_restore, "u", AnsiSequence::CursorRestore); +tag_parser!(erase_display, "2J", AnsiSequence::EraseDisplay); +tag_parser!(erase_line, "K", AnsiSequence::EraseLine); +tag_parser!(hide_cursor, "?25l", AnsiSequence::HideCursor); +tag_parser!(show_cursor, "?25h", AnsiSequence::ShowCursor); +tag_parser!(cursor_to_app, "?1h", AnsiSequence::CursorToApp); +tag_parser!(new_line_mode, "20h", AnsiSequence::SetNewLineMode); +tag_parser!(col_132, "?3h", AnsiSequence::SetCol132); +tag_parser!(smooth_scroll, "?4h", AnsiSequence::SetSmoothScroll); +tag_parser!(reverse_video, "?5h", AnsiSequence::SetReverseVideo); +tag_parser!(origin_relative,"?6h", AnsiSequence::SetOriginRelative); +tag_parser!(auto_wrap, "?7h", AnsiSequence::SetAutoWrap); +tag_parser!(auto_repeat, "?8h", AnsiSequence::SetAutoRepeat); +tag_parser!(interlacing, "?9h", AnsiSequence::SetInterlacing); named!( combined<&str, AnsiSequence>, @@ -192,6 +172,15 @@ named!( | reset_mode | hide_cursor | show_cursor + | cursor_to_app + | new_line_mode + | col_132 + | smooth_scroll + | reverse_video + | origin_relative + | auto_wrap + | auto_repeat + | interlacing ) ); diff --git a/src/parsers/tests.rs b/src/parsers/tests.rs index d243a8a..2d6c0df 100644 --- a/src/parsers/tests.rs +++ b/src/parsers/tests.rs @@ -61,6 +61,5 @@ fn test_parser_iterator_failure() { let strings: Vec = ParserIterator::new(parse_str) .collect(); - println!("{:#?}", strings); assert_eq!(strings.len(), 6); }