first commit, things are coming together
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
**/*.rs.bk
|
32
Cargo.lock
generated
Normal file
32
Cargo.lock
generated
Normal file
@@ -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"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "ansi-parser"
|
||||
version = "0.1.0"
|
||||
authors = ["David Bittner <bittneradave@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
nom = "4.2.3"
|
144
src/lib.rs
Normal file
144
src/lib.rs
Normal file
@@ -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<TextAttributes>,
|
||||
fg: Option<Color>,
|
||||
bg: Option<Color>
|
||||
},
|
||||
SetMode(u8),
|
||||
Resetmode(u8),
|
||||
}
|
||||
|
||||
named!(
|
||||
parse_int<u32>,
|
||||
map_res!(
|
||||
map_res!(
|
||||
nom::digit,
|
||||
std::str::from_utf8
|
||||
),
|
||||
|s: &str| s.parse::<u32>()
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
cursor_pos<AnsiSequence>,
|
||||
do_parse!(
|
||||
x: parse_int >>
|
||||
tag!(";") >>
|
||||
y: parse_int >>
|
||||
alt!(
|
||||
tag!("H") |
|
||||
tag!("f")
|
||||
) >>
|
||||
(AnsiSequence::CursorPos(x, y))
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
cursor_up<AnsiSequence>,
|
||||
do_parse!(
|
||||
am: parse_int >>
|
||||
tag!("A") >>
|
||||
(AnsiSequence::CursorUp(am))
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
cursor_down<AnsiSequence>,
|
||||
do_parse!(
|
||||
am: parse_int >>
|
||||
tag!("B") >>
|
||||
(AnsiSequence::CursorDown(am))
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
cursor_forward<AnsiSequence>,
|
||||
do_parse!(
|
||||
am: parse_int >>
|
||||
tag!("C") >>
|
||||
(AnsiSequence::CursorForward(am))
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
cursor_backward<AnsiSequence>,
|
||||
do_parse!(
|
||||
am: parse_int >>
|
||||
tag!("D") >>
|
||||
(AnsiSequence::CursorBackward(am))
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
combined<AnsiSequence>,
|
||||
alt!(
|
||||
cursor_pos
|
||||
| cursor_up
|
||||
| cursor_down
|
||||
| cursor_forward
|
||||
| cursor_backward
|
||||
)
|
||||
);
|
||||
|
||||
named!(
|
||||
parse_escape<AnsiSequence>,
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user