Change layout properties and improve gauge and sparkline

This commit is contained in:
Florian Dehau 2016-10-13 16:27:33 +02:00
parent 275b210fd4
commit 9816ccd4e3
6 changed files with 49 additions and 21 deletions

View file

@ -130,25 +130,27 @@ fn draw(terminal: &mut Terminal, app: &App) {
let ui = Group::default() let ui = Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.alignment(Alignment::Left) .alignment(Alignment::Left)
.chunks(&[Size::Fixed(5), Size::Min(5), Size::Fixed(3)]) .chunks(&[Size::Fixed(6), Size::Min(5), Size::Fixed(3)])
.render(&terminal.area(), |chunks, tree| { .render(&terminal.area(), |chunks, tree| {
info!("{:?}", terminal.area()); info!("{:?}", terminal.area());
tree.add(Block::default().borders(border::ALL).title("Gauges").render(&chunks[0])); tree.add(Block::default().borders(border::ALL).title("Graphs").render(&chunks[0]));
tree.add(Group::default() tree.add(Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.alignment(Alignment::Left) .alignment(Alignment::Left)
.margin(1) .margin(1)
.chunks(&[Size::Fixed(1), Size::Fixed(1), Size::Fixed(1)]) .chunks(&[Size::Fixed(2), Size::Fixed(2)])
.render(&chunks[0], |chunks, tree| { .render(&chunks[0], |chunks, tree| {
tree.add(Gauge::new() tree.add(Gauge::new()
.block(*Block::default().title("Gauge:"))
.percent(app.progress) .percent(app.progress)
.render(&chunks[0])); .render(&chunks[0]));
tree.add(Sparkline::new() tree.add(Sparkline::new()
.block(*Block::default().title("Sparkline:"))
.data(&app.data) .data(&app.data)
.render(&chunks[2])); .render(&chunks[1]));
})); }));
let sizes = if app.show_episodes { let sizes = if app.show_episodes {
vec![Size::Min(20), Size::Min(20)] vec![Size::Min(20), Size::Max(40)]
} else { } else {
vec![Size::Min(20)] vec![Size::Min(20)]
}; };

View file

@ -113,7 +113,7 @@ impl Buffer {
cursor = c; cursor = c;
} }
None => { None => {
break; warn!("Failed to set all string");
} }
} }
} }

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use cassowary::{Solver, Variable, Constraint}; use cassowary::{Solver, Variable, Constraint};
use cassowary::WeightedRelation::*; use cassowary::WeightedRelation::*;
use cassowary::strength::{WEAK, MEDIUM, REQUIRED}; use cassowary::strength::{WEAK, REQUIRED};
use buffer::Buffer; use buffer::Buffer;
use widgets::WidgetType; use widgets::WidgetType;
@ -118,7 +118,7 @@ pub enum Size {
/// &Direction::Vertical, /// &Direction::Vertical,
/// &Alignment::Left, /// &Alignment::Left,
/// 0, /// 0,
/// &[Size::Fixed(5), Size::Percent(80)]); /// &[Size::Fixed(5), Size::Min(5)]);
/// } /// }
/// ///
/// ``` /// ```

View file

@ -33,6 +33,28 @@ impl<'a> Block<'a> {
self.borders = flag; self.borders = flag;
self self
} }
pub fn inner(&self, area: Rect) -> Rect {
if area.width < 2 || area.height < 2 {
return Rect::default();
}
let mut inner = area;
if self.borders.intersects(border::LEFT) {
inner.x += 1;
inner.width -= 1;
}
if self.borders.intersects(border::TOP) || self.title.is_some() {
inner.y += 1;
inner.height -= 1;
}
if self.borders.intersects(border::RIGHT) {
inner.width -= 1;
}
if self.borders.intersects(border::BOTTOM) {
inner.height -= 1;
}
inner
}
} }
impl<'a> Widget for Block<'a> { impl<'a> Widget for Block<'a> {
@ -40,10 +62,6 @@ impl<'a> Widget for Block<'a> {
let mut buf = Buffer::empty(*area); let mut buf = Buffer::empty(*area);
if self.borders == border::NONE {
return buf;
}
if area.width < 2 || area.height < 2 { if area.width < 2 || area.height < 2 {
return buf; return buf;
} }
@ -88,7 +106,12 @@ impl<'a> Widget for Block<'a> {
buf.set_symbol(area.width - 1, area.height - 1, Line::BottomRight.get()); buf.set_symbol(area.width - 1, area.height - 1, Line::BottomRight.get());
} }
if let Some(ref title) = self.title { if let Some(ref title) = self.title {
buf.set_string(1, 0, &format!(" {} ", title)); let (margin_x, string) = if self.borders.intersects(border::LEFT) {
(1, format!(" {} ", title))
} else {
(0, format!("{}", title))
};
buf.set_string(margin_x, 0, &string);
} }
buf buf
} }

