mirror of
https://github.com/ClementTsang/bottom.git
synced 2024-09-11 03:35:35 +03:00
refactor: remove once_cell (#1361)
* refactor: remove once_cell * some missing fixes
This commit is contained in:
parent
854f3aed95
commit
a1168cac67
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -176,7 +176,6 @@ dependencies = [
|
||||
"log",
|
||||
"mach2",
|
||||
"nvml-wrapper",
|
||||
"once_cell",
|
||||
"predicates",
|
||||
"ratatui",
|
||||
"regex",
|
||||
|
@ -93,10 +93,7 @@ indexmap = "2.1.0"
|
||||
itertools = "0.12.0"
|
||||
kstring = { version = "2.0.0", features = ["arc"] }
|
||||
log = { version = "0.4.20", optional = true }
|
||||
nvml-wrapper = { version = "0.9.0", optional = true, features = [
|
||||
"legacy-functions",
|
||||
] }
|
||||
once_cell = "1.18.0"
|
||||
nvml-wrapper = { version = "0.9.0", optional = true, features = ["legacy-functions"] }
|
||||
regex = "1.10.2"
|
||||
serde = { version = "=1.0.193", features = ["derive"] }
|
||||
starship-battery = { version = "0.8.2", optional = true }
|
||||
@ -136,9 +133,7 @@ filedescriptor = "0.8.2"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_cmd = "2.0.12"
|
||||
cargo-husky = { version = "1.5.0", default-features = false, features = [
|
||||
"user-hooks",
|
||||
] }
|
||||
cargo-husky = { version = "1.5.0", default-features = false, features = ["user-hooks"] }
|
||||
predicates = "3.0.3"
|
||||
|
||||
[build-dependencies]
|
||||
|
@ -364,13 +364,12 @@ impl DataCollection {
|
||||
let io_device = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "macos")] {
|
||||
use once_cell::sync::Lazy;
|
||||
use std::sync::OnceLock;
|
||||
use regex::Regex;
|
||||
|
||||
// Must trim one level further for macOS!
|
||||
static DISK_REGEX: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r"disk\d+").unwrap());
|
||||
if let Some(new_name) = DISK_REGEX.find(checked_name) {
|
||||
static DISK_REGEX: OnceLock<Regex> = OnceLock::new();
|
||||
if let Some(new_name) = DISK_REGEX.get_or_init(|| Regex::new(r"disk\d+").unwrap()).find(checked_name) {
|
||||
io.get(new_name.as_str())
|
||||
} else {
|
||||
None
|
||||
|
@ -1,8 +1,9 @@
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use nvml_wrapper::enum_wrappers::device::TemperatureSensor;
|
||||
use nvml_wrapper::enums::device::UsedGpuMemory;
|
||||
use nvml_wrapper::{error::NvmlError, Nvml};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::app::Filter;
|
||||
|
||||
@ -10,7 +11,7 @@ use crate::app::layout_manager::UsedWidgets;
|
||||
use crate::data_harvester::memory::MemHarvest;
|
||||
use crate::data_harvester::temperature::{is_temp_filtered, TempHarvest, TemperatureType};
|
||||
|
||||
pub static NVML_DATA: Lazy<Result<Nvml, NvmlError>> = Lazy::new(Nvml::init);
|
||||
pub static NVML_DATA: OnceLock<Result<Nvml, NvmlError>> = OnceLock::new();
|
||||
|
||||
pub struct GpusData {
|
||||
pub memory: Option<Vec<(String, MemHarvest)>>,
|
||||
@ -23,7 +24,7 @@ pub struct GpusData {
|
||||
pub fn get_nvidia_vecs(
|
||||
temp_type: &TemperatureType, filter: &Option<Filter>, widgets_to_harvest: &UsedWidgets,
|
||||
) -> Option<GpusData> {
|
||||
if let Ok(nvml) = &*NVML_DATA {
|
||||
if let Ok(nvml) = NVML_DATA.get_or_init(Nvml::init) {
|
||||
if let Ok(num_gpu) = nvml.device_count() {
|
||||
let mut temp_vec = Vec::with_capacity(num_gpu as usize);
|
||||
let mut mem_vec = Vec::with_capacity(num_gpu as usize);
|
||||
|
@ -5,11 +5,11 @@ use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader, Read},
|
||||
path::PathBuf,
|
||||
sync::OnceLock,
|
||||
};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use libc::uid_t;
|
||||
use once_cell::sync::Lazy;
|
||||
use rustix::{
|
||||
fd::OwnedFd,
|
||||
fs::{Mode, OFlags},
|
||||
@ -18,7 +18,7 @@ use rustix::{
|
||||
|
||||
use crate::Pid;
|
||||
|
||||
static PAGESIZE: Lazy<u64> = Lazy::new(|| rustix::param::page_size() as u64);
|
||||
static PAGESIZE: OnceLock<u64> = OnceLock::new();
|
||||
|
||||
#[inline]
|
||||
fn next_part<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Result<&'a str, io::Error> {
|
||||
@ -103,7 +103,7 @@ impl Stat {
|
||||
/// Returns the Resident Set Size in bytes.
|
||||
#[inline]
|
||||
pub fn rss_bytes(&self) -> u64 {
|
||||
self.rss * *PAGESIZE
|
||||
self.rss * PAGESIZE.get_or_init(|| rustix::param::page_size() as u64)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,19 +132,19 @@ impl CanvasStyling {
|
||||
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 {
|
||||
|
@ -1,4 +1,3 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use tui::widgets::Borders;
|
||||
|
||||
use crate::options::ConfigColours;
|
||||
@ -23,13 +22,11 @@ pub const TIME_LABEL_HEIGHT_LIMIT: u16 = 7;
|
||||
|
||||
// Side borders
|
||||
pub const SIDE_BORDERS: Borders = Borders::LEFT.union(Borders::RIGHT);
|
||||
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
|
||||
pub static DEFAULT_LIGHT_MODE_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
|
||||
// TODO: Generate these with a macro or something...
|
||||
pub fn default_light_mode_colour_palette() -> ConfigColours {
|
||||
ConfigColours {
|
||||
text_color: Some("black".into()),
|
||||
border_color: Some("black".into()),
|
||||
table_header_color: Some("black".into()),
|
||||
@ -65,9 +62,11 @@ pub static DEFAULT_LIGHT_MODE_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(||
|
||||
"Red".into(),
|
||||
]),
|
||||
..ConfigColours::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub static GRUVBOX_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
|
||||
pub fn gruvbox_colour_palette() -> ConfigColours {
|
||||
ConfigColours {
|
||||
table_header_color: Some("#83a598".into()),
|
||||
all_cpu_color: Some("#8ec07c".into()),
|
||||
avg_cpu_color: Some("#fb4934".into()),
|
||||
@ -122,9 +121,11 @@ pub static GRUVBOX_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColo
|
||||
high_battery_color: Some("#98971a".into()),
|
||||
medium_battery_color: Some("#fabd2f".into()),
|
||||
low_battery_color: Some("#fb4934".into()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub static GRUVBOX_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
|
||||
pub fn gruvbox_light_colour_palette() -> ConfigColours {
|
||||
ConfigColours {
|
||||
table_header_color: Some("#076678".into()),
|
||||
all_cpu_color: Some("#8ec07c".into()),
|
||||
avg_cpu_color: Some("#fb4934".into()),
|
||||
@ -179,9 +180,11 @@ pub static GRUVBOX_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| Conf
|
||||
high_battery_color: Some("#98971a".into()),
|
||||
medium_battery_color: Some("#d79921".into()),
|
||||
low_battery_color: Some("#cc241d".into()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub static NORD_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
|
||||
pub fn nord_colour_palette() -> ConfigColours {
|
||||
ConfigColours {
|
||||
table_header_color: Some("#81a1c1".into()),
|
||||
all_cpu_color: Some("#88c0d0".into()),
|
||||
avg_cpu_color: Some("#8fbcbb".into()),
|
||||
@ -224,9 +227,11 @@ pub static NORD_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours
|
||||
high_battery_color: Some("#a3be8c".into()),
|
||||
medium_battery_color: Some("#ebcb8b".into()),
|
||||
low_battery_color: Some("#bf616a".into()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub static NORD_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
|
||||
pub fn nord_light_colour_palette() -> ConfigColours {
|
||||
ConfigColours {
|
||||
table_header_color: Some("#5e81ac".into()),
|
||||
all_cpu_color: Some("#81a1c1".into()),
|
||||
avg_cpu_color: Some("#8fbcbb".into()),
|
||||
@ -269,7 +274,8 @@ pub static NORD_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigC
|
||||
high_battery_color: Some("#a3be8c".into()),
|
||||
medium_battery_color: Some("#ebcb8b".into()),
|
||||
low_battery_color: Some("#bf616a".into()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Help text
|
||||
pub const HELP_CONTENTS_TEXT: [&str; 10] = [
|
||||
|
@ -1,5 +1,16 @@
|
||||
#[cfg(feature = "logging")]
|
||||
pub static OFFSET: once_cell::sync::Lazy<time::UtcOffset> = once_cell::sync::Lazy::new(|| {
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[cfg(feature = "logging")]
|
||||
pub static OFFSET: OnceLock<time::UtcOffset> = OnceLock::new();
|
||||
|
||||
#[cfg(feature = "logging")]
|
||||
pub fn init_logger(
|
||||
min_level: log::LevelFilter, debug_file_name: Option<&std::ffi::OsStr>,
|
||||
) -> Result<(), fern::InitError> {
|
||||
let dispatch = fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
let offset = OFFSET.get_or_init(|| {
|
||||
use time::util::local_offset::Soundness;
|
||||
|
||||
// SAFETY: We only invoke this once, quickly, and it should be invoked in a single-thread context.
|
||||
@ -11,22 +22,17 @@ pub static OFFSET: once_cell::sync::Lazy<time::UtcOffset> = once_cell::sync::Laz
|
||||
// mode when specifically using certain feature flags (e.g. dev-logging feature enables this behaviour).
|
||||
|
||||
time::util::local_offset::set_soundness(Soundness::Unsound);
|
||||
let res = time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
|
||||
let res =
|
||||
time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
|
||||
time::util::local_offset::set_soundness(Soundness::Sound);
|
||||
|
||||
res
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
#[cfg(feature = "logging")]
|
||||
pub fn init_logger(
|
||||
min_level: log::LevelFilter, debug_file_name: Option<&std::ffi::OsStr>,
|
||||
) -> Result<(), fern::InitError> {
|
||||
let dispatch = fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
let offset_time = {
|
||||
let utc = time::OffsetDateTime::now_utc();
|
||||
utc.checked_to_offset(*OFFSET).unwrap_or(utc)
|
||||
utc.checked_to_offset(*offset).unwrap_or(utc)
|
||||
};
|
||||
|
||||
out.finish(format_args!(
|
||||
|
Loading…
Reference in New Issue
Block a user