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
|
use std::{
cmp::{max, Ordering},
fmt::Display,
};
use concat_string::concat_string;
use tui::{text::Text, widgets::Row};
use super::proc_widget_column::ProcColumn;
use crate::{
app::data_harvester::processes::ProcessHarvest,
canvas::Painter,
components::data_table::{DataTableColumn, DataToCell},
data_conversion::{binary_byte_string, dec_bytes_per_second_string, dec_bytes_string},
utils::gen_util::truncate_to_text,
Pid,
};
#[derive(Clone, Debug)]
enum IdType {
Name(String),
Command(String),
}
#[derive(Clone, Debug)]
pub struct Id {
id_type: IdType,
prefix: Option<String>,
}
impl From<&'static str> for Id {
fn from(s: &'static str) -> Self {
Id {
id_type: IdType::Name(s.to_string()),
prefix: None,
}
}
}
impl Id {
/// Returns the ID as a lowercase [`String`], with no prefix. This is primarily useful for
/// cases like sorting where we treat everything as the same case (e.g. `Discord` comes before
/// `dkms`).
pub fn to_lowercase(&self) -> String {
match &self.id_type {
IdType::Name(name) => name.to_lowercase(),
IdType::Command(cmd) => cmd.to_lowercase(),
}
}
/// Return the ID as a borrowed [`str`] with no prefix.
pub fn as_str(&self) -> &str {
match &self.id_type {
IdType::Name(name) => name.as_str(),
IdType::Command(cmd) => cmd.as_str(),
}
}
/// Returns the ID as a [`String`] with prefix.
pub fn to_prefixed_string(&self) -> String {
if let Some(prefix) = &self.prefix {
concat_string!(
prefix,
match &self.id_type {
IdType::Name(name) => name,
IdType::Command(cmd) => cmd,
}
)
} else {
match &self.id_type {
IdType::Name(name) => name.to_string(),
IdType::Command(cmd) => cmd.to_string(),
}
}
}
}
impl Display for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
// TODO: Can reduce this to 32 bytes.
#[derive(PartialEq, Clone, Debug)]
pub enum MemUsage {
Percent(f64),
Bytes(u64),
}
impl PartialOrd for MemUsage {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(MemUsage::Percent(a), MemUsage::Percent(b)) => a.partial_cmp(b),
(MemUsage::Bytes(a), MemUsage::Bytes(b)) => a.partial_cmp(b),
_ => unreachable!(),
}
}
}
impl Display for MemUsage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MemUsage::Percent(percent) => f.write_fmt(format_args!("{:.1}%", percent)),
MemUsage::Bytes(bytes) => f.write_str(&binary_byte_string(*bytes)),
}
}
}
#[derive(Clone, Debug)]
pub struct ProcWidgetData {
pub pid: Pid,
pub ppid: Option<Pid>,
pub id: Id,
pub cpu_usage_percent: f64,
pub mem_usage: MemUsage,
pub rps: u64,
pub wps: u64,
pub total_read: u64,
pub total_write: u64,
pub process_state: String,
pub process_char: char,
#[cfg(target_family = "unix")]
pub user: String,
pub num_similar: u64,
pub disabled: bool,
}
impl ProcWidgetData {
pub fn from_data(process: &ProcessHarvest, is_command: bool, is_mem_percent: bool) -> Self {
let id = Id {
id_type: if is_command {
IdType::Command(process.command.clone())
} else {
IdType::Name(process.name.clone())
},
prefix: None,
};
let mem_usage = if is_mem_percent {
MemUsage::Percent(process.mem_usage_percent)
} else {
MemUsage::Bytes(process.mem_usage_bytes)
};
Self {
pid: process.pid,
ppid: process.parent_pid,
id,
cpu_usage_percent: process.cpu_usage_percent,
mem_usage,
rps: process.read_bytes_per_sec,
wps: process.write_bytes_per_sec,
total_read: process.total_read_bytes,
total_write: process.total_write_bytes,
process_state: process.process_state.0.clone(),
process_char: process.process_state.1,
#[cfg(target_family = "unix")]
user: process.user.to_string(),
num_similar: 1,
disabled: false,
}
}
pub fn num_similar(mut self, num_similar: u64) -> Self {
self.num_similar = num_similar;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn prefix(mut self, prefix: Option<String>) -> Self {
self.id.prefix = prefix;
self
}
pub fn add(&mut self, other: &Self) {
self.cpu_usage_percent += other.cpu_usage_percent;
self.mem_usage = match (&self.mem_usage, &other.mem_usage) {
(MemUsage::Percent(a), MemUsage::Percent(b)) => MemUsage::Percent(a + b),
(MemUsage::Bytes(a), MemUsage::Bytes(b)) => MemUsage::Bytes(a + b),
(MemUsage::Percent(_), MemUsage::Bytes(_))
| (MemUsage::Bytes(_), MemUsage::Percent(_)) => {
unreachable!("trying to add together two different memory usage types!")
}
};
self.rps += other.rps;
self.wps += other.wps;
self.total_read += other.total_read;
self.total_write += other.total_write;
}
fn to_string(&self, column: &ProcColumn) -> String {
match column {
ProcColumn::CpuPercent => format!("{:.1}%", self.cpu_usage_percent),
ProcColumn::MemoryVal | ProcColumn::MemoryPercent => self.mem_usage.to_string(),
ProcColumn::Pid => self.pid.to_string(),
ProcColumn::Count => self.num_similar.to_string(),
ProcColumn::Name | ProcColumn::Command => self.id.to_prefixed_string(),
ProcColumn::ReadPerSecond => dec_bytes_per_second_string(self.rps),
ProcColumn::WritePerSecond => dec_bytes_per_second_string(self.wps),
ProcColumn::TotalRead => dec_bytes_string(self.total_read),
ProcColumn::TotalWrite => dec_bytes_string(self.total_write),
ProcColumn::State => self.process_char.to_string(),
ProcColumn::User => {
#[cfg(target_family = "unix")]
{
self.user.clone()
}
#[cfg(not(target_family = "unix"))]
{
"".to_string()
}
}
}
}
}
impl DataToCell<ProcColumn> for ProcWidgetData {
fn to_cell<'a>(&'a self, column: &ProcColumn, calculated_width: u16) -> Option<Text<'a>> {
// TODO: Optimize the string allocations here...
Some(truncate_to_text(
&match column {
ProcColumn::CpuPercent => {
format!("{:.1}%", self.cpu_usage_percent)
}
ProcColumn::MemoryVal | ProcColumn::MemoryPercent => self.mem_usage.to_string(),
ProcColumn::Pid => self.pid.to_string(),
ProcColumn::Count => self.num_similar.to_string(),
ProcColumn::Name | ProcColumn::Command => self.id.to_prefixed_string(),
ProcColumn::ReadPerSecond => dec_bytes_per_second_string(self.rps),
ProcColumn::WritePerSecond => dec_bytes_per_second_string(self.wps),
ProcColumn::TotalRead => dec_bytes_string(self.total_read),
ProcColumn::TotalWrite => dec_bytes_string(self.total_write),
ProcColumn::State => {
|