forked from cjbassi/ytop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.rs
149 lines (138 loc) · 4.98 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::{Frame, Terminal};
use crate::app::{App, Widgets};
pub fn draw<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
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]);
frame.render_widget(statusbar, chunks[1]);
} else {
let chunks = Layout::default()
.constraints(vec![Constraint::Percentage(100)])
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
}
})
.unwrap();
}
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_ref() {
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)].as_ref())
.split(area);
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);
frame.render_widget(&widgets.cpu, 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);
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]);
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_ref() {
frame.render_widget(net, horizontal_chunks[0]);
} else {
frame.render_widget(&widgets.mem, horizontal_chunks[0]);
}
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());
frame.render_widget(&app.help_menu, rect);
})
.unwrap();
}
// TODO: figure out how to draw the proc widget without clearing rest of the screen
pub fn draw_proc<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
draw(terminal, app);
// terminal.draw(|mut frame| {
// let chunks = if app.statusbar.is_some() {
// Layout::default()
// .constraints([Constraint::Min(0), Constraint::Length(1)].as_ref())
// .split(frame.size())
// } else {
// Layout::default()
// .constraints(vec![Constraint::Percentage(100)])
// .split(frame.size())
// };
// let vertical_chunks = if app.widgets.temp.is_some() {
// Layout::default()
// .direction(Direction::Vertical)
// .constraints(
// [
// Constraint::Ratio(1, 3),
// Constraint::Ratio(1, 3),
// Constraint::Ratio(1, 3),
// ]
// .as_ref(),
// )
// .split(chunks[0])
// } else {
// Layout::default()
// .direction(Direction::Vertical)
// .constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)].as_ref())
// .split(chunks[0])
// };
// let horizontal_chunks = Layout::default()
// .direction(Direction::Horizontal)
// .constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)].as_ref())
// .split(*vertical_chunks.last().unwrap());
// app.widgets.proc.render(&mut frame, horizontal_chunks[1]);
// })
}
// TODO: figure out how to draw the graphs without clearing rest of the screen
pub fn draw_graphs<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
draw(terminal, app);
}