Skip to content

Commit

Permalink
Merge pull request cjbassi#71 from terorie/upgrade-tui
Browse files Browse the repository at this point in the history
Fix build with tui 0.9
  • Loading branch information
cjbassi committed May 10, 2020
2 parents ab3cf9b + 3685871 commit 1ad6317
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 372 deletions.
495 changes: 191 additions & 304 deletions Cargo.lock

Large diffs are not rendered by default.

35 changes: 13 additions & 22 deletions src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::widgets::Widget as _;
use tui::{Frame, Terminal};

use crate::app::{App, Widgets};
Expand All @@ -13,7 +12,7 @@ pub fn draw<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
.constraints([Constraint::Min(0), Constraint::Length(1)].as_ref())
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
statusbar.render(&mut frame, chunks[1]);
frame.render_widget(statusbar, chunks[1]);
} else {
let chunks = Layout::default()
.constraints(vec![Constraint::Percentage(100)])
Expand Down Expand Up @@ -51,19 +50,19 @@ pub fn draw_widgets<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, are
}

pub fn draw_top_row<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, area: Rect) {
if let Some(battery) = widgets.battery.as_mut() {
if let Some(battery) = widgets.battery.as_ref() {
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)].as_ref())
.split(area);
battery.render(frame, horizontal_chunks[0]);
widgets.cpu.render(frame, horizontal_chunks[1]);
frame.render_widget(battery, horizontal_chunks[0]);
frame.render_widget(&widgets.cpu, horizontal_chunks[1]);
} else {
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(100)].as_ref())
.split(area);
widgets.cpu.render(frame, horizontal_chunks[0]);
frame.render_widget(&widgets.cpu, horizontal_chunks[0]);
}
}

Expand All @@ -72,41 +71,33 @@ pub fn draw_middle_row<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets,
.direction(Direction::Horizontal)
.constraints([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)].as_ref())
.split(area);
widgets.mem.render(frame, horizontal_chunks[1]);
frame.render_widget(&widgets.mem, horizontal_chunks[1]);
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)].as_ref())
.split(horizontal_chunks[0]);
widgets
.disk
.as_mut()
.unwrap()
.render(frame, vertical_chunks[0]);
widgets
.temp
.as_mut()
.unwrap()
.render(frame, vertical_chunks[1]);
frame.render_widget(widgets.disk.as_ref().unwrap(), vertical_chunks[0]);
frame.render_widget(widgets.temp.as_ref().unwrap(), vertical_chunks[1]);
}

pub fn draw_bottom_row<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, area: Rect) {
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)].as_ref())
.split(area);
if let Some(net) = widgets.net.as_mut() {
net.render(frame, horizontal_chunks[0]);
if let Some(net) = widgets.net.as_ref() {
frame.render_widget(net, horizontal_chunks[0]);
} else {
widgets.mem.render(frame, horizontal_chunks[0]);
frame.render_widget(&widgets.mem, horizontal_chunks[0]);
}
widgets.proc.render(frame, horizontal_chunks[1]);
frame.render_widget(&mut widgets.proc, horizontal_chunks[1]);
}

pub fn draw_help_menu<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
terminal
.draw(|mut frame| {
let rect = app.help_menu.get_rect(frame.size());
app.help_menu.render(&mut frame, rect);
frame.render_widget(&app.help_menu, rect);
})
.unwrap();
}
Expand Down
22 changes: 7 additions & 15 deletions src/sparkline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use tui::widgets::{Block, Widget};
/// ```
/// # use tui::widgets::{Block, Borders, Sparkline};
/// # use tui::style::{Style, Color};
/// # fn main() {
/// Sparkline::default()
/// .block(Block::default().title("Sparkline").borders(Borders::ALL))
/// .data(&[0, 2, 3, 4, 1, 4, 10])
/// .max(5)
/// .style(Style::default().fg(Color::Red).bg(Color::White));
/// # }
/// ```
pub struct Sparkline<'a> {
/// A block to wrap the widget in
Expand Down Expand Up @@ -56,12 +54,6 @@ impl<'a> Default for Sparkline<'a> {
}

impl<'a> Sparkline<'a> {
#[allow(clippy::all)]
pub fn block(mut self, block: Block<'a>) -> Sparkline<'a> {
self.block = Some(block);
self
}

pub fn style(mut self, style: Style) -> Sparkline<'a> {
self.style = style;
self
Expand Down Expand Up @@ -89,10 +81,10 @@ impl<'a> Sparkline<'a> {
}

impl<'a> Widget for Sparkline<'a> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
fn render(self, area: Rect, buf: &mut Buffer) {
let spark_area = match self.block {
Some(ref mut b) => {
b.draw(area, buf);
Some(ref b) => {
b.render(area, buf);
b.inner(area)
}
None => area,
Expand Down Expand Up @@ -172,17 +164,17 @@ mod tests {

#[test]
fn it_does_not_panic_if_max_is_zero() {
let mut widget = Sparkline::default().data(&[0, 0, 0]);
let widget = Sparkline::default().data(&[0, 0, 0]);
let area = Rect::new(0, 0, 3, 1);
let mut buffer = Buffer::empty(area);
widget.draw(area, &mut buffer);
widget.render(area, &mut buffer);
}

#[test]
fn it_does_not_panic_if_max_is_set_to_zero() {
let mut widget = Sparkline::default().data(&[0, 1, 2]).max(0);
let widget = Sparkline::default().data(&[0, 1, 2]).max(0);
let area = Rect::new(0, 0, 3, 1);
let mut buffer = Buffer::empty(area);
widget.draw(area, &mut buffer);
widget.render(area, &mut buffer);
}
}
9 changes: 5 additions & 4 deletions src/widgets/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use battery::Manager;
use num_rational::Ratio;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use tui::symbols::Marker;
use tui::widgets::{Axis, Chart, Dataset, GraphType, Widget};

use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
Expand Down Expand Up @@ -69,8 +70,8 @@ impl UpdatableWidget for BatteryWidget<'_> {
}
}

impl Widget for BatteryWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Widget for &BatteryWidget<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let datasets: Vec<Dataset> = self
.battery_data
.values()
Expand All @@ -92,7 +93,7 @@ impl Widget for BatteryWidget<'_> {
]))
.y_axis(Axis::default().bounds([0.0, 100.0]))
.datasets(&datasets)
.draw(area, buf);
.render(area, buf);