View file

@ -49,22 +49,24 @@ impl<'a> Gauge<'a> {
impl<'a> Widget for Gauge<'a> { impl<'a> Widget for Gauge<'a> {
fn buffer(&self, area: &Rect) -> Buffer { fn buffer(&self, area: &Rect) -> Buffer {
let (mut buf, gauge_area) = match self.block { let (mut buf, gauge_area) = match self.block {
Some(ref b) => (b.buffer(area), area.inner(1)), Some(ref b) => (b.buffer(area), b.inner(*area)),
None => (Buffer::empty(*area), *area), None => (Buffer::empty(*area), *area),
}; };
if gauge_area.height < 1 { if gauge_area.height < 1 {
return buf; return buf;
} else { } else {
let margin = gauge_area.x - area.x; info!("{:?}{:?}", area, gauge_area);
let margin_x = gauge_area.x - area.x;
let margin_y = gauge_area.y - area.y;
let width = (gauge_area.width * self.percent) / 100; let width = (gauge_area.width * self.percent) / 100;
for i in 0..width { for i in 0..width {
buf.set_bg(margin + i, margin, self.bg); buf.set_bg(margin_x + i, margin_y, self.bg);
buf.set_fg(margin + i, margin, self.fg); buf.set_fg(margin_x + i, margin_y, self.fg);
} }
let percent_string = format!("{}%", self.percent); let percent_string = format!("{}%", self.percent);
let len = percent_string.len() as u16; let len = percent_string.len() as u16;
let middle = gauge_area.width / 2 - len / 2; let middle = gauge_area.width / 2 - len / 2;
buf.set_string(middle, margin, &percent_string); buf.set_string(middle, margin_y, &percent_string);
} }
buf buf
} }

View file

@ -41,13 +41,14 @@ impl<'a> Sparkline<'a> {
impl<'a> Widget for Sparkline<'a> { impl<'a> Widget for Sparkline<'a> {
fn buffer(&self, area: &Rect) -> Buffer { fn buffer(&self, area: &Rect) -> Buffer {
let (mut buf, spark_area) = match self.block { let (mut buf, spark_area) = match self.block {
Some(ref b) => (b.buffer(area), area.inner(1)), Some(ref b) => (b.buffer(area), b.inner(*area)),
None => (Buffer::empty(*area), *area), None => (Buffer::empty(*area), *area),
}; };
if spark_area.height < 1 { if spark_area.height < 1 {
return buf; return buf;
} else { } else {
let margin = spark_area.x - area.x; let margin_x = spark_area.x - area.x;
let margin_y = spark_area.y - area.y;
match self.data.iter().max() { match self.data.iter().max() {
Some(max_value) => { Some(max_value) => {
let max_index = min(spark_area.width as usize, self.data.len()); let max_index = min(spark_area.width as usize, self.data.len());
@ -70,7 +71,7 @@ impl<'a> Widget for Sparkline<'a> {
} }
}) })
.collect::<String>(); .collect::<String>();
buf.set_string(margin, margin, &line); buf.set_string(margin_x, margin_y, &line);
} }
None => {} None => {}
} }