other: Switch to once_cell (#324)

Switch from lazy_static to once_cell.
This commit is contained in:
Clement Tsang 2020-11-22 13:44:40 -08:00 committed by GitHub
parent 5abb1ce1a3
commit 6aa0dd64a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 233 additions and 211 deletions

2
Cargo.lock generated
View File

@ -125,9 +125,9 @@ dependencies = [
"heim",
"indexmap",
"itertools",
"lazy_static",
"libc",
"log",
"once_cell",
"predicates",
"regex",
"serde",

View File

@ -35,8 +35,8 @@ dirs-next = "2.0.0"
futures = "0.3.8"
indexmap = "1.6.0"
itertools = "0.9.0"
lazy_static = "1.4.0"
libc = "0.2"
once_cell = "1.5.2"
regex = "1.4.2"
serde = {version = "1.0", features = ["derive"] }
sysinfo = "0.15.3"

View File

@ -1,4 +1,3 @@
use lazy_static::lazy_static;
/// 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.
@ -13,6 +12,8 @@ use lazy_static::lazy_static;
/// call the purging function. Failure to do so *will* result in a growing
/// memory usage and higher CPU usage - you will be trying to process more and
/// more points as this is used!
use once_cell::sync::Lazy;
use std::{time::Instant, vec::Vec};
use crate::{
@ -245,9 +246,7 @@ impl DataCollection {
if let Some(trim) = device.name.split('/').last() {
let io_device = if cfg!(target_os = "macos") {
// Must trim one level further!
lazy_static! {
static ref DISK_REGEX: Regex = Regex::new(r"disk\d+").unwrap();
}
static DISK_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"disk\d+").unwrap());
if let Some(disk_trim) = DISK_REGEX.find(trim) {
io.get(disk_trim.as_str())
} else {

View File

@ -11,7 +11,7 @@ use std::collections::{hash_map::RandomState, HashMap};
#[cfg(not(target_os = "linux"))]
use sysinfo::{ProcessExt, ProcessorExt, System, SystemExt};
/// Maximum character length of a /proc/<PID>/stat process name.
/// Maximum character length of a /proc/<PID>/stat process name that we'll accept.
#[cfg(target_os = "linux")]
const MAX_STAT_NAME_LEN: usize = 15;

View File

@ -1,4 +1,4 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use tui::style::{Color, Style};
@ -14,8 +14,8 @@ pub const STANDARD_HIGHLIGHT_COLOUR: Color = Color::LightBlue;
pub const AVG_COLOUR: Color = Color::Red;
pub const ALL_COLOUR: Color = Color::Green;
lazy_static! {
static ref COLOR_NAME_LOOKUP_TABLE: HashMap<&'static str, Color> = [
static COLOR_NAME_LOOKUP_TABLE: Lazy<HashMap<&'static str, Color>> = Lazy::new(|| {
[
("reset", Color::Reset),
("black", Color::Black),
("red", Color::Red),
@ -33,12 +33,12 @@ lazy_static! {
("lightblue", Color::LightBlue),
("lightmagenta", Color::LightMagenta),
("lightcyan", Color::LightCyan),
("white", Color::White)
("white", Color::White),
]
.iter()
.copied()
.collect();
}
.collect()
});
pub fn convert_hex_to_color(hex: &str) -> error::Result<Color> {
fn hex_err(hex: &str) -> error::Result<u8> {

View File

@ -1,4 +1,4 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use std::borrow::Cow;
use unicode_segmentation::UnicodeSegmentation;
@ -26,12 +26,12 @@ const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"];
const AVG_POSITION: usize = 1;
const ALL_POSITION: usize = 0;
lazy_static! {
static ref CPU_LEGEND_HEADER_LENS: Vec<u16> = CPU_LEGEND_HEADER
static CPU_LEGEND_HEADER_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
CPU_LEGEND_HEADER
.iter()
.map(|entry| entry.len() as u16)
.collect::<Vec<_>>();
}
.collect::<Vec<_>>()
});
pub trait CpuGraphWidget {
fn draw_cpu<B: Backend>(

View File

@ -1,4 +1,4 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
@ -21,12 +21,12 @@ use unicode_segmentation::UnicodeSegmentation;
const DISK_HEADERS: [&str; 7] = ["Disk", "Mount", "Used", "Free", "Total", "R/s", "W/s"];
lazy_static! {
static ref DISK_HEADERS_LENS: Vec<u16> = DISK_HEADERS
static DISK_HEADERS_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
DISK_HEADERS
.iter()
.map(|entry| entry.len() as u16)
.collect::<Vec<_>>();
}
.collect::<Vec<_>>()
});
pub trait DiskTableWidget {
fn draw_disk_table<B: Backend>(

View File

@ -1,4 +1,4 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use std::cmp::max;
use unicode_segmentation::UnicodeSegmentation;
@ -21,12 +21,12 @@ use tui::{
const NETWORK_HEADERS: [&str; 4] = ["RX", "TX", "Total RX", "Total TX"];
lazy_static! {
static ref NETWORK_HEADERS_LENS: Vec<u16> = NETWORK_HEADERS
static NETWORK_HEADERS_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
NETWORK_HEADERS
.iter()
.map(|entry| entry.len() as u16)
.collect::<Vec<_>>();
}
.collect::<Vec<_>>()
});
pub trait NetworkGraphWidget {
fn draw_network<B: Backend>(

View File

@ -19,6 +19,81 @@ use std::borrow::Cow;
use unicode_segmentation::{GraphemeIndices, UnicodeSegmentation};
use unicode_width::UnicodeWidthStr;
use once_cell::sync::Lazy;
static PROCESS_HEADERS_HARD_WIDTH_NO_GROUP: Lazy<Vec<Option<u16>>> = Lazy::new(|| {
vec![
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
]
});
static PROCESS_HEADERS_HARD_WIDTH_GROUPED: Lazy<Vec<Option<u16>>> = Lazy::new(|| {
vec![
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
None,
]
});
static PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_COMMAND: Lazy<Vec<Option<f64>>> =
Lazy::new(|| vec![None, Some(0.7), None, None, None, None, None, None]);
static PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_TREE: Lazy<Vec<Option<f64>>> =
Lazy::new(|| vec![None, Some(0.5), None, None, None, None, None, None]);
static PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_ELSE: Lazy<Vec<Option<f64>>> =
Lazy::new(|| vec![None, Some(0.4), None, None, None, None, None, None]);
static PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_COMMAND: Lazy<Vec<Option<f64>>> = Lazy::new(|| {
vec![
None,
Some(0.7),
None,
None,
None,
None,
None,
None,
Some(0.2),
]
});
static PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_TREE: Lazy<Vec<Option<f64>>> = Lazy::new(|| {
vec![
None,
Some(0.5),
None,
None,
None,
None,
None,
None,
Some(0.2),
]
});
static PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_ELSE: Lazy<Vec<Option<f64>>> = Lazy::new(|| {
vec![
None,
Some(0.3),
None,
None,
None,
None,
None,
None,
Some(0.2),
]
});
pub trait ProcessTableWidget {
/// Draws and handles all process-related drawing. Use this.
/// - `widget_id` here represents the widget ID of the process widget itself!
@ -222,28 +297,9 @@ impl ProcessTableWidget for Painter {
// Calculate widths
let hard_widths = if proc_widget_state.is_grouped {
vec![
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
]
&*PROCESS_HEADERS_HARD_WIDTH_GROUPED
} else {
vec![
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
None,
]
&*PROCESS_HEADERS_HARD_WIDTH_NO_GROUP
};
if recalculate_column_widths {
@ -274,7 +330,7 @@ impl ProcessTableWidget for Painter {
.table_width_state
.desired_column_widths
.iter()
.zip(&hard_widths)
.zip(hard_widths)
.map(|(current, hard)| {
if let Some(hard) = hard {
if *hard > *current {
@ -290,48 +346,18 @@ impl ProcessTableWidget for Painter {
let soft_widths_max = if proc_widget_state.is_grouped {
if proc_widget_state.is_using_command {
vec![None, Some(0.7), None, None, None, None, None, None]
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_COMMAND
} else if proc_widget_state.is_tree_mode {
vec![None, Some(0.5), None, None, None, None, None, None]
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_TREE
} else {
vec![None, Some(0.4), None, None, None, None, None, None]
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_ELSE
}
} else if proc_widget_state.is_using_command {
vec![
None,
Some(0.7),
None,
None,
None,
None,
None,
None,
Some(0.2),
]
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_COMMAND
} else if proc_widget_state.is_tree_mode {
vec![
None,
Some(0.5),
None,
None,
None,
None,
None,
None,
Some(0.2),
]
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_TREE
} else {
vec![
None,
Some(0.3),
None,
None,
None,
None,
None,
None,
Some(0.2),
]
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_ELSE
};
proc_widget_state.table_width_state.calculated_column_widths =
@ -339,7 +365,7 @@ impl ProcessTableWidget for Painter {
draw_loc.width,
&hard_widths,
&soft_widths_min,
&soft_widths_max,
soft_widths_max,
&(proc_widget_state
.table_width_state
.desired_column_widths
@ -363,7 +389,7 @@ impl ProcessTableWidget for Painter {
let ccw = &proc_widget_state.table_width_state.calculated_column_widths;
let process_rows = sliced_vec.iter().map(|(data, disabled)| {
let truncated_data = data.iter().zip(&hard_widths).enumerate().map(
let truncated_data = data.iter().zip(hard_widths).enumerate().map(
|(itx, ((entry, alternative), width))| {
if let (Some(desired_col_width), Some(calculated_col_width)) =
(dcw.get(itx), ccw.get(itx))

View File

@ -1,4 +1,4 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
@ -21,12 +21,13 @@ use unicode_segmentation::UnicodeSegmentation;
const TEMP_HEADERS: [&str; 2] = ["Sensor", "Temp"];
lazy_static! {
static ref TEMP_HEADERS_LENS: Vec<u16> = TEMP_HEADERS
static TEMP_HEADERS_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
TEMP_HEADERS
.iter()
.map(|entry| entry.len() as u16)
.collect::<Vec<_>>();
}
.collect::<Vec<_>>()
});
pub trait TempTableWidget {
fn draw_temp_table<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect, draw_border: bool,

View File

@ -1,6 +1,5 @@
use lazy_static::lazy_static;
use crate::options::ConfigColours;
use once_cell::sync::Lazy;
// Default widget ID
pub const DEFAULT_WIDGET_ID: u64 = 56709;
@ -24,120 +23,116 @@ pub const TABLE_GAP_HEIGHT_LIMIT: u16 = 7;
pub const TIME_LABEL_HEIGHT_LIMIT: u16 = 7;
// Side borders
lazy_static! {
pub static ref SIDE_BORDERS: tui::widgets::Borders =
tui::widgets::Borders::from_bits_truncate(20);
pub static ref TOP_LEFT_RIGHT: tui::widgets::Borders =
tui::widgets::Borders::from_bits_truncate(22);
pub static ref BOTTOM_LEFT_RIGHT: tui::widgets::Borders =
tui::widgets::Borders::from_bits_truncate(28);
pub static ref DEFAULT_TEXT_STYLE: tui::style::Style =
tui::style::Style::default().fg(tui::style::Color::Gray);
pub static ref DEFAULT_HEADER_STYLE: tui::style::Style =
tui::style::Style::default().fg(tui::style::Color::LightBlue);
}
pub static SIDE_BORDERS: Lazy<tui::widgets::Borders> =
Lazy::new(|| tui::widgets::Borders::from_bits_truncate(20));
pub static TOP_LEFT_RIGHT: Lazy<tui::widgets::Borders> =
Lazy::new(|| tui::widgets::Borders::from_bits_truncate(22));
pub static BOTTOM_LEFT_RIGHT: Lazy<tui::widgets::Borders> =
Lazy::new(|| tui::widgets::Borders::from_bits_truncate(28));
pub static DEFAULT_TEXT_STYLE: Lazy<tui::style::Style> =
Lazy::new(|| tui::style::Style::default().fg(tui::style::Color::Gray));
pub static DEFAULT_HEADER_STYLE: Lazy<tui::style::Style> =
Lazy::new(|| tui::style::Style::default().fg(tui::style::Color::LightBlue));
// Colour profiles
lazy_static! {
pub static ref DEFAULT_LIGHT_MODE_COLOUR_PALETTE: ConfigColours = ConfigColours {
text_color: Some("black".to_string()),
border_color: Some("black".to_string()),
table_header_color: Some("black".to_string()),
widget_title_color: Some("black".to_string()),
selected_text_color: Some("white".to_string()),
graph_color: Some("black".to_string()),
disabled_text_color: Some("gray".to_string()),
..ConfigColours::default()
};
pub static ref GRUVBOX_COLOUR_PALETTE: ConfigColours = ConfigColours {
table_header_color: Some("#83a598".to_string()),
all_cpu_color: Some("#8ec07c".to_string()),
avg_cpu_color: Some("#fb4934".to_string()),
cpu_core_colors: Some(vec![
"#cc241d".to_string(),
"#98971a".to_string(),
"#d79921".to_string(),
"#458588".to_string(),
"#b16286".to_string(),
"#689d6a".to_string(),
"#fe8019".to_string(),
"#b8bb26".to_string(),
"#fabd2f".to_string(),
"#83a598".to_string(),
"#d3869b".to_string(),
"#d65d0e".to_string(),
"#9d0006".to_string(),
"#79740e".to_string(),
"#b57614".to_string(),
"#076678".to_string(),
"#8f3f71".to_string(),
"#427b58".to_string(),
"#d65d03".to_string(),
"#af3a03".to_string(),
]),
ram_color: Some("#8ec07c".to_string()),
swap_color: Some("#fabd2f".to_string()),
rx_color: Some("#8ec07c".to_string()),
tx_color: Some("#fabd2f".to_string()),
rx_total_color: Some("#689d6a".to_string()),
tx_total_color: Some("#d79921".to_string()),
border_color: Some("#ebdbb2".to_string()),
highlighted_border_color: Some("#fe8019".to_string()),
disabled_text_color: Some("#665c54".to_string()),
text_color: Some("#ebdbb2".to_string()),
selected_text_color: Some("#1d2021".to_string()),
selected_bg_color: Some("#ebdbb2".to_string()),
widget_title_color: Some("#ebdbb2".to_string()),
graph_color: Some("#ebdbb2".to_string()),
high_battery_color: Some("#98971a".to_string()),
medium_battery_color: Some("#fabd2f".to_string()),
low_battery_color: Some("#fb4934".to_string())
};
pub static ref GRUVBOX_LIGHT_COLOUR_PALETTE: ConfigColours = ConfigColours {
table_header_color: Some("#076678".to_string()),
all_cpu_color: Some("#8ec07c".to_string()),
avg_cpu_color: Some("#fb4934".to_string()),
cpu_core_colors: Some(vec![
"#cc241d".to_string(),
"#98971a".to_string(),
"#d79921".to_string(),
"#458588".to_string(),
"#b16286".to_string(),
"#689d6a".to_string(),
"#fe8019".to_string(),
"#b8bb26".to_string(),
"#fabd2f".to_string(),
"#83a598".to_string(),
"#d3869b".to_string(),
"#d65d0e".to_string(),
"#9d0006".to_string(),
"#79740e".to_string(),
"#b57614".to_string(),
"#076678".to_string(),
"#8f3f71".to_string(),
"#427b58".to_string(),
"#d65d03".to_string(),
"#af3a03".to_string(),
]),
ram_color: Some("#427b58".to_string()),
swap_color: Some("#cc241d".to_string()),
rx_color: Some("#427b58".to_string()),
tx_color: Some("#cc241d".to_string()),
rx_total_color: Some("#689d6a".to_string()),
tx_total_color: Some("#9d0006".to_string()),
border_color: Some("#3c3836".to_string()),
highlighted_border_color: Some("#af3a03".to_string()),
disabled_text_color: Some("#d5c4a1".to_string()),
text_color: Some("#3c3836".to_string()),
selected_text_color: Some("#ebdbb2".to_string()),
selected_bg_color: Some("#3c3836".to_string()),
widget_title_color: Some("#3c3836".to_string()),
graph_color: Some("#3c3836".to_string()),
high_battery_color: Some("#98971a".to_string()),
medium_battery_color: Some("#d79921".to_string()),
low_battery_color: Some("#cc241d".to_string())
};
}
pub static DEFAULT_LIGHT_MODE_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
text_color: Some("black".to_string()),
border_color: Some("black".to_string()),
table_header_color: Some("black".to_string()),
widget_title_color: Some("black".to_string()),
selected_text_color: Some("white".to_string()),
graph_color: Some("black".to_string()),
disabled_text_color: Some("gray".to_string()),
..ConfigColours::default()
});
pub static GRUVBOX_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
table_header_color: Some("#83a598".to_string()),
all_cpu_color: Some("#8ec07c".to_string()),
avg_cpu_color: Some("#fb4934".to_string()),
cpu_core_colors: Some(vec![
"#cc241d".to_string(),
"#98971a".to_string(),
"#d79921".to_string(),
"#458588".to_string(),
"#b16286".to_string(),
"#689d6a".to_string(),
"#fe8019".to_string(),
"#b8bb26".to_string(),
"#fabd2f".to_string(),
"#83a598".to_string(),
"#d3869b".to_string(),
"#d65d0e".to_string(),
"#9d0006".to_string(),
"#79740e".to_string(),
"#b57614".to_string(),
"#076678".to_string(),
"#8f3f71".to_string(),
"#427b58".to_string(),
"#d65d03".to_string(),
"#af3a03".to_string(),
]),
ram_color: Some("#8ec07c".to_string()),
swap_color: Some("#fabd2f".to_string()),
rx_color: Some("#8ec07c".to_string()),
tx_color: Some("#fabd2f".to_string()),
rx_total_color: Some("#689d6a".to_string()),
tx_total_color: Some("#d79921".to_string()),
border_color: Some("#ebdbb2".to_string()),
highlighted_border_color: Some("#fe8019".to_string()),
disabled_text_color: Some("#665c54".to_string()),
text_color: Some("#ebdbb2".to_string()),
selected_text_color: Some("#1d2021".to_string()),
selected_bg_color: Some("#ebdbb2".to_string()),
widget_title_color: Some("#ebdbb2".to_string()),
graph_color: Some("#ebdbb2".to_string()),
high_battery_color: Some("#98971a".to_string()),
medium_battery_color: Some("#fabd2f".to_string()),
low_battery_color: Some("#fb4934".to_string()),
});
pub static GRUVBOX_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
table_header_color: Some("#076678".to_string()),
all_cpu_color: Some("#8ec07c".to_string()),
avg_cpu_color: Some("#fb4934".to_string()),
cpu_core_colors: Some(vec![
"#cc241d".to_string(),
"#98971a".to_string(),
"#d79921".to_string(),
"#458588".to_string(),
"#b16286".to_string(),
"#689d6a".to_string(),
"#fe8019".to_string(),
"#b8bb26".to_string(),
"#fabd2f".to_string(),
"#83a598".to_string(),
"#d3869b".to_string(),
"#d65d0e".to_string(),
"#9d0006".to_string(),
"#79740e".to_string(),
"#b57614".to_string(),
"#076678".to_string(),
"#8f3f71".to_string(),
"#427b58".to_string(),
"#d65d03".to_string(),
"#af3a03".to_string(),
]),
ram_color: Some("#427b58".to_string()),
swap_color: Some("#cc241d".to_string()),
rx_color: Some("#427b58".to_string()),
tx_color: Some("#cc241d".to_string()),
rx_total_color: Some("#689d6a".to_string()),
tx_total_color: Some("#9d0006".to_string()),
border_color: Some("#3c3836".to_string()),
highlighted_border_color: Some("#af3a03".to_string()),
disabled_text_color: Some("#d5c4a1".to_string()),
text_color: Some("#3c3836".to_string()),
selected_text_color: Some("#ebdbb2".to_string()),
selected_bg_color: Some("#3c3836".to_string()),
widget_title_color: Some("#3c3836".to_string()),
graph_color: Some("#3c3836".to_string()),
high_battery_color: Some("#98971a".to_string()),
medium_battery_color: Some("#d79921".to_string()),
low_battery_color: Some("#cc241d".to_string()),
});
// Help text
pub const HELP_CONTENTS_TEXT: [&str; 8] = [
@ -277,8 +272,8 @@ pub const BASIC_MEM_HELP_TEXT: [&str; 2] = [
"% Toggle between values and percentages for memory usage",
];
lazy_static! {
pub static ref HELP_TEXT: Vec<Vec<&'static str>> = vec![
pub static HELP_TEXT: Lazy<Vec<Vec<&'static str>>> = Lazy::new(|| {
vec![
HELP_CONTENTS_TEXT.to_vec(),
GENERAL_HELP_TEXT.to_vec(),
CPU_HELP_TEXT.to_vec(),
@ -287,8 +282,8 @@ lazy_static! {
SORT_HELP_TEXT.to_vec(),
BATTERY_HELP_TEXT.to_vec(),
BASIC_MEM_HELP_TEXT.to_vec(),
];
}
]
});
// Default layouts
pub const DEFAULT_LAYOUT: &str = r##"

View File

@ -62,6 +62,7 @@ pub struct ConvertedProcessData {
pub tw_f64: f64,
pub process_state: String,
pub process_char: char,
/// Prefix printed before the process when displayed.
pub process_description_prefix: Option<String>,
/// Whether to mark this process entry as disabled (mostly for tree mode).