-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathcpu_graph.rs
185 lines (166 loc) · 5.69 KB
/
cpu_graph.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
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
use std::{borrow::Cow, num::NonZeroU16, time::Instant};
use concat_string::concat_string;
use tui::widgets::Row;
use crate::{
app::AppConfigFields,
canvas::{
components::data_table::{
Column, ColumnHeader, DataTable, DataTableColumn, DataTableProps, DataTableStyling,
DataToCell,
},
Painter,
},
data_collection::cpu::CpuDataType,
data_conversion::CpuWidgetData,
options::config::{cpu::CpuDefault, style::Styles},
};
pub enum CpuWidgetColumn {
Cpu,
Use,
}
impl ColumnHeader for CpuWidgetColumn {
fn text(&self) -> Cow<'static, str> {
match self {
CpuWidgetColumn::Cpu => "CPU".into(),
CpuWidgetColumn::Use => "Use".into(),
}
}
}
pub enum CpuWidgetTableData {
All,
Entry {
data_type: CpuDataType,
last_entry: f64,
},
}
impl CpuWidgetTableData {
pub fn from_cpu_widget_data(data: &CpuWidgetData) -> CpuWidgetTableData {
match data {
CpuWidgetData::All => CpuWidgetTableData::All,
CpuWidgetData::Entry {
data_type,
data: _,
last_entry,
} => CpuWidgetTableData::Entry {
data_type: *data_type,
last_entry: *last_entry,
},
}
}
}
impl DataToCell<CpuWidgetColumn> for CpuWidgetTableData {
fn to_cell(
&self, column: &CpuWidgetColumn, calculated_width: NonZeroU16,
) -> Option<Cow<'static, str>> {
const CPU_TRUNCATE_BREAKPOINT: u16 = 5;
let calculated_width = calculated_width.get();
// This is a bit of a hack, but apparently we can avoid having to do any fancy
// checks of showing the "All" on a specific column if the other is
// hidden by just always showing it on the CPU (first) column - if there
// isn't room for it, it will just collapse down.
//
// This is the same for the use percentages - we just *always* show them, and
// *always* hide the CPU column if it is too small.
match &self {
CpuWidgetTableData::All => match column {
CpuWidgetColumn::Cpu => Some("All".into()),
CpuWidgetColumn::Use => None,
},
CpuWidgetTableData::Entry {
data_type,
last_entry,
} => {
if calculated_width == 0 {
None
} else {
match column {
CpuWidgetColumn::Cpu => match data_type {
CpuDataType::Avg => Some("AVG".into()),
CpuDataType::Cpu(index) => {
let index_str = index.to_string();
let text = if calculated_width < CPU_TRUNCATE_BREAKPOINT {
index_str.into()
} else {
concat_string!("CPU", index_str).into()
};
Some(text)
}
},
CpuWidgetColumn::Use => Some(format!("{:.0}%", last_entry.round()).into()),
}
}
}
}
}
#[inline(always)]
fn style_row<'a>(&self, row: Row<'a>, painter: &Painter) -> Row<'a> {
let style = match self {
CpuWidgetTableData::All => painter.styles.all_cpu_colour,
CpuWidgetTableData::Entry {
data_type,
last_entry: _,
} => match data_type {
CpuDataType::Avg => painter.styles.avg_cpu_colour,
CpuDataType::Cpu(index) => {
painter.styles.cpu_colour_styles[index % painter.styles.cpu_colour_styles.len()]
}
},
};
row.style(style)
}
fn column_widths<C: DataTableColumn<CpuWidgetColumn>>(
_data: &[Self], _columns: &[C],
) -> Vec<u16>
where
Self: Sized,
{
vec![1, 3]
}
}
pub struct CpuWidgetState {
pub current_display_time: u64,
pub is_legend_hidden: bool,
pub autohide_timer: Option<Instant>,
pub table: DataTable<CpuWidgetTableData, CpuWidgetColumn>,
}
impl CpuWidgetState {
pub(crate) fn new(
config: &AppConfigFields, default_selection: CpuDefault, current_display_time: u64,
autohide_timer: Option<Instant>, colours: &Styles,
) -> Self {
const COLUMNS: [Column<CpuWidgetColumn>; 2] = [
Column::soft(CpuWidgetColumn::Cpu, Some(0.5)),
Column::soft(CpuWidgetColumn::Use, Some(0.5)),
];
let props = DataTableProps {
title: None,
table_gap: config.table_gap,
left_to_right: false,
is_basic: false,
show_table_scroll_position: false, // TODO: Should this be possible?
show_current_entry_when_unfocused: true,
};
let styling = DataTableStyling::from_palette(colours);
let mut table = DataTable::new(COLUMNS, props, styling);
match default_selection {
CpuDefault::All => {}
CpuDefault::Average if !config.show_average_cpu => {}
CpuDefault::Average => {
table = table.first_draw_index(1);
}
}
CpuWidgetState {
current_display_time,
is_legend_hidden: false,
autohide_timer,
table,
}
}
pub fn update_table(&mut self, data: &[CpuWidgetData]) {
self.table.set_data(
data.iter()
.map(CpuWidgetTableData::from_cpu_widget_data)
.collect(),
);
}
}