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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
#[macro_use]
extern crate log;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate lazy_static;
use crossterm::{
event::{
poll, read, DisableMouseCapture, EnableMouseCapture, Event as CEvent, KeyCode,
KeyModifiers, MouseEvent,
},
execute,
style::Print,
terminal::LeaveAlternateScreen,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
};
use std::{
io::{stdout, Write},
panic::{self, PanicInfo},
sync::mpsc,
thread,
time::{Duration, Instant},
};
use tui::{backend::CrosstermBackend, Terminal};
pub mod app;
mod utils {
pub mod error;
pub mod gen_util;
pub mod logging;
}
mod canvas;
mod constants;
mod data_conversion;
use app::data_harvester::{self, processes::ProcessSorting};
use constants::TICK_RATE_IN_MILLISECONDS;
use data_conversion::*;
use utils::error::{self, BottomError};
enum Event<I, J> {
KeyInput(I),
MouseInput(J),
Update(Box<data_harvester::Data>),
Clean,
}
enum ResetEvent {
Reset,
}
fn main() -> error::Result<()> {
// Parse command line options
let matches = clap_app!(app =>
(name: crate_name!())
(version: crate_version!())
(author: crate_authors!())
(about: crate_description!())
(@arg AVG_CPU: -a --avgcpu "Enables showing the average CPU usage.")
(@arg DOT_MARKER: -m --dot_marker "Use a dot marker instead of the default braille marker.")
(@arg DEBUG: -d --debug "Enables debug mode, which will output a log file.")
(@group TEMPERATURE_TYPE =>
(@arg CELSIUS : -c --celsius "Sets the temperature type to Celsius. This is the default option.")
(@arg FAHRENHEIT : -f --fahrenheit "Sets the temperature type to Fahrenheit.")
(@arg KELVIN : -k --kelvin "Sets the temperature type to Kelvin.")
)
(@arg RATE_MILLIS: -r --rate +takes_value "Sets a refresh rate in milliseconds; the minimum is 250ms, defaults to 1000ms. Smaller values may take more resources.")
(@arg LEFT_LEGEND: -l --left_legend "Puts external chart legends on the left side rather than the default right side.")
(@arg USE_CURR_USAGE: -u --current_usage "Within Linux, sets a process' CPU usage to be based on the total current CPU usage, rather than assuming 100% usage.")
//(@arg CONFIG_LOCATION: -co --config +takes_value "Sets the location of the config file. Expects a config file in the JSON format.")
//(@arg BASIC_MODE: -b --basic "Sets bottom to basic mode, not showing graphs and only showing basic tables.")
(@arg GROUP_PROCESSES: -g --group "Groups processes with the same name together on launch.")
(@arg CASE_SENSITIVE: -s --case_sensitive "Match case when searching by default.")
(@arg WHOLE_WORD: -w --whole "Match whole word when searching by default.")
(@arg REGEX_DEFAULT: -x --regex "Use regex in searching by default.")
)
.get_matches();
let update_rate_in_milliseconds: u128 = if matches.is_present("RATE_MILLIS") {
matches
.value_of("RATE_MILLIS")
.unwrap_or(&constants::DEFAULT_REFRESH_RATE_IN_MILLISECONDS.to_string())
.parse::<u128>()?
} else {
constants::DEFAULT_REFRESH_RATE_IN_MILLISECONDS
};
if update_rate_in_milliseconds < 250 {
return Err(BottomError::InvalidArg {
message: "Please set your update rate to be greater than 250 milliseconds.".to_string(),
});
} else if update_rate_in_milliseconds > u128::from(std::u64::MAX) {
return Err(BottomError::InvalidArg {
message: "Please set your update rate to be less than unsigned INT_MAX.".to_string(),
});
}
// Attempt to create debugging...
let enable_debugging = matches.is_present("DEBUG");
if enable_debugging || cfg!(debug_assertions) {
utils::logging::init_logger()?;
}
// Set other settings
let temperature_type = if matches.is_present("FAHRENHEIT") {
data_harvester::temperature::TemperatureType::Fahrenheit
} else if matches.is_present("KELVIN") {
data_harvester::temperature::TemperatureType::Kelvin
} else {
data_harvester::temperature::TemperatureType::Celsius
};
let show_average_cpu = matches.is_present("AVG_CPU");
let use_dot = matches.is_present("DOT_MARKER");
let left_legend = matches.is_present("LEFT_LEGEND");
let use_current_cpu_total = matches.is_present("USE_CURR_USAGE");
// Create "app" struct, which will control most of the program and store settings/state
let mut app = app::App::new(
show_average_cpu,
temperature_type,
update_rate_in_milliseconds as u64,
use_dot,
left_legend,
use_current_cpu_total,
);
// Enable grouping immediately if set.
if matches.is_present("GROUP_PROCESSES") {
app.toggle_grouping();
}
// Set default search method
if matches.is_present("CASE_SENSITIVE") {
app.search_state.toggle_ignore_case();
}
if matches.is_present("WHOLE_WORD") {
app.search_state.toggle_search_whole_word();
}
if matches.is_present("REGEX_DEFAULT") {
app.search_state.toggle_search_regex();
}
// Set up up tui and crossterm
let mut stdout_val = stdout();
enable_raw_mode()?;
execute!(stdout_val, EnterAlternateScreen)?;
execute!(stdout_val, EnableMouseCapture)?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?;
terminal.hide_cursor()?;
terminal.clear()?;
// Set panic hook
panic::set_hook(Box::new(|info| panic_hook(info)));
// Set up input handling
let (tx, rx) = mpsc::channel();
{
let tx = tx.clone();
thread::spawn(move || loop {
if poll(Duration::from_millis(20)).is_ok() {
let mut mouse_timer = Instant::now();
let mut keyboard_timer = Instant::now();
loop {
if poll(Duration::from_millis(20)).is_ok() {
if let Ok(event) = read() {
if let CEvent::Key(key) = event {
if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 {
if tx.send(Event::KeyInput(key)).is_err() {
return;
}
keyboard_timer = Instant::now();
}
} else if let CEvent::Mouse(mouse) = event {
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
if tx.send(Event::MouseInput(mouse)).is_err() {
return;
}
mouse_timer = Instant::now();
}
}
}
}
}
}
});
}
// Cleaning loop
{
let tx = tx.clone();
thread::spawn(move || loop {
thread::sleep(Duration::from_millis(
constants::STALE_MAX_MILLISECONDS as u64 + 5000,
));
tx.send(Event::Clean).unwrap();
});
}
// Event loop
let (rtx, rrx) = mpsc::channel();
{
let tx = tx;
let temp_type = app.temperature_type.clone();
thread::spawn(move || {
let tx = tx.clone();
let mut data_state = data_harvester::DataState::default();
data_state.init();
data_state.set_temperature_type(temp_type);
data_state.set_use_current_cpu_total(use_current_cpu_total);
loop
|