summaryrefslogtreecommitdiffstats
path: root/src/canvas.rs
blob: 6cdf276ee500d40e5770f3d70da9d7d30b35dcfd (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use tui::{
	layout::{Constraint, Direction, Layout},
	style::{Color, Modifier, Style},
	widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Row, Table, Widget},
	Terminal,
};

use crate::{app, utils::error};

const COLOUR_LIST : [Color; 6] = [Color::Red, Color::Green, Color::LightYellow, Color::LightBlue, Color::LightCyan, Color::LightMagenta];
const TEXT_COLOUR : Color = Color::Gray;
const GRAPH_COLOUR : Color = Color::Gray;
const BORDER_STYLE_COLOUR : Color = Color::Gray;
const HIGHLIGHTED_BORDER_STYLE_COLOUR : Color = Color::LightBlue;
const GRAPH_MARKER : Marker = Marker::Braille;

#[derive(Default)]
pub struct CanvasData {
	pub rx_display : String,
	pub tx_display : String,
	pub network_data_rx : Vec<(f64, f64)>,
	pub network_data_tx : Vec<(f64, f64)>,
	pub disk_data : Vec<Vec<String>>,
	pub temp_sensor_data : Vec<Vec<String>>,
	pub process_data : Vec<Vec<String>>,
	pub mem_data : Vec<(f64, f64)>,
	pub swap_data : Vec<(f64, f64)>,
	pub cpu_data : Vec<(String, Vec<(f64, f64)>)>,
}

pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_state : &mut app::App, canvas_data : &CanvasData) -> error::Result<()> {
	let border_style : Style = Style::default().fg(BORDER_STYLE_COLOUR);
	let highlighted_border_style : Style = Style::default().fg(HIGHLIGHTED_BORDER_STYLE_COLOUR);

	let temperature_rows = canvas_data
		.temp_sensor_data
		.iter()
		.map(|sensor| Row::StyledData(sensor.iter(), Style::default().fg(TEXT_COLOUR)));
	let disk_rows = canvas_data.disk_data.iter().map(|disk| Row::StyledData(disk.iter(), Style::default().fg(TEXT_COLOUR)));

	terminal.draw(|mut f| {
		//debug!("Drawing!");
		let vertical_chunks = Layout::default()
			.direction(Direction::Vertical)
			.margin(1)
			.constraints([Constraint::Percentage(34), Constraint::Percentage(34), Constraint::Percentage(33)].as_ref())
			.split(f.size());

		let middle_chunks = Layout::default()
			.direction(Direction::Horizontal)
			.margin(0)
			.constraints([Constraint::Percentage(60), Constraint::Percentage(40)].as_ref())
			.split(vertical_chunks[1]);

		let middle_divided_chunk_2 = Layout::default()
			.direction(Direction::Vertical)
			.margin(0)
			.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
			.split(middle_chunks[1]);

		let bottom_chunks = Layout::default()
			.direction(Direction::Horizontal)
			.margin(0)
			.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
			.split(vertical_chunks[2]);

		// Set up blocks and their components
		// CPU usage graph
		{
			let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]);
			let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([-0.5, 100.5]).labels(&["0%", "100%"]);

			let mut dataset_vector : Vec<Dataset> = Vec::new();

			for (i, cpu) in canvas_data.cpu_data.iter().enumerate() {
				let mut avg_cpu_exist_offset = 0;
				if app_state.show_average_cpu {
					if i == 0 {
						// Skip, we want to render the average cpu last!
						continue;
					}
					else {
						avg_cpu_exist_offset = 1;
					}
				}

				dataset_vector.push(
					Dataset::default()
						.name(&cpu.0)
						.marker(GRAPH_MARKER)
						.style(Style::default().fg(COLOUR_LIST[i - avg_cpu_exist_offset % COLOUR_LIST.len()]))
						.data(&(cpu.1)),
				);
			}

			if !canvas_data.cpu_data.is_empty() && app_state.show_average_cpu {
				dataset_vector.push(
					Dataset::default()
						.name(&canvas_data.cpu_data[0].0)
						.marker(GRAPH_MARKER)
						.style(Style::default().fg(COLOUR_LIST[canvas_data.cpu_data.len() - 1 % COLOUR_LIST.len()]))
						.data(&(canvas_data.cpu_data[0].1)),
				);
			}

			Chart::default()
				.block(
					Block::default()
						.title("CPU Usage")
						.borders(Borders::ALL)
						.border_style(match app_state.current_application_position {
							app::ApplicationPosition::CPU => highlighted_border_style,
							_ => border_style,
						}),
				)
				.x_axis(x_axis)
				.y_axis(y_axis)
				.datasets(&dataset_vector)
				.render(&mut f, vertical_chunks[0]);
		}

		//Memory usage graph
		{
			let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]);
			let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([-0.5, 100.5]).labels(&["0%", "100%"]); // Offset as the zero value isn't drawn otherwise...
			Chart::default()
				.block(
					Block::default()
						.title("Memory Usage")
						.borders(Borders::ALL)
						.border_style(match app_state.current_application_position {
							app::ApplicationPosition::MEM => highlighted_border_style,
							_ => border_style,
						}),
				)
				.x_axis(x_axis)
				.y_axis(y_axis)
				.datasets(&[
					Dataset::default()
						.name(&("RAM:".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
						.marker(GRAPH_MARKER)
						.style(Style::default().fg(Color::LightBlue))
						.data(&canvas_data.mem_data),
					Dataset::default()
						.name(&("SWP:".to_string() + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
						.marker(GRAPH_MARKER)
						.style(Style::default().fg(Color::LightYellow))
						.data(&canvas_data.swap_data),
				])
				.render(&mut f, middle_chunks[0]);
		}

		// Temperature table
		{
			let width = f64::from(middle_divided_chunk_2[0].width);
			Table::new(["Sensor", "Temp"].iter(), temperature_rows)
				.block(
					Block::default()
						.title("Temperatures")
						.borders(Borders::ALL)
						.border_style(match app_state.current_application_position {
							app::ApplicationPosition::TEMP => highlighted_border_style,
							_ => border_style,
						}),
				)
				.header_style(Style::default().fg(Color::LightBlue))
				.widths(&[(width * 0.45) as u16, (width * 0.4) as u16])
				.render(&mut f, middle_divided_chunk_2[0]);
		}

		// Disk usage table
		{
			// TODO: We may have to dynamically remove some of these table elements based on size...
			let width = f64::from(middle_divided_chunk_2[1].width);
			Table::new(["Disk", "Mount", "Used", "Total", "Free", "R/s", "W/s"].iter(), disk_rows)
				.block(
					Block::default()
						.title("Disk Usage")
						.borders(Borders::ALL)
						.border_style(match app_state.current_application_position {
							app::ApplicationPosition::DISK => highlighted_border_style,
							_ => border_style,
						}),
				)
				.header_style(Style::default().fg(Color::LightBlue).modifier(Modifier::BOLD))
				.widths(&[
					(width * 0.18).floor() as u16,
					(