From 7e4d0e05d883b3a1ce75e01c2287b9a60d15aae0 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 6 Nov 2023 23:02:12 +0100 Subject: [PATCH] Resize will approximately work --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++-- src/tile.rs | 61 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e19fdea..d293b10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,7 +114,12 @@ impl Multiview { self.last_render = now; - let mut buffer = vec![]; + let mut buffer = if self.refresh_ui { + vec![format!("{}", clear::All)] + } else { + vec![] + }; + for i in 0..self.tiles.len() { for j in 0..self.tiles[i].len() { if self.refresh_ui { @@ -212,6 +217,12 @@ impl Multiview { } } + /// Adds a finish line to the specified tile. + pub fn add_finish_line(&mut self, coords: (u16, u16), success: bool) { + let tile = self.tile_mut(coords); + tile.add_finish_line(success); + } + /// Exits. pub fn exit(&mut self) { write!(self.stdout, "{}", cursor::Show).ok(); @@ -241,6 +252,7 @@ impl Multiview { Msg::ScrollFullUp => self.scroll_full_up(), Msg::AddLine => self.add_line(), Msg::AddLineAll => self.add_line_all(), + Msg::AddFinishLine(coords, success) => self.add_finish_line(coords, success), Msg::Exit => self.exit(), } @@ -296,6 +308,9 @@ pub enum Msg { /// Adds a line to every tile. AddLineAll, + /// Adds the finish line to the tile. + AddFinishLine((u16, u16), bool), + /// The program was asked to exit. Exit, } @@ -342,7 +357,7 @@ pub fn main() -> io::Result<()> { }) .collect::>(); - let term_size = terminal_size()?; + let mut term_size = terminal_size()?; let col_len = tiles.len() as u16; @@ -424,6 +439,31 @@ pub fn main() -> io::Result<()> { } } + let new_term_size = terminal_size()?; + + if term_size != new_term_size { + term_size = new_term_size; + + for (i, row) in multiview.tiles.iter_mut().enumerate() { + let row_len = row.len() as u16; + + let tile_size = if is_row_major { + (term_size.0 / row_len, term_size.1 / col_len) + } else { + (term_size.0 / col_len, term_size.1 / row_len) + }; + + for (j, tile) in row.iter_mut().enumerate() { + let (p_i, p_j) = if is_row_major { (i, j) } else { (j, i) }; + tile.reposition((p_j as u16 * tile_size.0 + 1, p_i as u16 * tile_size.1 + 1)); + tile.resize(tile_size); + } + } + + multiview.refresh_tiles = true; + multiview.refresh_ui = true; + } + multiview.render(false)?; } diff --git a/src/tile.rs b/src/tile.rs index 96eebfe..3a77160 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -264,26 +264,8 @@ impl Tile { }; sender.send(Msg::Stdout(coords, exit_string)).unwrap(); - - let mut line = String::new(); - for _ in 0..size.0 - 1 { - line.push('─'); - } - sender - .send(Msg::Stdout( - coords, - format!( - "\n{}{}{}\n", - if code == Some(0) { - color::Green.fg_str() - } else { - color::Red.fg_str() - }, - line, - color::Reset.fg_str() - ), - )) + .send(Msg::AddFinishLine(coords, code == Some(0))) .unwrap(); }); @@ -665,6 +647,23 @@ impl Tile { self.start(); } + /// Repositions the tile. + pub fn reposition(&mut self, (i, j): (u16, u16)) { + self.outer_position = (i, j); + self.inner_position = (i + 2, j + 3); + } + + /// Resizes the tile. + pub fn resize(&mut self, (w, h): (u16, u16)) { + self.outer_size = (w, h); + self.inner_size = (w - 4, h - 5); + + let old_stdout = std::mem::replace(&mut self.stdout, vec![String::new()]); + for s in old_stdout { + self.push_stdout(s); + } + } + /// Draws a line. pub fn add_line(&mut self) { let mut line = String::new(); @@ -679,4 +678,28 @@ impl Tile { )) .unwrap(); } + + /// Draws a finish line, green if success or red if failure. + pub fn add_finish_line(&mut self, success: bool) { + let mut line = String::new(); + for _ in 0..self.inner_size.0 - 1 { + line.push('─'); + } + + self.sender + .send(Msg::Stdout( + self.coords, + format!( + "\n{}{}{}\n", + color::Reset.fg_str(), + if success { + color::Green.fg_str() + } else { + color::Red.fg_str() + }, + line + ), + )) + .unwrap(); + } }