From 3e50d269e5f49d512d8f43134740105a896332dc Mon Sep 17 00:00:00 2001 From: Joe Bellus Date: Sat, 13 Apr 2024 00:23:40 -0400 Subject: [PATCH] Fill functions now support a Color as an argument Fill functions were modified to support Into and From was implemented for Rune and Runes. This allows Passing a color to functions such as View::fill and View::fill_all. to set the background color. --- src/runes.rs | 10 ++++++++++ src/view.rs | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/runes.rs b/src/runes.rs index 8db9c36..0d6408c 100644 --- a/src/runes.rs +++ b/src/runes.rs @@ -41,6 +41,16 @@ impl From for Rune { } } +impl From for Rune { + fn from(value: Color) -> Self { + Rune { + content: Some(' '), + bg: Some(value), + ..Default::default() + } + } +} + impl Rune { pub fn new() -> Self { Self::default() diff --git a/src/view.rs b/src/view.rs index fd758ac..f6e1e74 100644 --- a/src/view.rs +++ b/src/view.rs @@ -82,11 +82,27 @@ impl View { /// Fill a region of the view with a single rune, repeating it in every /// position. - pub fn fill(&mut self, rect: R, rune: Rune) + pub fn fill(&mut self, rect: R, rune: U) where R: Into, + U: Into, { let rect = rect.into(); + let rune = rune.into(); + for y in rect.pos.y..(rect.size.height + rect.pos.y).min(self.0.len()) { + for x in rect.pos.x..(rect.size.width + rect.pos.x).min(self.0[y].len()) { + let _ = std::mem::replace(&mut self.0[y][x], rune); + } + } + } + + /// Fill the entire view context with a rune + pub fn fill_all(&mut self, rune: R) + where + R: Into, + { + let rune = rune.into(); + let rect = Rect::new((0, 0), self.size()); for y in rect.pos.y..(rect.size.height + rect.pos.y).min(self.0.len()) { for x in rect.pos.x..(rect.size.width + rect.pos.x).min(self.0[y].len()) { let _ = std::mem::replace(&mut self.0[y][x], rune);