Skip to content

Commit d303932

Browse files
Merge #313
313: Alternate grid debug mode r=samueltardieu a=samueltardieu Co-authored-by: Samuel Tardieu <[email protected]>
2 parents ded47f8 + 08bc415 commit d303932

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/grid.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use super::utils::absdiff;
2020
/// or as a collection of absent vertices, depending on the density of
2121
/// the grid. The switch between both representations is done automatically
2222
/// when vertices are added or removed, or when the grid is resized.
23+
///
24+
/// `Grid` implements `Debug` and represents the content using `#` and `.`
25+
/// characters. Alternate block characters can be selected by using the
26+
/// alternate debug format (`{:#?}`).
2327
pub struct Grid {
2428
/// The grid width.
2529
pub width: usize,
@@ -519,9 +523,10 @@ impl<'a> Iterator for EdgesIterator<'a> {
519523

520524
impl fmt::Debug for Grid {
521525
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
526+
let (present, absent) = [('#', '.'), ('▓', '░')][f.alternate() as usize];
522527
for y in 0..self.height {
523528
for x in 0..self.width {
524-
write!(f, "{}", if self.has_vertex((x, y)) { '#' } else { '.' })?;
529+
write!(f, "{}", [absent, present][self.has_vertex((x, y)) as usize])?;
525530
}
526531
if y != self.height - 1 {
527532
writeln!(f)?;

tests/grid.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,49 @@ fn distance() {
389389
assert_eq!(g.distance((3, 2), (5, 7)), 5);
390390
assert_eq!(g.distance((5, 7), (3, 2)), 5);
391391
}
392+
393+
#[test]
394+
fn debug() {
395+
let g = [
396+
(0, 0),
397+
(1, 0),
398+
(2, 0),
399+
(3, 0),
400+
(4, 0),
401+
(0, 1),
402+
(4, 1),
403+
(0, 2),
404+
(4, 2),
405+
(0, 3),
406+
(4, 3),
407+
(0, 4),
408+
(1, 4),
409+
(2, 4),
410+
(3, 4),
411+
(4, 4),
412+
]
413+
.into_iter()
414+
.collect::<Grid>();
415+
assert_eq!(
416+
format!("{:?}", g),
417+
String::from(
418+
"\
419+
#####
420+
#...#
421+
#...#
422+
#...#
423+
#####"
424+
)
425+
);
426+
assert_eq!(
427+
format!("{:#?}", g),
428+
String::from(
429+
"\
430+
▓▓▓▓▓
431+
▓░░░▓
432+
▓░░░▓
433+
▓░░░▓
434+
▓▓▓▓▓"
435+
)
436+
);
437+
}

0 commit comments

Comments
 (0)