From 30b481ee64a0dbe66aa30cae1837f31b75bd0c51 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 27 Feb 2021 01:10:23 -0600 Subject: [PATCH] update rectangle --- src/ui/mod.rs | 16 ++++++++++++++-- src/ui/table.rs | 16 ++++++++-------- src/ui/tabs.rs | 12 ++++++------ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 2d9b921..6254102 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -31,7 +31,19 @@ pub type Screen = Stdout; /// X Y W H #[derive(Copy, Clone)] -pub struct Rect(u16, u16, u16, u16); +pub struct Rect { + x: u16, + y: u16, + w: u16, + h: u16 +} + +impl Rect { + /// Construct a new rectangle from (x, y) and (w, h) + pub fn new(x: u16, y: u16, w: u16, h: u16) -> Self { + Rect { x, y, w, h } + } +} /// UI entrypoint. pub async fn run_ui(mut w: Stdout, exit: ExitSender) -> Result<()> { @@ -58,7 +70,7 @@ pub async fn run_ui(mut w: Stdout, exit: ExitSender) -> Result<()> { println!("time {}", now); let (term_width, term_height) = terminal::size()?; - let bounds = Rect(5, 5, term_width - 10, term_height - 10); + let bounds = Rect::new(5, 5, term_width - 10, term_height - 10); // table.draw(&mut w, bounds)?; tabs.draw(&mut w, bounds)?; w.flush()?; diff --git a/src/ui/table.rs b/src/ui/table.rs index 377bb8d..14aa8a6 100644 --- a/src/ui/table.rs +++ b/src/ui/table.rs @@ -59,7 +59,7 @@ impl Widget for Table { } for (i, row) in self.rows.iter().enumerate() { - queue!(w, cursor::MoveTo(rect.0, rect.1 + i as u16))?; + queue!(w, cursor::MoveTo(rect.x, rect.y + i as u16))?; if let Some(v) = self.selected_row { if v == i as u16 { queue!( @@ -75,33 +75,33 @@ impl Widget for Table { )?; } } - let mut s = String::with_capacity(rect.2 as usize); + let mut s = String::with_capacity(rect.w as usize); for (j, cell) in row.iter().enumerate() { s += &cell; for _ in 0..columns[j] + 1 { s += " "; } } - for _ in 0..(rect.2 - s.len() as u16) { + for _ in 0..(rect.w - s.len() as u16) { s += " "; } println!("{}", s); } - let d = "\u{b7}".repeat(rect.2 as usize); + let d = "\u{b7}".repeat(rect.w as usize); queue!( w, style::SetBackgroundColor(Color::Black), style::SetForegroundColor(Color::White) )?; - for j in self.rows.len() as u16..rect.3 { - queue!(w, cursor::MoveTo(rect.0, rect.1 + j))?; + for j in self.rows.len() as u16..rect.h { + queue!(w, cursor::MoveTo(rect.x, rect.y + j))?; println!("{}", d); } } else { let msg = "Nothing in this table!"; - let x = rect.0 + (rect.2 - msg.len() as u16) / 2; - let y = rect.1 + rect.3 / 2; + let x = rect.x + (rect.w - msg.len() as u16) / 2; + let y = rect.y + rect.h / 2; queue!(w, cursor::MoveTo(x, y))?; println!("{}", msg); } diff --git a/src/ui/tabs.rs b/src/ui/tabs.rs index 2a2474f..92eae4a 100644 --- a/src/ui/tabs.rs +++ b/src/ui/tabs.rs @@ -7,7 +7,7 @@ use crossterm::{cursor::MoveTo, event::Event}; use super::{Rect, Screen, Widget}; pub struct Tabs { - id: usize, + id_incr: usize, active_id: usize, names: Vec<(usize, String)>, contents: HashMap>, @@ -16,7 +16,7 @@ pub struct Tabs { impl Tabs { pub fn new() -> Self { Tabs { - id: 0, + id_incr: 0, active_id: 0, names: Vec::new(), contents: HashMap::new(), @@ -24,8 +24,8 @@ impl Tabs { } pub fn add_tab(&mut self, name: impl AsRef, drawable: impl Widget + 'static) { - let id = self.id; - self.id += 1; + let id = self.id_incr; + self.id_incr += 1; self.names.push((id, name.as_ref().to_owned())); self.contents.insert(id, Box::new(drawable)); @@ -36,12 +36,12 @@ impl Widget for Tabs { fn update(&mut self, event: Option) {} fn draw(&self, w: &mut Screen, rect: Rect) -> Result<()> { - queue!(w, MoveTo(rect.0, rect.1))?; + queue!(w, MoveTo(rect.x, rect.y))?; for (id, name) in self.names.iter() { println!(" {} ", name); } - let new_rect = Rect(rect.0, rect.1 + 1, rect.2, rect.3 - 1); + let new_rect = Rect::new(rect.x, rect.y + 1, rect.w, rect.h - 1); if let Some(widget) = self.contents.get(&self.active_id) { widget.draw(w, new_rect)?; }