From 7d97cc354cc28fad5971a75b2099a4116e9b884f Mon Sep 17 00:00:00 2001 From: Adrien Burgun Date: Fri, 12 Aug 2022 13:27:49 +0200 Subject: [PATCH] :sparkles: Alternate display for TextSurface, test Sender::draw --- stackline/src/text.rs | 17 ++++++++++++----- stackline/tests/common/mod.rs | 20 ++++++++++++++++++++ stackline/tests/transmit.rs | 10 ++++++++++ stackline/tests/transmit/sender-dir.json | 1 + stackline/tests/transmit/sender-dir.txt | 7 +++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 stackline/tests/transmit.rs create mode 100644 stackline/tests/transmit/sender-dir.json create mode 100644 stackline/tests/transmit/sender-dir.txt diff --git a/stackline/src/text.rs b/stackline/src/text.rs index 602663d..caac5d6 100644 --- a/stackline/src/text.rs +++ b/stackline/src/text.rs @@ -133,16 +133,23 @@ impl std::fmt::Display for TextSurface { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use colored::Colorize; + let alternate = f.alternate(); + for y in 0..self.height { for x in 0..self.width { let ch = self.chars[y * self.width + x]; - let mut string = String::from(ch.ch).truecolor(ch.fg.red, ch.fg.green, ch.fg.blue); + if alternate { + write!(f, "{}", ch.ch)?; + } else { + let mut string = + String::from(ch.ch).truecolor(ch.fg.red, ch.fg.green, ch.fg.blue); - if let Some(bg) = ch.bg { - string = string.on_truecolor(bg.red, bg.green, bg.blue); - } + if let Some(bg) = ch.bg { + string = string.on_truecolor(bg.red, bg.green, bg.blue); + } - write!(f, "{}", string)?; + write!(f, "{}", string)?; + } } writeln!(f)?; } diff --git a/stackline/tests/common/mod.rs b/stackline/tests/common/mod.rs index d3ffe34..9898e33 100644 --- a/stackline/tests/common/mod.rs +++ b/stackline/tests/common/mod.rs @@ -63,3 +63,23 @@ macro_rules! assert_no_signal { ); }}; } + +#[macro_export] +macro_rules! assert_display_eq { + ( $world:expr, $path:expr ) => {{ + use std::fmt::Write; + + let actual = format!("{:#}", $world); + let path = $path; + let expected = std::fs::read_to_string(&path).unwrap_or_else(|err| { + panic!("Couldn't read {}: {}", path, err); + }); + + let mut actual_trimmed = String::new(); + for line in actual.lines() { + write!(&mut actual_trimmed, "{}\n", line.trim_end()).unwrap(); + } + + assert_eq!(actual_trimmed.trim_end(), expected.trim_end()); + }}; +} diff --git a/stackline/tests/transmit.rs b/stackline/tests/transmit.rs new file mode 100644 index 0000000..98d79cc --- /dev/null +++ b/stackline/tests/transmit.rs @@ -0,0 +1,10 @@ +#[allow(unused_imports)] +use stackline::prelude::*; +mod common; + +#[test] +fn test_display_sender() { + let world = load_test!("tests/transmit/sender-dir.json"); + + assert_display_eq!(world, "tests/transmit/sender-dir.txt"); +} diff --git a/stackline/tests/transmit/sender-dir.json b/stackline/tests/transmit/sender-dir.json new file mode 100644 index 0000000..38a7477 --- /dev/null +++ b/stackline/tests/transmit/sender-dir.json @@ -0,0 +1 @@ +{"panes":{"second":{"tiles":[{"cell":{"Sender":{"coordinates":["target",0,0],"path":[[6,6],[3,6],[3,3]],"length":6,"signals":[]}},"signal":null,"state":"Idle","updated":false}],"width":1,"height":1,"position":[6,6]},"target":{"tiles":[{"cell":{"Wire":"Any"},"signal":null,"state":"Idle","updated":false}],"width":1,"height":1,"position":[3,3]},"main":{"tiles":[{"cell":{"Sender":{"coordinates":["target",0,0],"path":[[0,0],[3,0],[3,3]],"length":6,"signals":[]}},"signal":null,"state":"Idle","updated":false}],"width":1,"height":1,"position":[0,0]}}} \ No newline at end of file diff --git a/stackline/tests/transmit/sender-dir.txt b/stackline/tests/transmit/sender-dir.txt new file mode 100644 index 0000000..76a5817 --- /dev/null +++ b/stackline/tests/transmit/sender-dir.txt @@ -0,0 +1,7 @@ +>--v + | + | + + + | + | + ^--<