Replace statics with consts where possible

This commit is contained in:
Mateusz Mikuła 2021-12-30 21:29:57 +01:00
parent 6d4045a5f4
commit e33d6b5361
8 changed files with 97 additions and 114 deletions

View file

@ -69,7 +69,7 @@ impl BatteryDisplayWidget for Painter {
.border_style(border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style)
} else {
Block::default().borders(Borders::NONE)

View file

@ -42,7 +42,7 @@ impl CpuBasicWidget for Painter {
if app_state.current_widget.widget_id == widget_id {
f.render_widget(
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style),
draw_loc,
);

View file

@ -226,7 +226,7 @@ impl DiskTableWidget for Painter {
.border_style(border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style)
} else {
Block::default().borders(Borders::NONE)

View file

@ -34,7 +34,7 @@ impl MemBasicWidget for Painter {
if app_state.current_widget.widget_id == widget_id {
f.render_widget(
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style),
draw_loc,
);

View file

@ -38,7 +38,7 @@ impl NetworkBasicWidget for Painter {
if app_state.current_widget.widget_id == widget_id {
f.render_widget(
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style),
draw_loc,
);

View file

@ -18,86 +18,74 @@ use tui::{
use unicode_segmentation::{GraphemeIndices, UnicodeSegmentation};
use unicode_width::UnicodeWidthStr;
use once_cell::sync::Lazy;
const PROCESS_HEADERS_HARD_WIDTH_NO_GROUP: &[Option<u16>] = &[
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
#[cfg(target_family = "unix")]
None,
None,
];
const PROCESS_HEADERS_HARD_WIDTH_GROUPED: &[Option<u16>] = &[
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
];
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),
#[cfg(target_family = "unix")]
None,
None,
]
});
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),
]
});
const PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_COMMAND: &[Option<f64>] =
&[None, Some(0.7), None, None, None, None, None, None];
const PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_ELSE: &[Option<f64>] =
&[None, Some(0.3), None, None, None, None, None, 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_ELSE: Lazy<Vec<Option<f64>>> =
Lazy::new(|| vec![None, Some(0.3), 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,
#[cfg(target_family = "unix")]
Some(0.05),
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,
#[cfg(target_family = "unix")]
Some(0.05),
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,
#[cfg(target_family = "unix")]
Some(0.05),
Some(0.2),
]
});
const PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_COMMAND: &[Option<f64>] = &[
None,
Some(0.7),
None,
None,
None,
None,
None,
None,
#[cfg(target_family = "unix")]
Some(0.05),
Some(0.2),
];
const PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_TREE: &[Option<f64>] = &[
None,
Some(0.5),
None,
None,
None,
None,
None,
None,
#[cfg(target_family = "unix")]
Some(0.05),
Some(0.2),
];
const PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_ELSE: &[Option<f64>] = &[
None,
Some(0.3),
None,
None,
None,
None,
None,
None,
#[cfg(target_family = "unix")]
Some(0.05),
Some(0.2),
];
pub trait ProcessTableWidget {
/// Draws and handles all process-related drawing. Use this.
@ -291,7 +279,7 @@ impl ProcessTableWidget for Painter {
.border_style(border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style)
} else {
Block::default().borders(Borders::NONE)
@ -352,9 +340,9 @@ impl ProcessTableWidget for Painter {
// Calculate widths
// FIXME: See if we can move this into the recalculate block? I want to move column widths into the column widths
let hard_widths = if proc_widget_state.is_grouped {
&*PROCESS_HEADERS_HARD_WIDTH_GROUPED
PROCESS_HEADERS_HARD_WIDTH_GROUPED
} else {
&*PROCESS_HEADERS_HARD_WIDTH_NO_GROUP
PROCESS_HEADERS_HARD_WIDTH_NO_GROUP
};
if recalculate_column_widths {
@ -404,16 +392,16 @@ impl ProcessTableWidget for Painter {
// Note grouped trees are not a thing.
if proc_widget_state.is_using_command {
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_COMMAND
PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_COMMAND
} else {
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_ELSE
PROCESS_HEADERS_SOFT_WIDTH_MAX_GROUPED_ELSE
}
} else if proc_widget_state.is_using_command {
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_COMMAND
PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_COMMAND
} else if proc_widget_state.is_tree_mode {
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_TREE
PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_TREE
} else {
&*PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_ELSE
PROCESS_HEADERS_SOFT_WIDTH_MAX_NO_GROUP_ELSE
};
proc_widget_state.table_width_state.calculated_column_widths =
@ -752,7 +740,7 @@ impl ProcessTableWidget for Painter {
.border_style(current_border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(current_border_style)
} else {
Block::default().borders(Borders::NONE)
@ -863,7 +851,7 @@ impl ProcessTableWidget for Painter {
.border_style(current_border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(current_border_style)
} else {
Block::default().borders(Borders::NONE)

View file

@ -215,7 +215,7 @@ impl TempTableWidget for Painter {
.border_style(border_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.borders(SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style)
} else {
Block::default().borders(Borders::NONE)

View file

@ -23,12 +23,9 @@ pub const TABLE_GAP_HEIGHT_LIMIT: u16 = 7;
pub const TIME_LABEL_HEIGHT_LIMIT: u16 = 7;
// Side borders
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 const SIDE_BORDERS: tui::widgets::Borders = tui::widgets::Borders::from_bits_truncate(20);
pub const TOP_LEFT_RIGHT: tui::widgets::Borders = tui::widgets::Borders::from_bits_truncate(22);
pub const BOTTOM_LEFT_RIGHT: tui::widgets::Borders = 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> =
@ -359,18 +356,16 @@ pub const BASIC_MEM_HELP_TEXT: [&str; 2] = [
"% Toggle between values and percentages for memory usage",
];
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(),
PROCESS_HELP_TEXT.to_vec(),
SEARCH_HELP_TEXT.to_vec(),
SORT_HELP_TEXT.to_vec(),
BATTERY_HELP_TEXT.to_vec(),
BASIC_MEM_HELP_TEXT.to_vec(),
]
});
pub const HELP_TEXT: &[&[&str]] = &[
&HELP_CONTENTS_TEXT,
&GENERAL_HELP_TEXT,
&CPU_HELP_TEXT,
&PROCESS_HELP_TEXT,
&SEARCH_HELP_TEXT,
&SORT_HELP_TEXT,
&BATTERY_HELP_TEXT,
&BASIC_MEM_HELP_TEXT,
];
// Default layouts
pub const DEFAULT_LAYOUT: &str = r##"
@ -546,7 +541,7 @@ pub const CONFIG_TEXT: &str = r##"# This is a default config file for bottom. A
# [[row.child.child]] represents a widget.
#
# All widgets must have the type value set to one of ["cpu", "mem", "proc", "net", "temp", "disk", "empty"].
# All layout components have a ratio value - if this is not set, then it defaults to 1.
# All layout components have a ratio value - if this is not set, then it defaults to 1.
# The default widget layout:
#[[row]]
# ratio=30
@ -631,7 +626,7 @@ pub const CONFIG_LAYOUT_HEAD: &str = r##"
# [[row.child.child]] represents a widget.
#
# All widgets must have the valid type value set to one of ["cpu", "mem", "proc", "net", "temp", "disk", "empty"].
# All layout components have a ratio value - if this is not set, then it defaults to 1.
# All layout components have a ratio value - if this is not set, then it defaults to 1.
"##;
pub const CONFIG_FILTER_HEAD: &str = r##"