Optimize imports and refactoring using clion (#56)

* Optimized imports as per clion

* Updated rustfmt to remove nightly-only features (for now)

* rustfmt

* [skip travis] Update .gitignore to ignore .idea

* Some more suggestions based on clion.  Mostly removing brackets.
This commit is contained in:
Clement Tsang 2020-02-28 22:59:17 -05:00 committed by GitHub
commit 27a04b9dd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 90 additions and 76 deletions

1
.gitignore vendored
View file

@ -15,5 +15,6 @@ Cargo.lock
rust-unmangle rust-unmangle
*.svg *.svg
*.data *.data
.idea/
sample_configs/testing.toml sample_configs/testing.toml

View file

@ -3,11 +3,7 @@ max_width = 100
newline_style = "Unix" newline_style = "Unix"
reorder_imports = true reorder_imports = true
fn_args_layout = "Compressed" fn_args_layout = "Compressed"
empty_item_single_line = false
hard_tabs = true hard_tabs = true
merge_imports = true
merge_derives = true merge_derives = true
reorder_modules = true reorder_modules = true
reorder_impl_items = true
tab_spaces = 4 tab_spaces = 4
format_strings = true

View file

@ -1,16 +1,17 @@
pub mod data_harvester;
use data_harvester::{processes, temperature};
use std::time::Instant; use std::time::Instant;
pub mod data_farmer;
use data_farmer::*;
use crate::{canvas, constants, utils::error::Result};
mod process_killer;
use unicode_segmentation::GraphemeCursor; use unicode_segmentation::GraphemeCursor;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use data_farmer::*;
use data_harvester::{processes, temperature};
use crate::{canvas, constants, utils::error::Result};
pub mod data_farmer;
pub mod data_harvester;
mod process_killer;
const MAX_SEARCH_LENGTH: usize = 200; const MAX_SEARCH_LENGTH: usize = 200;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -1031,7 +1032,7 @@ impl App {
pub fn kill_highlighted_process(&mut self) -> Result<()> { pub fn kill_highlighted_process(&mut self) -> Result<()> {
// Technically unnecessary but this is a good check... // Technically unnecessary but this is a good check...
if let WidgetPosition::Process = self.current_widget_selected { if let WidgetPosition::Process = self.current_widget_selected {
if let Some(current_selected_processes) = &(self.to_delete_process_list) { if let Some(current_selected_processes) = &self.to_delete_process_list {
for pid in &current_selected_processes.1 { for pid in &current_selected_processes.1 {
process_killer::kill_process_given_pid(*pid)?; process_killer::kill_process_given_pid(*pid)?;
} }

View file

@ -1,4 +1,3 @@
use crate::data_harvester::{cpu, disks, mem, network, processes, temperature, Data};
/// In charge of cleaning, processing, and managing data. I couldn't think of /// In charge of cleaning, processing, and managing data. I couldn't think of
/// a better name for the file. Since I called data collection "harvesting", /// a better name for the file. Since I called data collection "harvesting",
/// then this is the farmer I guess. /// then this is the farmer I guess.
@ -16,6 +15,8 @@ use crate::data_harvester::{cpu, disks, mem, network, processes, temperature, Da
use std::time::Instant; use std::time::Instant;
use std::vec::Vec; use std::vec::Vec;
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;
pub type JoinedDataPoints = (Value, Vec<(TimeOffset, Value)>); pub type JoinedDataPoints = (Value, Vec<(TimeOffset, Value)>);

View file

@ -1,6 +1,7 @@
//! This is the main file to house data collection functions. //! This is the main file to house data collection functions.
use std::{collections::HashMap, time::Instant}; use std::{collections::HashMap, time::Instant};
use sysinfo::{System, SystemExt}; use sysinfo::{System, SystemExt};
pub mod cpu; pub mod cpu;

View file

@ -1,7 +1,8 @@
use std::time::Instant;
use futures::StreamExt; use futures::StreamExt;
use heim::net; use heim::net;
use heim::units::information::byte; use heim::units::information::byte;
use std::time::Instant;
use sysinfo::{NetworkExt, System, SystemExt}; use sysinfo::{NetworkExt, System, SystemExt};
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]

View file

@ -1,10 +1,13 @@
use crate::utils::error;
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};
use crate::utils::error;
#[derive(Clone)] #[derive(Clone)]
pub enum ProcessSorting { pub enum ProcessSorting {
CPU, CPU,

View file

@ -1,6 +1,7 @@
use std::cmp::Ordering;
use futures::StreamExt; use futures::StreamExt;
use heim::units::thermodynamic_temperature; use heim::units::thermodynamic_temperature;
use std::cmp::Ordering;
use sysinfo::{ComponentExt, System, SystemExt}; use sysinfo::{ComponentExt, System, SystemExt};
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]

View file

@ -1,5 +1,3 @@
/// This file is meant to house (OS specific) implementations on how to kill processes.
use crate::utils::error::BottomError;
use std::process::Command; use std::process::Command;
// Copied from SO: https://stackoverflow.com/a/55231715 // Copied from SO: https://stackoverflow.com/a/55231715
@ -12,6 +10,9 @@ use winapi::{
}, },
}; };
/// This file is meant to house (OS specific) implementations on how to kill processes.
use crate::utils::error::BottomError;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
struct Process(HANDLE); struct Process(HANDLE);

View file

@ -1,11 +1,6 @@
use crate::{
app::{self, data_harvester::processes::ProcessHarvest, WidgetPosition},
constants::*,
data_conversion::{ConvertedCpuData, ConvertedProcessData},
utils::error,
};
use std::cmp::max; 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},
@ -17,12 +12,19 @@ use tui::{
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
mod canvas_colours;
use canvas_colours::*; use canvas_colours::*;
mod drawing_utils;
use drawing_utils::*; use drawing_utils::*;
use crate::{
app::{self, data_harvester::processes::ProcessHarvest, WidgetPosition},
constants::*,
data_conversion::{ConvertedCpuData, ConvertedProcessData},
utils::error,
};
mod canvas_colours;
mod drawing_utils;
// Headers // Headers
const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"]; const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"];
const CPU_SELECT_LEGEND_HEADER: [&str; 2] = ["CPU", "Show (Space)"]; const CPU_SELECT_LEGEND_HEADER: [&str; 2] = ["CPU", "Show (Space)"];
@ -620,12 +622,12 @@ impl Painter {
fn draw_cpu_legend<B: backend::Backend>( fn draw_cpu_legend<B: backend::Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect, &self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect,
) { ) {
let cpu_data: &[ConvertedCpuData] = &(app_state.canvas_data.cpu_data); let cpu_data: &[ConvertedCpuData] = &app_state.canvas_data.cpu_data;
let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64; let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64;
let start_position = get_start_position( let start_position = get_start_position(
num_rows, num_rows,
&(app_state.app_scroll_positions.scroll_direction), &app_state.app_scroll_positions.scroll_direction,
&mut app_state &mut app_state
.app_scroll_positions .app_scroll_positions
.cpu_scroll_state .cpu_scroll_state
@ -766,8 +768,8 @@ impl Painter {
fn draw_memory_graph<B: backend::Backend>( fn draw_memory_graph<B: backend::Backend>(
&self, f: &mut Frame<'_, B>, app_state: &app::App, draw_loc: Rect, &self, f: &mut Frame<'_, B>, app_state: &app::App, draw_loc: Rect,
) { ) {
let mem_data: &[(f64, f64)] = &(app_state.canvas_data.mem_data); let mem_data: &[(f64, f64)] = &app_state.canvas_data.mem_data;
let swap_data: &[(f64, f64)] = &(app_state.canvas_data.swap_data); let swap_data: &[(f64, f64)] = &app_state.canvas_data.swap_data;
let x_axis: Axis<'_, String> = Axis::default().bounds([0.0, TIME_STARTS_FROM as f64]); let x_axis: Axis<'_, String> = Axis::default().bounds([0.0, TIME_STARTS_FROM as f64]);
@ -839,8 +841,8 @@ impl Painter {
fn draw_network_graph<B: backend::Backend>( fn draw_network_graph<B: backend::Backend>(
&self, f: &mut Frame<'_, B>, app_state: &app::App, draw_loc: Rect, &self, f: &mut Frame<'_, B>, app_state: &app::App, draw_loc: Rect,
) { ) {
let network_data_rx: &[(f64, f64)] = &(app_state.canvas_data.network_data_rx); let network_data_rx: &[(f64, f64)] = &app_state.canvas_data.network_data_rx;
let network_data_tx: &[(f64, f64)] = &(app_state.canvas_data.network_data_tx); let network_data_tx: &[(f64, f64)] = &app_state.canvas_data.network_data_tx;
let x_axis: Axis<'_, String> = Axis::default().bounds([0.0, 60_000.0]); let x_axis: Axis<'_, String> = Axis::default().bounds([0.0, 60_000.0]);
let y_axis: Axis<'_, &str> = Axis::default() let y_axis: Axis<'_, &str> = Axis::default()
@ -938,7 +940,7 @@ impl Painter {
// Calculate widths // Calculate widths
let width_ratios: Vec<f64> = vec![0.25, 0.25, 0.25, 0.25]; let width_ratios: Vec<f64> = vec![0.25, 0.25, 0.25, 0.25];
let lens: &Vec<usize> = &NETWORK_HEADERS_LENS; let lens: &[usize] = &NETWORK_HEADERS_LENS;
let width = f64::from(draw_loc.width); let width = f64::from(draw_loc.width);
let variable_intrinsic_results = let variable_intrinsic_results =
@ -967,12 +969,12 @@ impl Painter {
fn draw_temp_table<B: backend::Backend>( fn draw_temp_table<B: backend::Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect, &self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect,
) { ) {
let temp_sensor_data: &[Vec<String>] = &(app_state.canvas_data.temp_sensor_data); let temp_sensor_data: &[Vec<String>] = &app_state.canvas_data.temp_sensor_data;
let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64; let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64;
let start_position = get_start_position( let start_position = get_start_position(
num_rows, num_rows,
&(app_state.app_scroll_positions.scroll_direction), &app_state.app_scroll_positions.scroll_direction,
&mut app_state &mut app_state
.app_scroll_positions .app_scroll_positions
.temp_scroll_state .temp_scroll_state
@ -984,7 +986,7 @@ impl Painter {
app_state.is_resized, app_state.is_resized,
); );
let sliced_vec = &(temp_sensor_data[start_position as usize..]); let sliced_vec = &temp_sensor_data[start_position as usize..];
let mut temp_row_counter: i64 = 0; let mut temp_row_counter: i64 = 0;
let temperature_rows = sliced_vec.iter().map(|temp_row| { let temperature_rows = sliced_vec.iter().map(|temp_row| {
@ -1064,11 +1066,11 @@ impl Painter {
fn draw_disk_table<B: backend::Backend>( fn draw_disk_table<B: backend::Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect, &self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect,
) { ) {
let disk_data: &[Vec<String>] = &(app_state.canvas_data.disk_data); let disk_data: &[Vec<String>] = &app_state.canvas_data.disk_data;
let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64; let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64;
let start_position = get_start_position( let start_position = get_start_position(
num_rows, num_rows,
&(app_state.app_scroll_positions.scroll_direction), &app_state.app_scroll_positions.scroll_direction,
&mut app_state &mut app_state
.app_scroll_positions .app_scroll_positions
.disk_scroll_state .disk_scroll_state
@ -1350,7 +1352,7 @@ impl Painter {
let position = get_start_position( let position = get_start_position(
num_rows, num_rows,
&(app_state.app_scroll_positions.scroll_direction), &app_state.app_scroll_positions.scroll_direction,
&mut app_state &mut app_state
.app_scroll_positions .app_scroll_positions
.process_scroll_state .process_scroll_state
@ -1369,7 +1371,7 @@ impl Painter {
position position
}; };
let sliced_vec = &(process_data[start_position as usize..]); let sliced_vec = &process_data[start_position as usize..];
let mut process_counter: i64 = 0; let mut process_counter: i64 = 0;
// Draw! // Draw!

View file

@ -1,9 +1,11 @@
mod colour_utils;
use colour_utils::*;
use tui::style::{Color, Modifier, Style}; use tui::style::{Color, Modifier, Style};
use colour_utils::*;
use crate::{constants::*, utils::error}; use crate::{constants::*, utils::error};
mod colour_utils;
pub struct CanvasColours { pub struct CanvasColours {
pub currently_selected_text_colour: Color, pub currently_selected_text_colour: Color,
pub currently_selected_bg_colour: Color, pub currently_selected_bg_colour: Color,

View file

@ -1,7 +1,9 @@
use crate::utils::{error, gen_util::*};
use std::collections::HashMap; use std::collections::HashMap;
use tui::style::{Color, Style}; use tui::style::{Color, Style};
use crate::utils::{error, gen_util::*};
const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long) const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long)
pub const STANDARD_FIRST_COLOUR: Color = Color::LightMagenta; pub const STANDARD_FIRST_COLOUR: Color = Color::LightMagenta;
pub const STANDARD_SECOND_COLOUR: Color = Color::LightYellow; pub const STANDARD_SECOND_COLOUR: Color = Color::LightYellow;

View file

@ -1,6 +1,10 @@
//! This mainly concerns converting collected data into things that the canvas //! This mainly concerns converting collected data into things that the canvas
//! can actually handle. //! can actually handle.
use std::collections::HashMap;
use constants::*;
use crate::{ use crate::{
app::{ app::{
data_farmer, data_farmer,
@ -10,8 +14,6 @@ use crate::{
constants, constants,
utils::gen_util::{get_exact_byte_values, get_simple_byte_values}, utils::gen_util::{get_exact_byte_values, get_simple_byte_values},
}; };
use constants::*;
use std::collections::HashMap;
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct ConvertedNetworkData { pub struct ConvertedNetworkData {

View file

@ -1,15 +1,22 @@
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]
#[macro_use]
extern crate log;
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
#[macro_use] #[macro_use]
extern crate futures;
#[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate futures; extern crate log;
use serde::Deserialize; use std::{
boxed::Box,
io::{stdout, Write},
panic::{self, PanicInfo},
sync::mpsc,
thread,
time::{Duration, Instant},
};
use crossterm::{ use crossterm::{
event::{ event::{
@ -21,17 +28,17 @@ use crossterm::{
terminal::LeaveAlternateScreen, terminal::LeaveAlternateScreen,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
}; };
use serde::Deserialize;
use std::{
boxed::Box,
io::{stdout, Write},
panic::{self, PanicInfo},
sync::mpsc,
thread,
time::{Duration, Instant},
};
use tui::{backend::CrosstermBackend, Terminal}; use tui::{backend::CrosstermBackend, Terminal};
use app::{
data_harvester::{self, processes::ProcessSorting},
App,
};
use constants::*;
use data_conversion::*;
use utils::error::{self, BottomError};
pub mod app; pub mod app;
mod utils { mod utils {
pub mod error; pub mod error;
@ -42,14 +49,6 @@ mod canvas;
mod constants; mod constants;
mod data_conversion; mod data_conversion;
use app::{
data_harvester::{self, processes::ProcessSorting},
App,
};
use constants::*;
use data_conversion::*;
use utils::error::{self, BottomError};
enum Event<I, J> { enum Event<I, J> {
KeyInput(I), KeyInput(I),
MouseInput(J), MouseInput(J),
@ -495,20 +494,20 @@ fn get_temperature_option(
} else if let Some(flags) = &config.flags { } else if let Some(flags) = &config.flags {
if let Some(temp_type) = &flags.temperature_type { if let Some(temp_type) = &flags.temperature_type {
// Give lowest priority to config. // Give lowest priority to config.
match temp_type.as_str() { return match temp_type.as_str() {
"fahrenheit" | "f" => { "fahrenheit" | "f" => {
return Ok(data_harvester::temperature::TemperatureType::Fahrenheit); Ok(data_harvester::temperature::TemperatureType::Fahrenheit)
} }
"kelvin" | "k" => { "kelvin" | "k" => {
return Ok(data_harvester::temperature::TemperatureType::Kelvin); Ok(data_harvester::temperature::TemperatureType::Kelvin)
} }
"celsius" | "c" => { "celsius" | "c" => {
return Ok(data_harvester::temperature::TemperatureType::Celsius); Ok(data_harvester::temperature::TemperatureType::Celsius)
} }
_ => { _ => {
return Err(BottomError::ConfigError( Err(BottomError::ConfigError(
"Invalid temperature type. Please have the value be of the form <kelvin|k|celsius|c|fahrenheit|f>".to_string() "Invalid temperature type. Please have the value be of the form <kelvin|k|celsius|c|fahrenheit|f>".to_string()
)); ))
} }
} }
} }
@ -714,7 +713,7 @@ fn generate_config_colours(config: &Config, painter: &mut canvas::Painter) -> er
painter.colours.set_avg_cpu_colour(avg_cpu_color)?; painter.colours.set_avg_cpu_colour(avg_cpu_color)?;
} }
if let Some(cpu_core_colors) = &(colours.cpu_core_colors) { if let Some(cpu_core_colors) = &colours.cpu_core_colors {
painter.colours.set_cpu_colours(cpu_core_colors)?; painter.colours.set_cpu_colours(cpu_core_colors)?;
} }

View file

@ -1,6 +1,7 @@
use std::process::Command;
use assert_cmd::prelude::*; use assert_cmd::prelude::*;
use predicates::prelude::*; use predicates::prelude::*;
use std::process::Command;
// These tests are mostly here just to ensure that invalid results will be caught when passing arguments... // These tests are mostly here just to ensure that invalid results will be caught when passing arguments...