From e371fd7e5427ce0710d2c7d04267b19eacf77a9c Mon Sep 17 00:00:00 2001 From: jaewan-github <59152909+jaewan-github@users.noreply.github.com> Date: Fri, 21 Apr 2023 01:04:33 +0900 Subject: [PATCH] Main - Fix bug in exercise solution (#569) * Fix bug in the exercise solution Window may be nested, so its width should also consider border size. --- src/exercises/day-3/simple-gui.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/exercises/day-3/simple-gui.rs b/src/exercises/day-3/simple-gui.rs index 3bcd72c3..a74cd0de 100644 --- a/src/exercises/day-3/simple-gui.rs +++ b/src/exercises/day-3/simple-gui.rs @@ -70,6 +70,13 @@ impl Window { fn add_widget(&mut self, widget: Box) { self.widgets.push(widget); } + + fn inner_width(&self) -> usize { + std::cmp::max( + self.title.chars().count(), + self.widgets.iter().map(|w| w.width()).max().unwrap_or(0), + ) + } } // ANCHOR_END: setup @@ -78,10 +85,8 @@ impl Window { impl Widget for Window { fn width(&self) -> usize { // ANCHOR_END: Window-width - std::cmp::max( - self.title.chars().count(), - self.widgets.iter().map(|w| w.width()).max().unwrap_or(0), - ) + // Add 4 paddings for borders + self.inner_width() + 4 } // ANCHOR: Window-draw_into @@ -92,18 +97,18 @@ impl Widget for Window { widget.draw_into(&mut inner); } - let window_width = self.width(); + let inner_width = self.inner_width(); // TODO: after learning about error handling, you can change // draw_into to return Result<(), std::fmt::Error>. Then use // the ?-operator here instead of .unwrap(). - writeln!(buffer, "+-{:-