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
*.svg
*.data
.idea/
sample_configs/testing.toml

View file

@ -3,11 +3,7 @@ max_width = 100
newline_style = "Unix"
reorder_imports = true
fn_args_layout = "Compressed"
empty_item_single_line = false
hard_tabs = true
merge_imports = true
merge_derives = true
reorder_modules = true
reorder_impl_items = true
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;
pub mod data_farmer;
use data_farmer::*;
use crate::{canvas, constants, utils::error::Result};
mod process_killer;
use unicode_segmentation::GraphemeCursor;
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;
#[derive(Debug, Clone, Copy)]
@ -1031,7 +1032,7 @@ impl App {
pub fn kill_highlighted_process(&mut self) -> Result<()> {
// Technically unnecessary but this is a good check...
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 {
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
/// a better name for the file. Since I called data collection "harvesting",
/// 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::vec::Vec;
use crate::data_harvester::{cpu, disks, mem, network, processes, temperature, Data};
pub type TimeOffset = f64;
pub type Value = f64;
pub type JoinedDataPoints = (Value, Vec<(TimeOffset, Value)>);

View file

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

View file

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

View file

@ -1,10 +1,13 @@
use crate::utils::error;
use std::{
collections::{hash_map::RandomState, HashMap},
process::Command,
time::Instant,
};
use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt};
use crate::utils::error;
#[derive(Clone)]
pub enum ProcessSorting {
CPU,

View file

@ -1,6 +1,7 @@
use std::cmp::Ordering;
use futures::StreamExt;
use heim::units::thermodynamic_temperature;
use std::cmp::Ordering;
use sysinfo::{ComponentExt, System, SystemExt};
#[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;
// 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")]
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::collections::HashMap;
use tui::{
backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
@ -17,12 +12,19 @@ use tui::{
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
mod canvas_colours;
use canvas_colours::*;
mod 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
const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"];
const CPU_SELECT_LEGEND_HEADER: [&str; 2] = ["CPU", "Show (Space)"];
@ -620,12 +622,12 @@ impl Painter {
fn draw_cpu_legend<B: backend::Backend>(
&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 start_position = get_start_position(
num_rows,
&(app_state.app_scroll_positions.scroll_direction),
&app_state.app_scroll_positions.scroll_direction,
&mut app_state
.app_scroll_positions
.cpu_scroll_state
@ -766,8 +768,8 @@ impl Painter {
fn draw_memory_graph<B: backend::Backend>(
&self, f: &mut Frame<'_, B>, app_state: &app::App, draw_loc: Rect,
) {
let mem_data: &[(f64, f64)] = &(app_state.canvas_data.mem_data);
let swap_data: &[(f64, f64)] = &(app_state.canvas_data.swap_data);
let mem_data: &[(f64, f64)] = &app_state.canvas_data.mem_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]);
@ -839,8 +841,8 @@ impl Painter {
fn draw_network_graph<B: backend::Backend>(
&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_tx: &[(f64, f64)] = &(app_state.canvas_data.network_data_tx);
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 x_axis: Axis<'_, String> = Axis::default().bounds([0.0, 60_000.0]);
let y_axis: Axis<'_, &str> = Axis::default()
@ -938,7 +940,7 @@ impl Painter {
// Calculate widths
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 variable_intrinsic_results =
@ -967,12 +969,12 @@ impl Painter {
fn draw_temp_table<B: backend::Backend>(
&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 start_position = get_start_position(
num_rows,
&(app_state.app_scroll_positions.scroll_direction),
&app_state.app_scroll_positions.scroll_direction,
&mut app_state
.app_scroll_positions
.temp_scroll_state
@ -984,7 +986,7 @@ impl Painter {
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 temperature_rows = sliced_vec.iter().map(|temp_row| {
@ -1064,11 +1066,11 @@ impl Painter {
fn draw_disk_table<B: backend::Backend>(
&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 start_position = get_start_position(
num_rows,
&(app_state.app_scroll_positions.scroll_direction),
&app_state.app_scroll_positions.scroll_direction,
&mut app_state
.app_scroll_positions
.disk_scroll_state
@ -1350,7 +1352,7 @@ impl Painter {
let position = get_start_position(
num_rows,
&(app_state.app_scroll_positions.scroll_direction),
&app_state.app_scroll_positions.scroll_direction,
&mut app_state
.app_scroll_positions
.process_scroll_state
@ -1369,7 +1371,7 @@ impl Painter {
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;
// Draw!

View file

@ -1,9 +1,11 @@
mod colour_utils;
use colour_utils::*;
use tui::style::{Color, Modifier, Style};
use colour_utils::*;
use crate::{constants::*, utils::error};
mod colour_utils;
pub struct CanvasColours {
pub currently_selected_text_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 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)
pub const STANDARD_FIRST_COLOUR: Color = Color::LightMagenta;
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
//! can actually handle.
use std::collections::HashMap;
use constants::*;
use crate::{
app::{
data_farmer,
@ -10,8 +14,6 @@ use crate::{
constants,
utils::gen_util::{get_exact_byte_values, get_simple_byte_values},
};
use constants::*;
use std::collections::HashMap;
#[derive(Default, Debug)]
pub struct ConvertedNetworkData {

View file

@ -1,15 +1,22 @@
#![warn(rust_2018_idioms)]
#[macro_use]
extern crate log;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate futures;
#[macro_use]
extern crate lazy_static;
#[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::{
event::{
@ -21,17 +28,17 @@ use crossterm::{
terminal::LeaveAlternateScreen,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
};
use std::{
boxed::Box,
io::{stdout, Write},
panic::{self, PanicInfo},
sync::mpsc,
thread,
time::{Duration, Instant},
};
use serde::Deserialize;
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;
mod utils {
pub mod error;
@ -42,14 +49,6 @@ mod canvas;
mod constants;
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> {
KeyInput(I),
MouseInput(J),
@ -495,20 +494,20 @@ fn get_temperature_option(
} else if let Some(flags) = &config.flags {
if let Some(temp_type) = &flags.temperature_type {
// Give lowest priority to config.
match temp_type.as_str() {
return match temp_type.as_str() {
"fahrenheit" | "f" => {
return Ok(data_harvester::temperature::TemperatureType::Fahrenheit);
Ok(data_harvester::temperature::TemperatureType::Fahrenheit)
}
"kelvin" | "k" => {
return Ok(data_harvester::temperature::TemperatureType::Kelvin);
Ok(data_harvester::temperature::TemperatureType::Kelvin)
}
"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()
));
))
}
}
}
@ -714,7 +713,7 @@ fn generate_config_colours(config: &Config, painter: &mut canvas::Painter) -> er
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)?;
}

View file

@ -1,6 +1,7 @@
use std::process::Command;
use assert_cmd::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...