diff --git a/src/enums.rs b/src/enums.rs index 556ceb2..62f9434 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests; -use heapless::{Vec, consts::U5}; +use heapless::{consts::U5, Vec}; ///The following are the implemented ANSI escape sequences. More to be added. #[derive(Debug, PartialEq, Clone)] @@ -64,114 +64,66 @@ impl Display for AnsiSequence { use AnsiSequence::*; match self { - Escape => - write!(formatter, "\u{1b}"), - CursorPos(line, col) - => write!(formatter, "[{};{}H", line, col), - CursorUp(amt) - => write!(formatter, "[{}A", amt), - CursorDown(amt) - => write!(formatter, "[{}B", amt), - CursorForward(amt) - => write!(formatter, "[{}C", amt), - CursorBackward(amt) - => write!(formatter, "[{}D", amt), - CursorSave - => write!(formatter, "[s"), - CursorRestore - => write!(formatter, "[u"), - EraseDisplay - => write!(formatter, "[2J"), - EraseLine - => write!(formatter, "[K"), - SetGraphicsMode(vec) - => { - match vec.len() { - 0 => write!(formatter, "[m"), - 1 => write!(formatter, "[{}m", vec[0]), - 2 => write!(formatter, "[{};{}m", vec[0], vec[1]), - 3 => write!(formatter, "[{};{};{}m", vec[0], vec[1], vec[2]), - 5 => write!(formatter, "[{};{};{};{};{}m", vec[0], vec[1], - vec[2], vec[3], vec[4]), - _ => unreachable!() - } - }, - SetMode(mode) - => write!(formatter, "[={}h", mode), - ResetMode(mode) - => write!(formatter, "[={}l", mode), - ShowCursor - => write!(formatter, "[?25h"), - HideCursor - => 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"), - SetVT52 - => write!(formatter, "[?2l"), - SetCol80 - => write!(formatter, "[?3l"), - SetJumpScrolling - => write!(formatter, "[?4l"), - SetNormalVideo - => write!(formatter, "[?5l"), - SetOriginAbsolute - => write!(formatter, "[?6l"), - ResetAutoWrap - => write!(formatter, "[?7l"), - ResetAutoRepeat - => write!(formatter, "[?8l"), - ResetInterlacing - => write!(formatter, "[?9l"), - SetAlternateKeypad - => write!(formatter, "="), - SetNumericKeypad - => write!(formatter, ">"), - SetUKG0 - => write!(formatter, "(A"), - SetUKG1 - => write!(formatter, ")A"), - SetUSG0 - => write!(formatter, "(B"), - SetUSG1 - => write!(formatter, ")B"), - SetG0SpecialChars - => write!(formatter, "(0"), - SetG1SpecialChars - => write!(formatter, ")0"), - SetG0AlternateChar - => write!(formatter, "(1"), - SetG1AlternateChar - => write!(formatter, ")1"), - SetG0AltAndSpecialGraph - => write!(formatter, "(2"), - SetG1AltAndSpecialGraph - => write!(formatter, ")2"), - SetSingleShift2 - => write!(formatter, "N"), - SetSingleShift3 - => write!(formatter, "O"), - SetTopAndBottom(x, y) - => write!(formatter, "{};{}r", x, y) + Escape => write!(formatter, "\u{1b}"), + CursorPos(line, col) => write!(formatter, "[{};{}H", line, col), + CursorUp(amt) => write!(formatter, "[{}A", amt), + CursorDown(amt) => write!(formatter, "[{}B", amt), + CursorForward(amt) => write!(formatter, "[{}C", amt), + CursorBackward(amt) => write!(formatter, "[{}D", amt), + CursorSave => write!(formatter, "[s"), + CursorRestore => write!(formatter, "[u"), + EraseDisplay => write!(formatter, "[2J"), + EraseLine => write!(formatter, "[K"), + SetGraphicsMode(vec) => match vec.len() { + 0 => write!(formatter, "[m"), + 1 => write!(formatter, "[{}m", vec[0]), + 2 => write!(formatter, "[{};{}m", vec[0], vec[1]), + 3 => write!(formatter, "[{};{};{}m", vec[0], vec[1], vec[2]), + 5 => write!( + formatter, + "[{};{};{};{};{}m", + vec[0], vec[1], vec[2], vec[3], vec[4] + ), + _ => unreachable!(), + }, + SetMode(mode) => write!(formatter, "[={}h", mode), + ResetMode(mode) => write!(formatter, "[={}l", mode), + ShowCursor => write!(formatter, "[?25h"), + HideCursor => 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"), + SetVT52 => write!(formatter, "[?2l"), + SetCol80 => write!(formatter, "[?3l"), + SetJumpScrolling => write!(formatter, "[?4l"), + SetNormalVideo => write!(formatter, "[?5l"), + SetOriginAbsolute => write!(formatter, "[?6l"), + ResetAutoWrap => write!(formatter, "[?7l"), + ResetAutoRepeat => write!(formatter, "[?8l"), + ResetInterlacing => write!(formatter, "[?9l"), + SetAlternateKeypad => write!(formatter, "="), + SetNumericKeypad => write!(formatter, ">"), + SetUKG0 => write!(formatter, "(A"), + SetUKG1 => write!(formatter, ")A"), + SetUSG0 => write!(formatter, "(B"), + SetUSG1 => write!(formatter, ")B"), + SetG0SpecialChars => write!(formatter, "(0"), + SetG1SpecialChars => write!(formatter, ")0"), + SetG0AlternateChar => write!(formatter, "(1"), + SetG1AlternateChar => write!(formatter, ")1"), + SetG0AltAndSpecialGraph => write!(formatter, "(2"), + SetG1AltAndSpecialGraph => write!(formatter, ")2"), + SetSingleShift2 => write!(formatter, "N"), + SetSingleShift3 => write!(formatter, "O"), + SetTopAndBottom(x, y) => write!(formatter, "{};{}r", x, y), } } } @@ -182,7 +134,7 @@ impl Display for AnsiSequence { #[derive(Debug, PartialEq)] pub enum Output<'a> { TextBlock(&'a str), - Escape(AnsiSequence) + Escape(AnsiSequence), } impl<'a> Display for Output<'a> { @@ -190,7 +142,7 @@ impl<'a> Display for Output<'a> { use Output::*; match self { TextBlock(txt) => write!(formatter, "{}", txt), - Escape(seq) => write!(formatter, "{}", seq) + Escape(seq) => write!(formatter, "{}", seq), } } } diff --git a/src/enums/tests.rs b/src/enums/tests.rs index de7dc85..3eec91a 100644 --- a/src/enums/tests.rs +++ b/src/enums/tests.rs @@ -7,8 +7,7 @@ fn test_cursor_pos() { let pos = AnsiSequence::CursorPos(5, 20); let mut buff = String::new(); - write!(&mut buff, "{}", pos) - .expect("failed to write"); + write!(&mut buff, "{}", pos).expect("failed to write"); assert_eq!(buff, "\x1b[5;20H"); } diff --git a/src/lib.rs b/src/lib.rs index aad31f2..d43f02a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![recursion_limit="256"] +#![recursion_limit = "256"] #![cfg_attr(not(any(feature = "std", test)), no_std)] mod enums; @@ -18,7 +18,6 @@ mod traits; /// This is done through a pulldown type parser, where an iterator is exposed. This essentially /// turns all of the ANSI sequences into enums and splits the string at every location that there /// was an ANSI Sequence. - pub use enums::*; -pub use traits::*; pub use parsers::parse_escape; +pub use traits::*; diff --git a/src/parsers.rs b/src/parsers.rs index 7880d50..b0bf613 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -190,7 +190,6 @@ named!( ) ); - named!( graphics_mode<&str, AnsiSequence>, alt!( @@ -236,31 +235,31 @@ named!( ) ); -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!(set_new_line_mode, "[20h", AnsiSequence::SetNewLineMode); -tag_parser!(set_col_132, "[?3h", AnsiSequence::SetCol132); -tag_parser!(set_smooth_scroll, "[?4h", AnsiSequence::SetSmoothScroll); -tag_parser!(set_reverse_video, "[?5h", AnsiSequence::SetReverseVideo); -tag_parser!(set_origin_rel, "[?6h", AnsiSequence::SetOriginRelative); -tag_parser!(set_auto_wrap, "[?7h", AnsiSequence::SetAutoWrap); -tag_parser!(set_auto_repeat, "[?8h", AnsiSequence::SetAutoRepeat); -tag_parser!(set_interlacing, "[?9h", AnsiSequence::SetInterlacing); -tag_parser!(set_linefeed, "[20l", AnsiSequence::SetLineFeedMode); -tag_parser!(set_cursorkey, "[?1l", AnsiSequence::SetCursorKeyToCursor); -tag_parser!(set_vt52, "[?2l", AnsiSequence::SetVT52); -tag_parser!(set_col80, "[?3l", AnsiSequence::SetCol80); -tag_parser!(set_jump_scroll, "[?4l", AnsiSequence::SetJumpScrolling); -tag_parser!(set_normal_video, "[?5l", AnsiSequence::SetNormalVideo); -tag_parser!(set_origin_abs, "[?6l", AnsiSequence::SetOriginAbsolute); -tag_parser!(reset_auto_wrap, "[?7l", AnsiSequence::ResetAutoWrap); -tag_parser!(reset_auto_repeat, "[?8l", AnsiSequence::ResetAutoRepeat); -tag_parser!(reset_interlacing, "[?9l", AnsiSequence::ResetInterlacing); +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!(set_new_line_mode, "[20h", AnsiSequence::SetNewLineMode); +tag_parser!(set_col_132, "[?3h", AnsiSequence::SetCol132); +tag_parser!(set_smooth_scroll, "[?4h", AnsiSequence::SetSmoothScroll); +tag_parser!(set_reverse_video, "[?5h", AnsiSequence::SetReverseVideo); +tag_parser!(set_origin_rel, "[?6h", AnsiSequence::SetOriginRelative); +tag_parser!(set_auto_wrap, "[?7h", AnsiSequence::SetAutoWrap); +tag_parser!(set_auto_repeat, "[?8h", AnsiSequence::SetAutoRepeat); +tag_parser!(set_interlacing, "[?9h", AnsiSequence::SetInterlacing); +tag_parser!(set_linefeed, "[20l", AnsiSequence::SetLineFeedMode); +tag_parser!(set_cursorkey, "[?1l", AnsiSequence::SetCursorKeyToCursor); +tag_parser!(set_vt52, "[?2l", AnsiSequence::SetVT52); +tag_parser!(set_col80, "[?3l", AnsiSequence::SetCol80); +tag_parser!(set_jump_scroll, "[?4l", AnsiSequence::SetJumpScrolling); +tag_parser!(set_normal_video, "[?5l", AnsiSequence::SetNormalVideo); +tag_parser!(set_origin_abs, "[?6l", AnsiSequence::SetOriginAbsolute); +tag_parser!(reset_auto_wrap, "[?7l", AnsiSequence::ResetAutoWrap); +tag_parser!(reset_auto_repeat, "[?8l", AnsiSequence::ResetAutoRepeat); +tag_parser!(reset_interlacing, "[?9l", AnsiSequence::ResetInterlacing); tag_parser!(set_alternate_keypad, "=", AnsiSequence::SetAlternateKeypad); tag_parser!(set_numeric_keypad, ">", AnsiSequence::SetNumericKeypad); @@ -268,8 +267,8 @@ tag_parser!(set_uk_g0, "(A", AnsiSequence::SetUKG0); tag_parser!(set_uk_g1, ")A", AnsiSequence::SetUKG1); tag_parser!(set_us_g0, "(B", AnsiSequence::SetUSG0); tag_parser!(set_us_g1, ")B", AnsiSequence::SetUSG1); -tag_parser!(set_g0_special, "(0", AnsiSequence::SetG0SpecialChars); -tag_parser!(set_g1_special, ")0", AnsiSequence::SetG1SpecialChars); +tag_parser!(set_g0_special, "(0", AnsiSequence::SetG0SpecialChars); +tag_parser!(set_g1_special, ")0", AnsiSequence::SetG1SpecialChars); tag_parser!(set_g0_alternate, "(1", AnsiSequence::SetG0AlternateChar); tag_parser!(set_g1_alternate, ")1", AnsiSequence::SetG1AlternateChar); tag_parser!(set_g0_graph, "(2", AnsiSequence::SetG0AltAndSpecialGraph); diff --git a/src/parsers/tests.rs b/src/parsers/tests.rs index a625d50..720811d 100644 --- a/src/parsers/tests.rs +++ b/src/parsers/tests.rs @@ -16,12 +16,11 @@ macro_rules! test_parser { assert!(ret.is_ok()); let ret = ret.unwrap().1; - write!(&mut buff, "{}", ret) - .unwrap(); + write!(&mut buff, "{}", ret).unwrap(); assert_eq!(buff, $string); } - } + }; } macro_rules! test_def_val_parser { @@ -34,8 +33,7 @@ macro_rules! test_def_val_parser { assert!(ret.is_ok()); let ret = ret.unwrap().1; - write!(&mut buff, "{}", ret) - .unwrap(); + write!(&mut buff, "{}", ret).unwrap(); let ret2 = parse_escape(&buff); assert!(ret2.is_ok()); @@ -43,21 +41,21 @@ macro_rules! test_def_val_parser { 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_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_line, "\u{1b}[K"); +test_parser!(erase_line, "\u{1b}[K"); test_parser!(set_video_mode_a, "\u{1b}[4m"); test_parser!(set_video_mode_b, "\u{1b}[4;42m"); @@ -65,30 +63,30 @@ test_parser!(set_video_mode_c, "\u{1b}[4;31;42m"); test_parser!(set_video_mode_d, "\u{1b}[4;31;42;42;42m"); test_parser!(reset_mode, "\u{1b}[=13l"); -test_parser!(set_mode, "\u{1b}[=7h"); +test_parser!(set_mode, "\u{1b}[=7h"); -test_parser!(show_cursor, "\u{1b}[?25h"); -test_parser!(hide_cursor, "\u{1b}[?25l"); +test_parser!(show_cursor, "\u{1b}[?25h"); +test_parser!(hide_cursor, "\u{1b}[?25l"); test_parser!(cursor_to_app, "\u{1b}[?1h"); -test_parser!(set_newline_mode, "\u{1b}[20h"); -test_parser!(set_column_132, "\u{1b}[?3h"); +test_parser!(set_newline_mode, "\u{1b}[20h"); +test_parser!(set_column_132, "\u{1b}[?3h"); test_parser!(set_smooth_scroll, "\u{1b}[?4h"); test_parser!(set_reverse_video, "\u{1b}[?5h"); -test_parser!(set_origin_rel, "\u{1b}[?6h"); -test_parser!(set_auto_wrap, "\u{1b}[?7h"); -test_parser!(set_auto_repeat, "\u{1b}[?8h"); -test_parser!(set_interlacing, "\u{1b}[?9h"); +test_parser!(set_origin_rel, "\u{1b}[?6h"); +test_parser!(set_auto_wrap, "\u{1b}[?7h"); +test_parser!(set_auto_repeat, "\u{1b}[?8h"); +test_parser!(set_interlacing, "\u{1b}[?9h"); test_parser!(set_cursor_key_to_cursor, "\u{1b}[?1l"); -test_parser!(set_linefeed, "\u{1b}[20l"); -test_parser!(set_vt52, "\u{1b}[?2l"); -test_parser!(set_col80, "\u{1b}[?3l"); -test_parser!(set_jump_scroll, "\u{1b}[?4l"); -test_parser!(set_normal_video, "\u{1b}[?5l"); -test_parser!(set_origin_abs, "\u{1b}[?6l"); -test_parser!(reset_auto_wrap, "\u{1b}[?7l"); +test_parser!(set_linefeed, "\u{1b}[20l"); +test_parser!(set_vt52, "\u{1b}[?2l"); +test_parser!(set_col80, "\u{1b}[?3l"); +test_parser!(set_jump_scroll, "\u{1b}[?4l"); +test_parser!(set_normal_video, "\u{1b}[?5l"); +test_parser!(set_origin_abs, "\u{1b}[?6l"); +test_parser!(reset_auto_wrap, "\u{1b}[?7l"); test_parser!(reset_auto_repeat, "\u{1b}[?8l"); test_parser!(reset_interlacing, "\u{1b}[?9l"); @@ -109,20 +107,20 @@ test_parser!(set_single_shift3, "\u{1b}O"); #[test] fn test_parser_iterator() { - let strings: Vec<_> = "\x1b[=25l\x1b[=7l\x1b[0m\x1b[36m\x1b[1m-`" + let count = "\x1b[=25l\x1b[=7l\x1b[0m\x1b[36m\x1b[1m-`" .ansi_parse() - .collect(); + .count(); - assert_eq!(strings.len(), 6); + assert_eq!(count, 6); } #[test] fn test_parser_iterator_failure() { - let strings: Vec<_> = "\x1b[=25l\x1b[=7l\x1b[0m\x1b[36;1;15;2m\x1b[1m-`" + let count = "\x1b[=25l\x1b[=7l\x1b[0m\x1b[36;1;15;2m\x1b[1m-`" .ansi_parse() - .collect(); + .count(); - assert_eq!(strings.len(), 6); + assert_eq!(count, 6); } #[test] @@ -131,9 +129,15 @@ fn test_default_value() { .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[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")); } diff --git a/src/traits.rs b/src/traits.rs index 9d5b35a..31a8700 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,4 +1,4 @@ -use crate::enums::{Output}; +use crate::enums::Output; use crate::parsers::parse_escape; pub trait AnsiParser { @@ -7,18 +7,14 @@ pub trait AnsiParser { impl AnsiParser for str { fn ansi_parse(&self) -> AnsiParseIterator<'_> { - AnsiParseIterator { - dat: self - } + AnsiParseIterator { dat: self } } } #[cfg(any(feature = "std", test))] impl AnsiParser for String { fn ansi_parse(&self) -> AnsiParseIterator<'_> { - AnsiParseIterator { - dat: self - } + AnsiParseIterator { dat: self } } } @@ -28,9 +24,9 @@ pub struct AnsiParseIterator<'a> { impl<'a> Iterator for AnsiParseIterator<'a> { type Item = Output<'a>; - + fn next(&mut self) -> Option { - if self.dat == "" { + if self.dat.is_empty() { return None; } @@ -40,33 +36,32 @@ impl<'a> Iterator for AnsiParseIterator<'a> { let res = parse_escape(&self.dat[loc..]); if let Ok(ret) = res { - self.dat = &ret.0; + self.dat = ret.0; Some(Output::Escape(ret.1)) - }else{ - let pos = self.dat[(loc+1)..].find('\u{1b}'); + } else { + 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 loc = loc + 1; let temp = &self.dat[..loc]; self.dat = &self.dat[loc..]; Some(Output::TextBlock(temp)) - }else{ + } else { let temp = self.dat; self.dat = ""; Some(Output::TextBlock(temp)) } } - - }else { + } else { let temp = &self.dat[..loc]; self.dat = &self.dat[loc..]; - Some(Output::TextBlock(&temp)) + Some(Output::TextBlock(temp)) } - }else{ + } else { let temp = self.dat; self.dat = ""; Some(Output::TextBlock(temp))