Skip to content

Commit

Permalink
cleanup drawing and fix some panics when window is too small
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Feb 15, 2020
1 parent 65b7c5b commit 805548d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
30 changes: 19 additions & 11 deletions src/widgets/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,31 @@ impl Widget for NetWidget<'_, '_> {
height: (inner.height / 2),
};

let top_sparkline = Rect {
x: inner.x,
y: inner.y + 3,
width: inner.width,
height: i16::max((inner.height as i16 / 2) - 3, 0) as u16,
};

let bottom_half = Rect {
x: inner.x,
y: inner.y + (inner.height / 2),
width: inner.width,
height: (inner.height / 2),
};

let top_sparkline = Rect {
x: top_half.x,
y: top_half.y + 3,
width: top_half.width,
height: i16::max(top_half.height as i16 - 3, 0) as u16,
};

let bottom_sparkline = Rect {
x: inner.x,
y: inner.y + (inner.height / 2) + 3,
width: inner.width,
height: i16::max((inner.height as i16 / 2) - 3, 0) as u16,
x: bottom_half.x,
y: bottom_half.y + 3,
width: bottom_half.width,
height: i16::max(bottom_half.height as i16 - 3, 0) as u16,
};

if inner.height < 3 {
return;
}

buf.set_string(
top_half.x + 1,
top_half.y + 1,
Expand Down Expand Up @@ -141,6 +145,10 @@ impl Widget for NetWidget<'_, '_> {
.style(self.colorscheme.net_bars)
.draw(top_sparkline, buf);

if inner.height < 5 {
return;
}

buf.set_string(
bottom_half.x + 1,
bottom_half.y + 1,
Expand Down
24 changes: 18 additions & 6 deletions src/widgets/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,18 @@ impl UpdatableWidget for ProcWidget<'_> {

impl Widget for ProcWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
self.view_height = area.height as usize - 3;
if area.height < 3 {
return;
}

let inner = Rect {
x: area.x + 1,
y: area.y + 1,
width: area.width - 2,
height: area.height - 2,
};

self.view_height = inner.height as usize - 1;

let mut procs = if self.grouping {
self.grouped_procs.values().cloned().collect()
Expand Down Expand Up @@ -322,8 +333,8 @@ impl Widget for ProcWidget<'_> {

if self.scrolled {
self.scrolled = false;
if self.selected_row > area.height as usize + self.view_offset - 4 {
self.view_offset = self.selected_row + 4 - area.height as usize;
if self.selected_row > inner.height as usize + self.view_offset - 2 {
self.view_offset = self.selected_row + 2 - inner.height as usize;
} else if self.selected_row < self.view_offset {
self.view_offset = self.selected_row;
}
Expand Down Expand Up @@ -363,9 +374,10 @@ impl Widget for ProcWidget<'_> {
.header_gap(0)
.draw(area, buf);

let cursor_y = area.y + 2 + self.selected_row as u16 - self.view_offset as u16;
if cursor_y < area.y + area.height - 1 {
for i in (area.x + 1)..(area.x + area.width - 1) {
// Draw cursor.
let cursor_y = inner.y + 1 + self.selected_row as u16 - self.view_offset as u16;
if cursor_y < inner.bottom() {
for i in inner.x..inner.right() {
let cell = buf.get_mut(i, cursor_y);
if cell.symbol != " " {
cell.set_modifier(Modifier::REVERSED);
Expand Down
19 changes: 15 additions & 4 deletions src/widgets/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,25 @@ impl Widget for TempWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
block::new(self.colorscheme, &self.title).draw(area, buf);

if area.height < 2 {
return;
}

let inner = Rect {
x: area.x + 1,
y: area.y + 1,
width: area.width - 2,
height: area.height - 2,
};

for (i, (label, data)) in self.temp_data.iter().enumerate() {
if i >= area.height as usize - 2 {
if i >= inner.height as usize {
break;
}
let y = area.y + 1 + i as u16;
buf.set_string(area.x + 1, y, label, self.colorscheme.text);
let y = inner.y + i as u16;
buf.set_string(inner.x, y, label, self.colorscheme.text);
buf.set_string(
area.x + area.width - 5,
inner.right() - 4,
y,
format!("{:2.0}°{}", data, if self.fahrenheit { "F" } else { "C" },),
if data < &self.temp_threshold {
Expand Down

0 comments on commit 805548d

Please sign in to comment.