Resize will approximately work

This commit is contained in:
Thomas Forgione 2023-11-06 23:02:12 +01:00
parent b9c31c2251
commit 7e4d0e05d8
2 changed files with 84 additions and 21 deletions

View File

@ -114,7 +114,12 @@ impl<W: Write> Multiview<W> {
self.last_render = now; 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 i in 0..self.tiles.len() {
for j in 0..self.tiles[i].len() { for j in 0..self.tiles[i].len() {
if self.refresh_ui { if self.refresh_ui {
@ -212,6 +217,12 @@ impl<W: Write> Multiview<W> {
} }
} }
/// 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. /// Exits.
pub fn exit(&mut self) { pub fn exit(&mut self) {
write!(self.stdout, "{}", cursor::Show).ok(); write!(self.stdout, "{}", cursor::Show).ok();
@ -241,6 +252,7 @@ impl<W: Write> Multiview<W> {
Msg::ScrollFullUp => self.scroll_full_up(), Msg::ScrollFullUp => self.scroll_full_up(),
Msg::AddLine => self.add_line(), Msg::AddLine => self.add_line(),
Msg::AddLineAll => self.add_line_all(), Msg::AddLineAll => self.add_line_all(),
Msg::AddFinishLine(coords, success) => self.add_finish_line(coords, success),
Msg::Exit => self.exit(), Msg::Exit => self.exit(),
} }
@ -296,6 +308,9 @@ pub enum Msg {
/// Adds a line to every tile. /// Adds a line to every tile.
AddLineAll, AddLineAll,
/// Adds the finish line to the tile.
AddFinishLine((u16, u16), bool),
/// The program was asked to exit. /// The program was asked to exit.
Exit, Exit,
} }
@ -342,7 +357,7 @@ pub fn main() -> io::Result<()> {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let term_size = terminal_size()?; let mut term_size = terminal_size()?;
let col_len = tiles.len() as u16; 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)?; multiview.render(false)?;
} }

View File

@ -264,26 +264,8 @@ impl Tile {
}; };
sender.send(Msg::Stdout(coords, exit_string)).unwrap(); sender.send(Msg::Stdout(coords, exit_string)).unwrap();
let mut line = String::new();
for _ in 0..size.0 - 1 {
line.push('─');
}
sender sender
.send(Msg::Stdout( .send(Msg::AddFinishLine(coords, code == Some(0)))
coords,
format!(
"\n{}{}{}\n",
if code == Some(0) {
color::Green.fg_str()
} else {
color::Red.fg_str()
},
line,
color::Reset.fg_str()
),
))
.unwrap(); .unwrap();
}); });
@ -665,6 +647,23 @@ impl Tile {
self.start(); 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. /// Draws a line.
pub fn add_line(&mut self) { pub fn add_line(&mut self) {
let mut line = String::new(); let mut line = String::new();
@ -679,4 +678,28 @@ impl Tile {
)) ))
.unwrap(); .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();
}
} }