update rectangle
This commit is contained in:
parent
2074b95778
commit
30b481ee64
3 changed files with 28 additions and 16 deletions
|
@ -31,7 +31,19 @@ pub type Screen = Stdout;
|
||||||
|
|
||||||
/// X Y W H
|
/// X Y W H
|
||||||
#[derive(Copy, Clone)]
|
#[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.
|
/// UI entrypoint.
|
||||||
pub async fn run_ui(mut w: Stdout, exit: ExitSender) -> Result<()> {
|
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);
|
println!("time {}", now);
|
||||||
|
|
||||||
let (term_width, term_height) = terminal::size()?;
|
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)?;
|
// table.draw(&mut w, bounds)?;
|
||||||
tabs.draw(&mut w, bounds)?;
|
tabs.draw(&mut w, bounds)?;
|
||||||
w.flush()?;
|
w.flush()?;
|
||||||
|
|
|
@ -59,7 +59,7 @@ impl Widget for Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i, row) in self.rows.iter().enumerate() {
|
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 let Some(v) = self.selected_row {
|
||||||
if v == i as u16 {
|
if v == i as u16 {
|
||||||
queue!(
|
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() {
|
for (j, cell) in row.iter().enumerate() {
|
||||||
s += &cell;
|
s += &cell;
|
||||||
for _ in 0..columns[j] + 1 {
|
for _ in 0..columns[j] + 1 {
|
||||||
s += " ";
|
s += " ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _ in 0..(rect.2 - s.len() as u16) {
|
for _ in 0..(rect.w - s.len() as u16) {
|
||||||
s += " ";
|
s += " ";
|
||||||
}
|
}
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
let d = "\u{b7}".repeat(rect.2 as usize);
|
let d = "\u{b7}".repeat(rect.w as usize);
|
||||||
queue!(
|
queue!(
|
||||||
w,
|
w,
|
||||||
style::SetBackgroundColor(Color::Black),
|
style::SetBackgroundColor(Color::Black),
|
||||||
style::SetForegroundColor(Color::White)
|
style::SetForegroundColor(Color::White)
|
||||||
)?;
|
)?;
|
||||||
for j in self.rows.len() as u16..rect.3 {
|
for j in self.rows.len() as u16..rect.h {
|
||||||
queue!(w, cursor::MoveTo(rect.0, rect.1 + j))?;
|
queue!(w, cursor::MoveTo(rect.x, rect.y + j))?;
|
||||||
println!("{}", d);
|
println!("{}", d);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let msg = "Nothing in this table!";
|
let msg = "Nothing in this table!";
|
||||||
let x = rect.0 + (rect.2 - msg.len() as u16) / 2;
|
let x = rect.x + (rect.w - msg.len() as u16) / 2;
|
||||||
let y = rect.1 + rect.3 / 2;
|
let y = rect.y + rect.h / 2;
|
||||||
queue!(w, cursor::MoveTo(x, y))?;
|
queue!(w, cursor::MoveTo(x, y))?;
|
||||||
println!("{}", msg);
|
println!("{}", msg);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crossterm::{cursor::MoveTo, event::Event};
|
||||||
use super::{Rect, Screen, Widget};
|
use super::{Rect, Screen, Widget};
|
||||||
|
|
||||||
pub struct Tabs {
|
pub struct Tabs {
|
||||||
id: usize,
|
id_incr: usize,
|
||||||
active_id: usize,
|
active_id: usize,
|
||||||
names: Vec<(usize, String)>,
|
names: Vec<(usize, String)>,
|
||||||
contents: HashMap<usize, Box<dyn Widget>>,
|
contents: HashMap<usize, Box<dyn Widget>>,
|
||||||
|
@ -16,7 +16,7 @@ pub struct Tabs {
|
||||||
impl Tabs {
|
impl Tabs {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Tabs {
|
Tabs {
|
||||||
id: 0,
|
id_incr: 0,
|
||||||
active_id: 0,
|
active_id: 0,
|
||||||
names: Vec::new(),
|
names: Vec::new(),
|
||||||
contents: HashMap::new(),
|
contents: HashMap::new(),
|
||||||
|
@ -24,8 +24,8 @@ impl Tabs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_tab(&mut self, name: impl AsRef<str>, drawable: impl Widget + 'static) {
|
pub fn add_tab(&mut self, name: impl AsRef<str>, drawable: impl Widget + 'static) {
|
||||||
let id = self.id;
|
let id = self.id_incr;
|
||||||
self.id += 1;
|
self.id_incr += 1;
|
||||||
|
|
||||||
self.names.push((id, name.as_ref().to_owned()));
|
self.names.push((id, name.as_ref().to_owned()));
|
||||||
self.contents.insert(id, Box::new(drawable));
|
self.contents.insert(id, Box::new(drawable));
|
||||||
|
@ -36,12 +36,12 @@ impl Widget for Tabs {
|
||||||
fn update(&mut self, event: Option<Event>) {}
|
fn update(&mut self, event: Option<Event>) {}
|
||||||
|
|
||||||
fn draw(&self, w: &mut Screen, rect: Rect) -> Result<()> {
|
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() {
|
for (id, name) in self.names.iter() {
|
||||||
println!(" {} ", name);
|
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) {
|
if let Some(widget) = self.contents.get(&self.active_id) {
|
||||||
widget.draw(w, new_rect)?;
|
widget.draw(w, new_rect)?;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue