refactor: clippy and small cleanup/comments (#865)

* refactor: small cleanup and comments in some code

* clippy

* more clippy

* fixes for macos/freebsd
This commit is contained in:
Clement Tsang 2022-11-04 04:17:22 -04:00 committed by GitHub
parent d56b551e39
commit 76c3bc4254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 27 additions and 34 deletions

View File

@ -8,7 +8,7 @@ use std::{
include!("src/clap.rs");
fn create_dir(dir: &Path) -> Result<()> {
let res = fs::create_dir_all(&dir);
let res = fs::create_dir_all(dir);
match &res {
Ok(()) => {}
Err(err) => {

View File

@ -839,7 +839,7 @@ impl App {
&self.current_widget.top_left_corner,
&self.current_widget.bottom_right_corner,
) {
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
let border_offset = u16::from(self.is_drawing_border());
let header_offset = self.header_offset(&self.current_widget);
let height = brc_y - tlc_y - 2 * border_offset - header_offset;
self.change_position_count(-(height as i64));
@ -867,7 +867,7 @@ impl App {
&self.current_widget.top_left_corner,
&self.current_widget.bottom_right_corner,
) {
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
let border_offset = u16::from(self.is_drawing_border());
let header_offset = self.header_offset(&self.current_widget);
let height = brc_y - tlc_y - 2 * border_offset - header_offset;
self.change_position_count(height as i64);
@ -886,7 +886,7 @@ impl App {
&self.current_widget.top_left_corner,
&self.current_widget.bottom_right_corner,
) {
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
let border_offset = u16::from(self.is_drawing_border());
let header_offset = self.header_offset(&self.current_widget);
let height = brc_y - tlc_y - 2 * border_offset - header_offset;
self.change_position_count(-(height as i64) / 2);
@ -905,7 +905,7 @@ impl App {
&self.current_widget.top_left_corner,
&self.current_widget.bottom_right_corner,
) {
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
let border_offset = u16::from(self.is_drawing_border());
let header_offset = self.header_offset(&self.current_widget);
let height = brc_y - tlc_y - 2 * border_offset - header_offset;
self.change_position_count(height as i64 / 2);
@ -1330,13 +1330,13 @@ impl App {
pub fn kill_highlighted_process(&mut self) -> Result<()> {
if let BottomWidgetType::Proc = self.current_widget.widget_type {
if let Some(current_selected_processes) = &self.to_delete_process_list {
if let Some((_, pids)) = &self.to_delete_process_list {
#[cfg(target_family = "unix")]
let signal = match self.delete_dialog_state.selected_signal {
KillSignal::Kill(sig) => sig,
KillSignal::Cancel => 15, // should never happen, so just TERM
};
for pid in &current_selected_processes.1 {
for pid in pids {
#[cfg(target_family = "unix")]
{
process_killer::kill_process_given_pid(*pid, signal)?;
@ -2456,7 +2456,7 @@ impl App {
&self.current_widget.top_left_corner,
&self.current_widget.bottom_right_corner,
) {
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
let border_offset = u16::from(self.is_drawing_border());
// This check ensures the click isn't actually just clicking on the bottom border.
if y < (brc_y - border_offset) {
@ -2616,11 +2616,7 @@ impl App {
1 + self.app_config_fields.table_gap
} else {
let min_height_for_header = if self.is_drawing_border() { 3 } else { 1 };
if height_diff > min_height_for_header {
1
} else {
0
}
u16::from(height_diff > min_height_for_header)
}
} else {
1 + self.app_config_fields.table_gap

View File

@ -99,7 +99,7 @@ fn matches_ignore_list(filter_check_map: &[(&Option<Filter>, &String)]) -> bool
fn get_disk_info() -> io::Result<StorageSystemInformation> {
let output = std::process::Command::new("df")
.args(&["--libxo", "json", "-k", "-t", "ufs,msdosfs,zfs"])
.args(["--libxo", "json", "-k", "-t", "ufs,msdosfs,zfs"])
.output()?;
deserialize_xo("storage-system-information", &output.stdout)
}

View File

@ -42,7 +42,7 @@ fn get_freebsd_process_cpu_usage(pids: &[i32]) -> io::Result<std::collections::H
}
let output = std::process::Command::new("ps")
.args(&["--libxo", "json", "-o", "pid,pcpu", "-p"])
.args(["--libxo", "json", "-o", "pid,pcpu", "-p"])
.args(pids.iter().map(i32::to_string))
.output()?;
deserialize_xo("process-information", &output.stdout).map(|process_info: ProcessInformation| {

View File

@ -29,7 +29,7 @@ fn get_macos_process_cpu_usage(
) -> std::io::Result<std::collections::HashMap<i32, f64>> {
use itertools::Itertools;
let output = std::process::Command::new("ps")
.args(&["-o", "pid=,pcpu=", "-p"])
.args(["-o", "pid=,pcpu=", "-p"])
.arg(
// Has to look like this since otherwise, it you hit a `unstable_name_collisions` warning.
Itertools::intersperse(pids.iter().map(i32::to_string), ",".to_string())

View File

@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::hash_map::Entry};
use std::borrow::Cow;
use crate::{
app::{
@ -499,7 +499,7 @@ impl ProcWidget {
let mut id_pid_map: FxHashMap<String, Vec<Pid>> = FxHashMap::default();
let mut filtered_data: Vec<ProcWidgetData> = if let ProcWidgetMode::Grouped = self.mode {
let mut id_process_mapping: FxHashMap<String, ProcessHarvest> = FxHashMap::default();
let mut id_process_mapping: FxHashMap<&String, ProcessHarvest> = FxHashMap::default();
for process in filtered_iter {
let id = if is_using_command {
&process.command
@ -508,19 +508,17 @@ impl ProcWidget {
};
let pid = process.pid;
match id_pid_map.entry(id.clone()) {
Entry::Occupied(mut occupied) => {
occupied.get_mut().push(pid);
}
Entry::Vacant(vacant) => {
vacant.insert(vec![pid]);
}
if let Some(entry) = id_pid_map.get_mut(id) {
entry.push(pid);
} else {
id_pid_map.insert(id.clone(), vec![pid]);
}
if let Some(grouped_process_harvest) = id_process_mapping.get_mut(id) {
grouped_process_harvest.add(process);
} else {
id_process_mapping.insert(id.clone(), process.clone());
// FIXME: [PERF] could maybe eliminate an allocation here in the grouped mode... or maybe just avoid the entire transformation step, making an alloc fine.
id_process_mapping.insert(id, process.clone());
}
}

View File

@ -100,19 +100,19 @@ impl CanvasColours {
match colour_scheme {
ColourScheme::Default => {}
ColourScheme::DefaultLight => {
canvas_colours.set_colours_from_palette(&*DEFAULT_LIGHT_MODE_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&DEFAULT_LIGHT_MODE_COLOUR_PALETTE)?;
}
ColourScheme::Gruvbox => {
canvas_colours.set_colours_from_palette(&*GRUVBOX_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&GRUVBOX_COLOUR_PALETTE)?;
}
ColourScheme::GruvboxLight => {
canvas_colours.set_colours_from_palette(&*GRUVBOX_LIGHT_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&GRUVBOX_LIGHT_COLOUR_PALETTE)?;
}
ColourScheme::Nord => {
canvas_colours.set_colours_from_palette(&*NORD_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&NORD_COLOUR_PALETTE)?;
}
ColourScheme::NordLight => {
canvas_colours.set_colours_from_palette(&*NORD_LIGHT_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&NORD_LIGHT_COLOUR_PALETTE)?;
}
ColourScheme::Custom => {
if let Some(colors) = &config.colors {

View File

@ -230,7 +230,7 @@ pub fn build_app() -> Command<'static> {
.long("color")
.takes_value(true)
.value_name("COLOR SCHEME")
.possible_values(&[
.possible_values([
"default",
"default-light",
"gruvbox",

View File

@ -194,7 +194,7 @@ where
}
let show_header = inner_height > 1;
let header_height = if show_header { 1 } else { 0 };
let header_height = u16::from(show_header);
let table_gap = if !show_header || draw_loc.height < TABLE_GAP_HEIGHT_LIMIT {
0
} else {

View File

@ -336,7 +336,6 @@ where
#[cfg(test)]
mod test {
use super::*;
#[derive(Clone, PartialEq, Eq, Debug)]