Add Display & Eq trait for ObservedScreen (#422)

* Add Display&Eq trait for ObservedScreen
This commit is contained in:
Johannes Wünsche 2020-01-27 23:53:23 +01:00 committed by Alexandre Bury
parent 5afefb8645
commit 9de4d206bf

View File

@ -7,6 +7,7 @@ use std::ops::Index;
use std::ops::IndexMut; use std::ops::IndexMut;
use std::rc::Rc; use std::rc::Rc;
use std::string::ToString; use std::string::ToString;
use std::{fmt, fmt::Display, fmt::Formatter};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -84,7 +85,7 @@ impl ObservedCell {
/// Puppet backend output /// Puppet backend output
/// ///
/// Represents single frame. /// Represents single frame.
#[derive(Debug, Clone)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct ObservedScreen { pub struct ObservedScreen {
/// Size /// Size
size: Vec2, size: Vec2,
@ -92,6 +93,53 @@ pub struct ObservedScreen {
contents: Vec<Option<ObservedCell>>, contents: Vec<Option<ObservedCell>>,
} }
impl Display for ObservedScreen {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "captured piece:\n")?;
write!(f, "x")?;
for x in 0..self.size().x {
write!(f, "{}", x % 10)?;
}
write!(f, "x\n")?;
for y in 0..self.size().y {
write!(f, "{}", y % 10)?;
for x in 0..self.size().x {
let pos = Vec2::new(x, y);
let cell_op: &Option<ObservedCell> = &self[pos];
if cell_op.is_some() {
let cell = cell_op.as_ref().unwrap();
if cell.letter.is_continuation() {
write!(f, "c")?;
continue;
} else {
let letter = cell.letter.unwrap();
if letter == " " {
write!(f, " ")?;
} else {
write!(f, "{}", letter)?;
}
}
} else {
write!(f, ".")?;
}
}
write!(f, "|")?;
write!(f, "\n")?;
}
write!(f, "x")?;
for _x in 0..self.size().x {
write!(f, "-")?;
}
write!(f, "x\n")?;
Ok(())
}
}
impl ObservedScreen { impl ObservedScreen {
/// Creates empty ObservedScreen /// Creates empty ObservedScreen
pub fn new(size: Vec2) -> Self { pub fn new(size: Vec2) -> Self {
@ -136,47 +184,7 @@ impl ObservedScreen {
/// Prints the piece to stdout. /// Prints the piece to stdout.
pub fn print_stdout(&self) { pub fn print_stdout(&self) {
println!("captured piece:"); println!("{}", self)
print!("x");
for x in 0..self.size().x {
print!("{}", x % 10);
}
println!("x");
for y in 0..self.size().y {
print!("{}", y % 10);
for x in 0..self.size().x {
let pos = Vec2::new(x, y);
let cell_op: &Option<ObservedCell> = &self[pos];
if cell_op.is_some() {
let cell = cell_op.as_ref().unwrap();
if cell.letter.is_continuation() {
print!("c");
continue;
} else {
let letter = cell.letter.unwrap();
if letter == " " {
print!(" ");
} else {
print!("{}", letter);
}
}
} else {
print!(".");
}
}
print!("|");
println!();
}
print!("x");
for _x in 0..self.size().x {
print!("-");
}
println!("x");
} }
/// Returns occurences of given string pattern /// Returns occurences of given string pattern