forked from cjbassi/ytop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.rs
110 lines (102 loc) · 3.49 KB
/
draw.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::io;
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::widgets::Widget as _;
use tui::{Frame, Terminal};
use crate::{App, Widgets};
pub fn draw<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<()> {
terminal.draw(|mut frame| {
if let Some(statusbar) = app.statusbar.as_mut() {
let chunks = Layout::default()
.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]);
} else {
let chunks = Layout::default()
.constraints(vec![Constraint::Percentage(100)])
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
}
})
}
pub fn draw_widgets<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, area: Rect) {
if widgets.temp.is_some() {
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
]
.as_ref(),
)
.split(area);
draw_top_row(frame, widgets, vertical_chunks[0]);
draw_middle_row(frame, widgets, vertical_chunks[1]);
draw_bottom_row(frame, widgets, vertical_chunks[2]);
} else {
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)].as_ref())
.split(area);
draw_top_row(frame, widgets, vertical_chunks[0]);
draw_bottom_row(frame, widgets, vertical_chunks[1]);
}
}
pub fn draw_top_row<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, area: Rect) {
if let Some(battery) = widgets.battery.as_mut() {
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]);
} else {
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(100)].as_ref())
.split(area);
widgets.cpu.render(frame, horizontal_chunks[0]);
}
}
pub fn draw_middle_row<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, area: Rect) {
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)].as_ref())
.split(area);
widgets.mem.render(frame, 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]);
}
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]);
} else {
widgets.mem.render(frame, horizontal_chunks[0]);
}
widgets.proc.render(frame, horizontal_chunks[1]);
}
pub fn draw_help_menu<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<()> {
terminal.draw(|mut frame| {
let rect = app.help_menu.get_rect(&frame.size());
app.help_menu.render(&mut frame, rect);
})
}