commit cf99422b0ac3dc2cd57c0d621132b8f8c019ee2b Author: David Bittner Date: Fri Apr 26 15:01:11 2019 -0400 first commit, things are coming together diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0e3bca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..885967e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,32 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ansi-parser" +version = "0.1.0" +dependencies = [ + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nom" +version = "4.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "version_check" +version = "0.1.5" +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 version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..49338a6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ansi-parser" +version = "0.1.0" +authors = ["David Bittner "] +edition = "2018" + +[dependencies] +nom = "4.2.3" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..3c6a8a0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,144 @@ +use nom::*; + +#[derive(Debug, PartialEq)] +enum TextAttributes { + Off = 0, + Bold = 1, + Underscore = 4, + Blink = 5, + ReverseVideo = 7, + Concealed = 8 +} + +#[derive(Debug, PartialEq)] +enum Color { + Black = 30, + Red = 31, + Green = 32, + Yellow = 33, + Blue = 34, + Magenta = 35, + Cyan = 36, + White = 37 +} + +#[derive(Debug, PartialEq)] +enum AnsiSequence { + CursorPos(u32, u32), + CursorUp(u32), + CursorDown(u32), + CursorForward(u32), + CursorBackward(u32), + SaveCursorPos(u32), + RestoreCursorPos(u32), + EraseDisplay, + EraseLine, + SetGraphicsMode{ + ta: Option, + fg: Option, + bg: Option + }, + SetMode(u8), + Resetmode(u8), +} + +named!( + parse_int, + map_res!( + map_res!( + nom::digit, + std::str::from_utf8 + ), + |s: &str| s.parse::() + ) +); + +named!( + cursor_pos, + do_parse!( + x: parse_int >> + tag!(";") >> + y: parse_int >> + alt!( + tag!("H") | + tag!("f") + ) >> + (AnsiSequence::CursorPos(x, y)) + ) +); + +named!( + cursor_up, + do_parse!( + am: parse_int >> + tag!("A") >> + (AnsiSequence::CursorUp(am)) + ) +); + +named!( + cursor_down, + do_parse!( + am: parse_int >> + tag!("B") >> + (AnsiSequence::CursorDown(am)) + ) +); + +named!( + cursor_forward, + do_parse!( + am: parse_int >> + tag!("C") >> + (AnsiSequence::CursorForward(am)) + ) +); + +named!( + cursor_backward, + do_parse!( + am: parse_int >> + tag!("D") >> + (AnsiSequence::CursorBackward(am)) + ) +); + +named!( + combined, + alt!( + cursor_pos + | cursor_up + | cursor_down + | cursor_forward + | cursor_backward + ) +); + +named!( + parse_escape, + do_parse!( + tag_s!("\\\x27[") >> + seq: combined >> + (seq) + ) +); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_combined() { + let temp = "\\\x27[15;244H"; + let temp = parse_escape(temp.as_bytes()); + + assert!(temp.is_ok()); + assert_eq!(AnsiSequence::CursorPos(15, 244), temp.unwrap().1); + + let temp = "\\\x27[22D"; + let temp = parse_escape(temp.as_bytes()); + + assert!(temp.is_ok()); + assert_eq!(AnsiSequence::CursorBackward(22), temp.unwrap().1); + } +}