for (i, data) in self.battery_data.iter().enumerate() {
buf.set_string(
Expand Down
9 changes: 5 additions & 4 deletions src/widgets/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use num_rational::Ratio;
use psutil::cpu;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use tui::symbols::Marker;
use tui::widgets::{Axis, Chart, Dataset, GraphType, Widget};

use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
Expand Down Expand Up @@ -115,8 +116,8 @@ impl UpdatableWidget for CpuWidget<'_> {
}
}

impl Widget for CpuWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Widget for &CpuWidget<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut datasets = Vec::new();
if self.show_average {
datasets.push(
Expand Down Expand Up @@ -151,7 +152,7 @@ impl Widget for CpuWidget<'_> {
]))
.y_axis(Axis::default().bounds([0.0, 100.0]))
.datasets(&datasets)
.draw(area, buf);
.render(area, buf);

if self.show_average {
buf.set_string(
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ fn custom_column_sizing(width: u16) -> Vec<Constraint> {
}
}

impl Widget for DiskWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Widget for &DiskWidget<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut partitions: Vec<Partition> = self.partitions.values().cloned().collect();
partitions.sort_by(|a, b| a.name.cmp(&b.name));

Expand All @@ -192,6 +192,6 @@ impl Widget for DiskWidget<'_> {
.widths(&custom_column_sizing(area.width))
.column_spacing(1)
.header_gap(0)
.draw(area, buf);
.render(area, buf);
}
}
6 changes: 3 additions & 3 deletions src/widgets/help_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ impl HelpMenu<'_> {
}
}

impl Widget for HelpMenu<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Widget for &HelpMenu<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
Paragraph::new(TEXT_VEC.iter())
.block(block::new(self.colorscheme, &self.title))
.draw(area, buf);
.render(area, buf);
}
}
9 changes: 5 additions & 4 deletions src/widgets/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use psutil::memory;
use size::Size;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use tui::symbols::Marker;
use tui::widgets::{Axis, Chart, Dataset, GraphType, Widget};

use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
Expand Down Expand Up @@ -102,8 +103,8 @@ impl UpdatableWidget for MemWidget<'_> {
}
}

impl Widget for MemWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Widget for &MemWidget<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut datasets = vec![Dataset::default()
.marker(Marker::Braille)
.graph_type(GraphType::Line)
Expand All @@ -127,7 +128,7 @@ impl Widget for MemWidget<'_> {
]))
.y_axis(Axis::default().bounds([0.0, 100.0]))
.datasets(&datasets)
.draw(area, buf);
.render(area, buf);

buf.set_string(
area.x + 3,
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ impl UpdatableWidget for NetWidget<'_, '_> {
}
}

impl Widget for NetWidget<'_, '_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
block::new(self.colorscheme, &self.title).draw(area, buf);
impl Widget for &NetWidget<'_, '_> {
fn render(self, area: Rect, buf: &mut Buffer) {
block::new(self.colorscheme, &self.title).render(area, buf);

let inner = Rect {
x: area.x + 1,
Expand Down Expand Up @@ -162,7 +162,7 @@ impl Widget for NetWidget<'_, '_> {
.show_baseline(true)
.max(*self.bytes_recv.iter().max().unwrap())
.style(self.colorscheme.net_bars)
.draw(top_sparkline, buf);
.render(top_sparkline, buf);

if inner.height < 5 {
return;
Expand Down Expand Up @@ -199,6 +199,6 @@ impl Widget for NetWidget<'_, '_> {
.show_baseline(true)
.max(*self.bytes_sent.iter().max().unwrap())
.style(self.colorscheme.net_bars)
.draw(bottom_sparkline, buf);
.render(bottom_sparkline, buf);
}
}
6 changes: 3 additions & 3 deletions src/widgets/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ impl UpdatableWidget for ProcWidget<'_> {
}
}

impl Widget for ProcWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Widget for &mut ProcWidget<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
if area.height < 3 {
return;
}
Expand Down Expand Up @@ -390,7 +390,7 @@ impl Widget for ProcWidget<'_> {
])
.column_spacing(1)
.header_gap(0)
.draw(area, buf);
.render(area, buf);

// Draw cursor.
let cursor_y = inner.y + 1 + self.selected_row as u16 - self.view_offset as u16;
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/statusbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl Statusbar<'_> {
}
}

impl Widget for Statusbar<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl<'a> Widget for &mut Statusbar<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let time = Local::now().format("%H:%M:%S").to_string();
buf.set_string(area.x + 1, area.y, &self.hostname, self.colorscheme.text);
buf.set_string(
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl UpdatableWidget for TempWidget<'_> {
}
}

impl Widget for TempWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
block::new(self.colorscheme, &self.title).draw(area, buf);
impl<'a> Widget for &TempWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
block::new(self.colorscheme, &self.title).render(area, buf);

if area.height < 2 {
return;
Expand Down

0 comments on commit 1ad6317

Please sign in to comment.