Alternate display for TextSurface, test Sender::draw

main
Shad Amethyst 2 years ago
parent 0a05ec8b67
commit 7d97cc354c
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -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)?;
}

@ -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());
}};
}

@ -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");
}

@ -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]}}}
Loading…
Cancel
Save