Fill functions now support a Color as an argument

Fill functions were modified to support Into<Rune> and
From<Color> 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.
This commit is contained in:
Joe Bellus 2024-04-13 00:23:40 -04:00
parent 44baee56b0
commit 3e50d269e5
2 changed files with 27 additions and 1 deletions

View File

@ -41,6 +41,16 @@ impl From<char> for Rune {
}
}
impl From<Color> for Rune {
fn from(value: Color) -> Self {
Rune {
content: Some(' '),
bg: Some(value),
..Default::default()
}
}
}
impl Rune {
pub fn new() -> Self {
Self::default()

View File

@ -82,11 +82,27 @@ impl View {
/// Fill a region of the view with a single rune, repeating it in every
/// position.
pub fn fill<R>(&mut self, rect: R, rune: Rune)
pub fn fill<R, U>(&mut self, rect: R, rune: U)
where
R: Into<Rect>,
U: Into<Rune>,
{
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<R>(&mut self, rune: R)
where
R: Into<Rune>,
{
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);