auto-strip trailing whitespace in serialized grids

This commit is contained in:
Crispy 2025-03-29 11:53:58 +01:00
parent c2babaa674
commit c4378c85f5
3 changed files with 12 additions and 7 deletions

View file

@ -53,7 +53,7 @@ impl ResizeDeltas {
}
impl Grid {
pub fn parse(source: &str) -> Self {
pub fn from_ascii(source: &str) -> Self {
let mut rows = Vec::new();
let mut width = 0;
@ -79,13 +79,18 @@ impl Grid {
}
}
pub fn to_str(&self) -> String {
pub fn to_ascii(&self) -> String {
let mut out = String::new();
for y in 0..self.height {
for x in 0..self.width {
let tile = self.get((x, y).into()).unwrap();
out.push(tile.to_char());
}
if y > 0 {
while out.as_bytes().last() == Some(&b' ') {
out.pop();
}
}
out.push('\n');
}
out
@ -269,12 +274,12 @@ impl Grid {
impl From<String> for Grid {
fn from(value: String) -> Self {
Self::parse(&value)
Self::from_ascii(&value)
}
}
impl From<Grid> for String {
fn from(val: Grid) -> String {
val.to_str()
val.to_ascii()
}
}