Refactoring.

This commit is contained in:
ClementTsang 2020-02-29 17:07:47 -05:00
parent eb2622467f
commit 4c98fe4fde
14 changed files with 246 additions and 234 deletions

View File

@ -3,7 +3,6 @@ max_width = 100
newline_style = "Unix" newline_style = "Unix"
reorder_imports = true reorder_imports = true
fn_args_layout = "Compressed" fn_args_layout = "Compressed"
hard_tabs = true
merge_derives = true merge_derives = true
reorder_modules = true reorder_modules = true
tab_spaces = 4 tab_spaces = 4

View File

@ -609,11 +609,11 @@ impl App {
WidgetPosition::ProcessSearch => { WidgetPosition::ProcessSearch => {
if self.process_search_state.search_state.is_enabled if self.process_search_state.search_state.is_enabled
&& self.get_cursor_position() && self.get_cursor_position()
< self < self
.process_search_state .process_search_state
.search_state .search_state
.current_search_query .current_search_query
.len() .len()
{ {
self.process_search_state self.process_search_state
.search_state .search_state
@ -712,7 +712,8 @@ impl App {
pub fn on_up_key(&mut self) { pub fn on_up_key(&mut self) {
if !self.is_in_dialog() { if !self.is_in_dialog() {
if let WidgetPosition::ProcessSearch = self.current_widget_selected {} else { if let WidgetPosition::ProcessSearch = self.current_widget_selected {
} else {
self.decrement_position_count(); self.decrement_position_count();
} }
} }
@ -720,7 +721,8 @@ impl App {
pub fn on_down_key(&mut self) { pub fn on_down_key(&mut self) {
if !self.is_in_dialog() { if !self.is_in_dialog() {
if let WidgetPosition::ProcessSearch = self.current_widget_selected {} else { if let WidgetPosition::ProcessSearch = self.current_widget_selected {
} else {
self.increment_position_count(); self.increment_position_count();
} }
} }
@ -858,7 +860,8 @@ impl App {
let current_key_press_inst = Instant::now(); let current_key_press_inst = Instant::now();
if current_key_press_inst if current_key_press_inst
.duration_since(self.last_key_press) .duration_since(self.last_key_press)
.as_millis() > constants::MAX_KEY_TIMEOUT_IN_MILLISECONDS .as_millis()
> constants::MAX_KEY_TIMEOUT_IN_MILLISECONDS
{ {
self.reset_multi_tap_keys(); self.reset_multi_tap_keys();
} }
@ -1172,12 +1175,14 @@ impl App {
WidgetPosition::Process => { WidgetPosition::Process => {
self.app_scroll_positions self.app_scroll_positions
.process_scroll_state .process_scroll_state
.current_scroll_position = self.canvas_data.finalized_process_data.len() as u64 - 1 .current_scroll_position =
self.canvas_data.finalized_process_data.len() as u64 - 1
} }
WidgetPosition::Temp => { WidgetPosition::Temp => {
self.app_scroll_positions self.app_scroll_positions
.temp_scroll_state .temp_scroll_state
.current_scroll_position = self.canvas_data.temp_sensor_data.len() as u64 - 1 .current_scroll_position =
self.canvas_data.temp_sensor_data.len() as u64 - 1
} }
WidgetPosition::Disk => { WidgetPosition::Disk => {
self.app_scroll_positions self.app_scroll_positions
@ -1247,7 +1252,7 @@ impl App {
if current_posn as i64 + num_to_change_by >= 0 if current_posn as i64 + num_to_change_by >= 0
&& current_posn as i64 + num_to_change_by && current_posn as i64 + num_to_change_by
< self.canvas_data.finalized_process_data.len() as i64 < self.canvas_data.finalized_process_data.len() as i64
{ {
self.app_scroll_positions self.app_scroll_positions
.process_scroll_state .process_scroll_state
@ -1263,7 +1268,7 @@ impl App {
if current_posn as i64 + num_to_change_by >= 0 if current_posn as i64 + num_to_change_by >= 0
&& current_posn as i64 + num_to_change_by && current_posn as i64 + num_to_change_by
< self.canvas_data.temp_sensor_data.len() as i64 < self.canvas_data.temp_sensor_data.len() as i64
{ {
self.app_scroll_positions self.app_scroll_positions
.temp_scroll_state .temp_scroll_state

View File

@ -15,7 +15,7 @@
use std::time::Instant; use std::time::Instant;
use std::vec::Vec; use std::vec::Vec;
use crate::data_harvester::{cpu, Data, disks, mem, network, processes, temperature}; use crate::data_harvester::{cpu, disks, mem, network, processes, temperature, Data};
pub type TimeOffset = f64; pub type TimeOffset = f64;
pub type Value = f64; pub type Value = f64;

View File

@ -149,13 +149,13 @@ impl DataState {
let temp_data_fut = temperature::get_temperature_data(&self.sys, &self.temperature_type); let temp_data_fut = temperature::get_temperature_data(&self.sys, &self.temperature_type);
let (net_data, mem_res, swap_res, disk_res, io_res, temp_res) = join!( let (net_data, mem_res, swap_res, disk_res, io_res, temp_res) = join!(
network_data_fut, network_data_fut,
mem_data_fut, mem_data_fut,
swap_data_fut, swap_data_fut,
disk_data_fut, disk_data_fut,
disk_io_usage_fut, disk_io_usage_fut,
temp_data_fut temp_data_fut
); );
// After async // After async
self.data.network = net_data; self.data.network = net_data;

View File

@ -68,13 +68,13 @@ pub async fn get_disk_usage_list() -> crate::utils::error::Result<Vec<DiskHarves
.mount_point() .mount_point()
.to_str() .to_str()
.unwrap_or("Name Unavailable")) .unwrap_or("Name Unavailable"))
.to_string(), .to_string(),
name: (partition name: (partition
.device() .device()
.unwrap_or_else(|| std::ffi::OsStr::new("Name Unavailable")) .unwrap_or_else(|| std::ffi::OsStr::new("Name Unavailable"))
.to_str() .to_str()
.unwrap_or("Name Unavailable")) .unwrap_or("Name Unavailable"))
.to_string(), .to_string(),
}); });
} }
} }

View File

@ -1,7 +1,7 @@
use std::{ use std::{
collections::{hash_map::RandomState, HashMap}, collections::{hash_map::RandomState, HashMap},
process::Command, process::Command,
time::Instant, time::Instant,
}; };
use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt}; use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt};

View File

@ -37,13 +37,15 @@ pub async fn get_temperature_data(
temperature: match temp_type { temperature: match temp_type {
TemperatureType::Celsius => sensor TemperatureType::Celsius => sensor
.current() .current()
.get::<thermodynamic_temperature::degree_celsius>(), .get::<thermodynamic_temperature::degree_celsius>(
),
TemperatureType::Kelvin => { TemperatureType::Kelvin => {
sensor.current().get::<thermodynamic_temperature::kelvin>() sensor.current().get::<thermodynamic_temperature::kelvin>()
} }
TemperatureType::Fahrenheit => sensor TemperatureType::Fahrenheit => sensor
.current() .current()
.get::<thermodynamic_temperature::degree_fahrenheit>(), .get::<thermodynamic_temperature::degree_fahrenheit>(
),
}, },
}); });
} }

View File

@ -3,10 +3,10 @@ use std::process::Command;
// Copied from SO: https://stackoverflow.com/a/55231715 // Copied from SO: https://stackoverflow.com/a/55231715
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use winapi::{ use winapi::{
shared::{minwindef::DWORD, ntdef::HANDLE}, shared::{minwindef::DWORD, ntdef::HANDLE},
um::{ um::{
processthreadsapi::{OpenProcess, TerminateProcess}, processthreadsapi::{OpenProcess, TerminateProcess},
winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE}, winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE},
}, },
}; };
@ -38,10 +38,10 @@ pub fn kill_process_given_pid(pid: u32) -> crate::utils::error::Result<()> {
Command::new("kill").arg(pid.to_string()).output()?; Command::new("kill").arg(pid.to_string()).output()?;
} else if cfg!(target_os = "windows") { } else if cfg!(target_os = "windows") {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
let process = Process::open(pid as DWORD)?; let process = Process::open(pid as DWORD)?;
process.kill()?; process.kill()?;
} }
} else { } else {
return Err(BottomError::GenericError( return Err(BottomError::GenericError(
"Sorry, support operating systems outside the main three are not implemented yet!" "Sorry, support operating systems outside the main three are not implemented yet!"

View File

@ -2,12 +2,12 @@ use std::cmp::max;
use std::collections::HashMap; use std::collections::HashMap;
use tui::{ use tui::{
backend, backend,
layout::{Alignment, Constraint, Direction, Layout, Rect}, layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style}, style::{Color, Style},
Terminal, terminal::Frame,
terminal::Frame, widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Paragraph, Row, Table, Text, Widget},
widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Paragraph, Row, Table, Text, Widget}, Terminal,
}; };
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -16,10 +16,10 @@ use canvas_colours::*;
use drawing_utils::*; use drawing_utils::*;
use crate::{ use crate::{
app::{self, data_harvester::processes::ProcessHarvest, WidgetPosition}, app::{self, data_harvester::processes::ProcessHarvest, WidgetPosition},
constants::*, constants::*,
data_conversion::{ConvertedCpuData, ConvertedProcessData}, data_conversion::{ConvertedCpuData, ConvertedProcessData},
utils::error, utils::error,
}; };
mod canvas_colours; mod canvas_colours;
@ -35,32 +35,32 @@ const NETWORK_HEADERS: [&str; 4] = ["RX", "TX", "Total RX", "Total TX"];
const FORCE_MIN_THRESHOLD: usize = 5; const FORCE_MIN_THRESHOLD: usize = 5;
lazy_static! { lazy_static! {
static ref DEFAULT_TEXT_STYLE: Style = Style::default().fg(Color::Gray); static ref DEFAULT_TEXT_STYLE: Style = Style::default().fg(Color::Gray);
static ref DEFAULT_HEADER_STYLE: Style = Style::default().fg(Color::LightBlue); static ref DEFAULT_HEADER_STYLE: Style = Style::default().fg(Color::LightBlue);
static ref DISK_HEADERS_LENS: Vec<usize> = DISK_HEADERS static ref DISK_HEADERS_LENS: Vec<usize> = DISK_HEADERS
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
static ref CPU_LEGEND_HEADER_LENS: Vec<usize> = CPU_LEGEND_HEADER static ref CPU_LEGEND_HEADER_LENS: Vec<usize> = CPU_LEGEND_HEADER
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
static ref CPU_SELECT_LEGEND_HEADER_LENS: Vec<usize> = CPU_SELECT_LEGEND_HEADER static ref CPU_SELECT_LEGEND_HEADER_LENS: Vec<usize> = CPU_SELECT_LEGEND_HEADER
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
static ref TEMP_HEADERS_LENS: Vec<usize> = TEMP_HEADERS static ref TEMP_HEADERS_LENS: Vec<usize> = TEMP_HEADERS
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
static ref MEM_HEADERS_LENS: Vec<usize> = MEM_HEADERS static ref MEM_HEADERS_LENS: Vec<usize> = MEM_HEADERS
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
static ref NETWORK_HEADERS_LENS: Vec<usize> = NETWORK_HEADERS static ref NETWORK_HEADERS_LENS: Vec<usize> = NETWORK_HEADERS
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
} }
#[derive(Default)] #[derive(Default)]
@ -677,9 +677,10 @@ impl Painter {
app::WidgetPosition::Cpu => { app::WidgetPosition::Cpu => {
if itx as u64 if itx as u64
== app_state == app_state
.app_scroll_positions .app_scroll_positions
.cpu_scroll_state .cpu_scroll_state
.current_scroll_position - start_position .current_scroll_position
- start_position
{ {
self.colours.currently_selected_text_style self.colours.currently_selected_text_style
} else if app_state.app_config_fields.show_average_cpu && itx == 0 { } else if app_state.app_config_fields.show_average_cpu && itx == 0 {
@ -687,7 +688,7 @@ impl Painter {
} else { } else {
self.colours.cpu_colour_styles[itx self.colours.cpu_colour_styles[itx
+ start_position as usize + start_position as usize
% self.colours.cpu_colour_styles.len()] % self.colours.cpu_colour_styles.len()]
} }
} }
_ => { _ => {
@ -696,7 +697,7 @@ impl Painter {
} else { } else {
self.colours.cpu_colour_styles[itx self.colours.cpu_colour_styles[itx
+ start_position as usize + start_position as usize
% self.colours.cpu_colour_styles.len()] % self.colours.cpu_colour_styles.len()]
} }
} }
}, },
@ -738,34 +739,34 @@ impl Painter {
} else { } else {
CPU_LEGEND_HEADER CPU_LEGEND_HEADER
} }
.iter(), .iter(),
cpu_rows, cpu_rows,
) )
.block( .block(
Block::default() Block::default()
.title(&title) .title(&title)
.title_style(if app_state.is_expanded { .title_style(if app_state.is_expanded {
self.colours.highlighted_border_style self.colours.highlighted_border_style
} else { } else {
match app_state.current_widget_selected { match app_state.current_widget_selected {
app::WidgetPosition::Cpu => self.colours.highlighted_border_style,
_ => self.colours.border_style,
}
})
.borders(Borders::ALL)
.border_style(match app_state.current_widget_selected {
app::WidgetPosition::Cpu => self.colours.highlighted_border_style, app::WidgetPosition::Cpu => self.colours.highlighted_border_style,
_ => self.colours.border_style, _ => self.colours.border_style,
}), }
) })
.header_style(self.colours.table_header_style) .borders(Borders::ALL)
.widths( .border_style(match app_state.current_widget_selected {
&(intrinsic_widths app::WidgetPosition::Cpu => self.colours.highlighted_border_style,
.iter() _ => self.colours.border_style,
.map(|calculated_width| Constraint::Length(*calculated_width as u16)) }),
.collect::<Vec<_>>()), )
) .header_style(self.colours.table_header_style)
.render(f, draw_loc); .widths(
&(intrinsic_widths
.iter()
.map(|calculated_width| Constraint::Length(*calculated_width as u16))
.collect::<Vec<_>>()),
)
.render(f, draw_loc);
} }
fn draw_memory_graph<B: backend::Backend>( fn draw_memory_graph<B: backend::Backend>(
@ -999,9 +1000,10 @@ impl Painter {
app::WidgetPosition::Temp => { app::WidgetPosition::Temp => {
if temp_row_counter as u64 if temp_row_counter as u64
== app_state == app_state
.app_scroll_positions .app_scroll_positions
.temp_scroll_state .temp_scroll_state
.current_scroll_position - start_position .current_scroll_position
- start_position
{ {
temp_row_counter = -1; temp_row_counter = -1;
self.colours.currently_selected_text_style self.colours.currently_selected_text_style
@ -1095,9 +1097,10 @@ impl Painter {
app::WidgetPosition::Disk => { app::WidgetPosition::Disk => {
if disk_counter as u64 if disk_counter as u64
== app_state == app_state
.app_scroll_positions .app_scroll_positions
.disk_scroll_state .disk_scroll_state
.current_scroll_position - start_position .current_scroll_position
- start_position
{ {
disk_counter = -1; disk_counter = -1;
self.colours.currently_selected_text_style self.colours.currently_selected_text_style
@ -1395,9 +1398,10 @@ impl Painter {
app::WidgetPosition::Process => { app::WidgetPosition::Process => {
if process_counter as u64 if process_counter as u64
== app_state == app_state
.app_scroll_positions .app_scroll_positions
.process_scroll_state .process_scroll_state
.current_scroll_position - start_position .current_scroll_position
- start_position
{ {
process_counter = -1; process_counter = -1;
self.colours.currently_selected_text_style self.colours.currently_selected_text_style
@ -1419,7 +1423,7 @@ impl Painter {
} else { } else {
"PID(p)" "PID(p)"
} }
.to_string(); .to_string();
let mut name = "Name(n)".to_string(); let mut name = "Name(n)".to_string();
let mut cpu = "CPU%(c)".to_string(); let mut cpu = "CPU%(c)".to_string();
let mut mem = "Mem%(m)".to_string(); let mut mem = "Mem%(m)".to_string();

View File

@ -13,28 +13,28 @@ pub const STANDARD_FOURTH_COLOUR: Color = Color::LightGreen;
pub const AVG_COLOUR: Color = Color::Red; pub const AVG_COLOUR: Color = Color::Red;
lazy_static! { lazy_static! {
static ref COLOR_NAME_LOOKUP_TABLE: HashMap<&'static str, Color> = [ static ref COLOR_NAME_LOOKUP_TABLE: HashMap<&'static str, Color> = [
("reset", Color::Reset), ("reset", Color::Reset),
("black", Color::Black), ("black", Color::Black),
("red", Color::Red), ("red", Color::Red),
("green", Color::Green), ("green", Color::Green),
("yellow", Color::Yellow), ("yellow", Color::Yellow),
("blue", Color::Blue), ("blue", Color::Blue),
("magenta", Color::Magenta), ("magenta", Color::Magenta),
("cyan", Color::Cyan), ("cyan", Color::Cyan),
("gray", Color::Gray), ("gray", Color::Gray),
("darkgray", Color::DarkGray), ("darkgray", Color::DarkGray),
("lightred", Color::LightRed), ("lightred", Color::LightRed),
("lightgreen", Color::LightGreen), ("lightgreen", Color::LightGreen),
("lightyellow", Color::LightYellow), ("lightyellow", Color::LightYellow),
("lightblue", Color::LightBlue), ("lightblue", Color::LightBlue),
("lightmagenta", Color::LightMagenta), ("lightmagenta", Color::LightMagenta),
("lightcyan", Color::LightCyan), ("lightcyan", Color::LightCyan),
("white", Color::White) ("white", Color::White)
] ]
.iter() .iter()
.copied() .copied()
.collect(); .collect();
} }
/// Generates random colours. Strategy found from /// Generates random colours. Strategy found from

View File

@ -6,13 +6,13 @@ use std::collections::HashMap;
use constants::*; use constants::*;
use crate::{ use crate::{
app::{ app::{
App, data_farmer,
data_farmer, data_harvester::{self, processes::ProcessHarvest},
data_harvester::{self, processes::ProcessHarvest}, App,
}, },
constants, constants,
utils::gen_util::{get_exact_byte_values, get_simple_byte_values}, utils::gen_util::{get_exact_byte_values, get_simple_byte_values},
}; };
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -54,10 +54,10 @@ pub fn convert_temp_row(app: &App) -> Vec<Vec<String>> {
sensor.component_name.to_string(), sensor.component_name.to_string(),
(sensor.temperature.ceil() as u64).to_string() (sensor.temperature.ceil() as u64).to_string()
+ match temp_type { + match temp_type {
data_harvester::temperature::TemperatureType::Celsius => "C", data_harvester::temperature::TemperatureType::Celsius => "C",
data_harvester::temperature::TemperatureType::Kelvin => "K", data_harvester::temperature::TemperatureType::Kelvin => "K",
data_harvester::temperature::TemperatureType::Fahrenheit => "F", data_harvester::temperature::TemperatureType::Fahrenheit => "F",
}, },
]); ]);
} }
} }
@ -191,15 +191,16 @@ pub fn convert_mem_labels(current_data: &data_farmer::DataCollection) -> (String
} else { } else {
"RAM:".to_string() "RAM:".to_string()
+ &format!( + &format!(
"{:3.0}%", "{:3.0}%",
(current_data.memory_harvest.mem_used_in_mb as f64 * 100.0 (current_data.memory_harvest.mem_used_in_mb as f64 * 100.0
/ current_data.memory_harvest.mem_total_in_mb as f64) / current_data.memory_harvest.mem_total_in_mb as f64)
.round() .round()
) + &format!( )
" {:.1}GB/{:.1}GB", + &format!(
current_data.memory_harvest.mem_used_in_mb as f64 / 1024.0, " {:.1}GB/{:.1}GB",
(current_data.memory_harvest.mem_total_in_mb as f64 / 1024.0).round() current_data.memory_harvest.mem_used_in_mb as f64 / 1024.0,
) (current_data.memory_harvest.mem_total_in_mb as f64 / 1024.0).round()
)
}; };
let swap_label = if current_data.swap_harvest.mem_total_in_mb == 0 { let swap_label = if current_data.swap_harvest.mem_total_in_mb == 0 {
@ -207,15 +208,16 @@ pub fn convert_mem_labels(current_data: &data_farmer::DataCollection) -> (String
} else { } else {
"SWP:".to_string() "SWP:".to_string()
+ &format!( + &format!(
"{:3.0}%", "{:3.0}%",
(current_data.swap_harvest.mem_used_in_mb as f64 * 100.0 (current_data.swap_harvest.mem_used_in_mb as f64 * 100.0
/ current_data.swap_harvest.mem_total_in_mb as f64) / current_data.swap_harvest.mem_total_in_mb as f64)
.round() .round()
) + &format!( )
" {:.1}GB/{:.1}GB", + &format!(
current_data.swap_harvest.mem_used_in_mb as f64 / 1024.0, " {:.1}GB/{:.1}GB",
(current_data.swap_harvest.mem_total_in_mb as f64 / 1024.0).round() current_data.swap_harvest.mem_used_in_mb as f64 / 1024.0,
) (current_data.swap_harvest.mem_total_in_mb as f64 / 1024.0).round()
)
}; };
(mem_label, swap_label) (mem_label, swap_label)

View File

@ -10,29 +10,29 @@ extern crate lazy_static;
extern crate log; extern crate log;
use std::{ use std::{
boxed::Box, boxed::Box,
io::{stdout, Write}, io::{stdout, Write},
panic::{self, PanicInfo}, panic::{self, PanicInfo},
sync::mpsc, sync::mpsc,
thread, thread,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{ use crossterm::{
event::{ event::{
DisableMouseCapture, EnableMouseCapture, Event as CEvent, KeyCode, KeyEvent, KeyModifiers, MouseEvent, poll, read, DisableMouseCapture, EnableMouseCapture, Event as CEvent, KeyCode, KeyEvent,
poll, read, KeyModifiers, MouseEvent,
}, },
execute, execute,
style::Print, style::Print,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen}, terminal::LeaveAlternateScreen,
terminal::LeaveAlternateScreen, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
}; };
use tui::{backend::CrosstermBackend, Terminal}; use tui::{backend::CrosstermBackend, Terminal};
use app::{ use app::{
App, data_harvester::{self, processes::ProcessSorting},
data_harvester::{self, processes::ProcessSorting}, App,
}; };
use constants::*; use constants::*;
use data_conversion::*; use data_conversion::*;
@ -435,10 +435,10 @@ fn cleanup_terminal(
) -> error::Result<()> { ) -> error::Result<()> {
disable_raw_mode()?; disable_raw_mode()?;
execute!( execute!(
terminal.backend_mut(), terminal.backend_mut(),
DisableMouseCapture, DisableMouseCapture,
LeaveAlternateScreen LeaveAlternateScreen
)?; )?;
terminal.show_cursor()?; terminal.show_cursor()?;
Ok(()) Ok(())
@ -543,15 +543,15 @@ fn panic_hook(panic_info: &PanicInfo<'_>) {
// Print stack trace. Must be done after! // Print stack trace. Must be done after!
execute!( execute!(
stdout, stdout,
Print(format!( Print(format!(
"thread '<unnamed>' panicked at '{}', {}\n\r{}", "thread '<unnamed>' panicked at '{}', {}\n\r{}",
msg, msg,
panic_info.location().unwrap(), panic_info.location().unwrap(),
stacktrace stacktrace
)), )),
) )
.unwrap(); .unwrap();
} }
fn update_final_process_list(app: &mut App) { fn update_final_process_list(app: &mut App) {

View File

@ -1,9 +1,9 @@
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
app::{self, App, data_harvester}, app::{self, data_harvester, App},
constants::*, constants::*,
utils::error::{self, BottomError}, utils::error::{self, BottomError},
}; };
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]

View File

@ -10,64 +10,64 @@ use predicates::prelude::*;
//======================RATES======================// //======================RATES======================//
fn get_os_binary_loc() -> String { fn get_os_binary_loc() -> String {
if cfg!(target_os = "linux") { if cfg!(target_os = "linux") {
"./target/x86_64-unknown-linux-gnu/debug/btm".to_string() "./target/x86_64-unknown-linux-gnu/debug/btm".to_string()
} else if cfg!(target_os = "windows") { } else if cfg!(target_os = "windows") {
"./target/x86_64-pc-windows-msvc/debug/btm".to_string() "./target/x86_64-pc-windows-msvc/debug/btm".to_string()
} else if cfg!(target_os = "macos") { } else if cfg!(target_os = "macos") {
"./target/x86_64-apple-darwin/debug/btm".to_string() "./target/x86_64-apple-darwin/debug/btm".to_string()
} else { } else {
"".to_string() "".to_string()
} }
} }
#[test] #[test]
fn test_small_rate() -> Result<(), Box<dyn std::error::Error>> { fn test_small_rate() -> Result<(), Box<dyn std::error::Error>> {
Command::new(get_os_binary_loc()) Command::new(get_os_binary_loc())
.arg("-r") .arg("-r")
.arg("249") .arg("249")
.assert() .assert()
.failure() .failure()
.stderr(predicate::str::contains("rate to be greater than 250")); .stderr(predicate::str::contains("rate to be greater than 250"));
Ok(()) Ok(())
} }
#[test] #[test]
fn test_large_rate() -> Result<(), Box<dyn std::error::Error>> { fn test_large_rate() -> Result<(), Box<dyn std::error::Error>> {
Command::new(get_os_binary_loc()) Command::new(get_os_binary_loc())
.arg("-r") .arg("-r")
.arg("18446744073709551616") .arg("18446744073709551616")
.assert() .assert()
.failure() .failure()
.stderr(predicate::str::contains( .stderr(predicate::str::contains(
"rate to be less than unsigned INT_MAX.", "rate to be less than unsigned INT_MAX.",
)); ));
Ok(()) Ok(())
} }
#[test] #[test]
fn test_negative_rate() -> Result<(), Box<dyn std::error::Error>> { fn test_negative_rate() -> Result<(), Box<dyn std::error::Error>> {
// This test should auto fail due to how clap works // This test should auto fail due to how clap works
Command::new(get_os_binary_loc()) Command::new(get_os_binary_loc())
.arg("-r") .arg("-r")
.arg("-1000") .arg("-1000")
.assert() .assert()
.failure() .failure()
.stderr(predicate::str::contains( .stderr(predicate::str::contains(
"wasn't expected, or isn't valid in this context", "wasn't expected, or isn't valid in this context",
)); ));
Ok(()) Ok(())
} }
#[test] #[test]
fn test_invalid_rate() -> Result<(), Box<dyn std::error::Error>> { fn test_invalid_rate() -> Result<(), Box<dyn std::error::Error>> {
Command::new(get_os_binary_loc()) Command::new(get_os_binary_loc())
.arg("-r") .arg("-r")
.arg("100-1000") .arg("100-1000")
.assert() .assert()
.failure() .failure()
.stderr(predicate::str::contains("invalid digit")); .stderr(predicate::str::contains("invalid digit"));
Ok(()) Ok(())
} }