aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-03-06 14:42:01 -0500
committerJesse Luehrs <doy@tozt.net>2021-03-06 14:45:00 -0500
commitda463e1ab80793bd87f3ecb523e8a0fe137d89af (patch)
treef90f824bf2a036d88c452abbdf8b8dbe83cb78ba
parent37a1b3b1c77ad94e48bec70ef20093c1319e1dea (diff)
downloadvt100-rust-da463e1ab80793bd87f3ecb523e8a0fe137d89af.tar.gz
vt100-rust-da463e1ab80793bd87f3ecb523e8a0fe137d89af.zip
add a Write implementation for Parser
-rw-r--r--CHANGELOG.md6
-rw-r--r--src/parser.rs11
-rw-r--r--tests/write.rs60
3 files changed, 77 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 41af44a..277b879 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog
+## [Unreleased]
+
+### Added
+
+* Implementation of `std::io::Write` for `Parser`
+
## [0.9.0] - 2021-03-05
### Added
diff --git a/src/parser.rs b/src/parser.rs
index 45bef2f..26431ac 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -61,3 +61,14 @@ impl Default for Parser {
Self::new(24, 80, 0)
}
}
+
+impl std::io::Write for Parser {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.process(buf);
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+}
diff --git a/tests/write.rs b/tests/write.rs
new file mode 100644
index 0000000..0b1d5fe
--- /dev/null
+++ b/tests/write.rs
@@ -0,0 +1,60 @@
+use std::io::Write as _;
+
+#[test]
+fn write_text() {
+ let mut parser = vt100::Parser::default();
+ let input = b"foo\x1b[31m\x1b[32mb\x1b[3;7;42ma\x1b[23mr";
+ let bytes = parser.write(input).unwrap();
+ assert_eq!(bytes, input.len());
+ assert_eq!(parser.screen().contents(), "foobar");
+}
+
+#[test]
+fn cell_contents() {
+ let mut parser = vt100::Parser::default();
+ let input = b"foo\x1b[31m\x1b[32mb\x1b[3;7;42ma\x1b[23mr";
+ let bytes = parser.write(input).unwrap();
+ assert_eq!(bytes, input.len());
+ assert_eq!(parser.screen().cell(0, 0).unwrap().contents(), "f");
+ assert_eq!(parser.screen().cell(0, 1).unwrap().contents(), "o");
+ assert_eq!(parser.screen().cell(0, 2).unwrap().contents(), "o");
+ assert_eq!(parser.screen().cell(0, 3).unwrap().contents(), "b");
+ assert_eq!(parser.screen().cell(0, 4).unwrap().contents(), "a");
+ assert_eq!(parser.screen().cell(0, 5).unwrap().contents(), "r");
+ assert_eq!(parser.screen().cell(0, 6).unwrap().contents(), "");
+}
+
+#[test]
+fn cell_colors() {
+ let mut parser = vt100::Parser::default();
+ let input = b"foo\x1b[31m\x1b[32mb\x1b[3;7;42ma\x1b[23mr";
+ let bytes = parser.write(input).unwrap();
+ assert_eq!(bytes, input.len());
+
+ assert_eq!(
+ parser.screen().cell(0, 0).unwrap().fgcolor(),
+ vt100::Color::Default
+ );
+ assert_eq!(
+ parser.screen().cell(0, 3).unwrap().fgcolor(),
+ vt100::Color::Idx(2)
+ );
+ assert_eq!(
+ parser.screen().cell(0, 4).unwrap().fgcolor(),
+ vt100::Color::Idx(2)
+ );
+ assert_eq!(
+ parser.screen().cell(0, 4).unwrap().bgcolor(),
+ vt100::Color::Idx(2)
+ );
+}
+
+#[test]
+fn cell_attrs() {
+ let mut parser = vt100::Parser::default();
+ let input = b"foo\x1b[31m\x1b[32mb\x1b[3;7;42ma\x1b[23mr";
+ let bytes = parser.write(input).unwrap();
+ assert_eq!(bytes, input.len());
+
+ assert!(parser.screen().cell(0, 4).unwrap().italic());
+}