Merge pull request #649 from mati865/more-consts

Replace statics with consts where possible
This commit is contained in:
Clement Tsang 2021-12-30 16:57:15 -08:00 committed by GitHub
commit 456efdc242
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 95 additions and 114 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -23,12 +23,7 @@ pub const TABLE_GAP_HEIGHT_LIMIT: u16 = 7;
pub const TIME_LABEL_HEIGHT_LIMIT: u16 = 7; pub const TIME_LABEL_HEIGHT_LIMIT: u16 = 7;
// Side borders // Side borders
pub static SIDE_BORDERS: Lazy<tui::widgets::Borders> = pub const SIDE_BORDERS: tui::widgets::Borders = tui::widgets::Borders::from_bits_truncate(20);
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> = pub static DEFAULT_TEXT_STYLE: Lazy<tui::style::Style> =
Lazy::new(|| tui::style::Style::default().fg(tui::style::Color::Gray)); Lazy::new(|| tui::style::Style::default().fg(tui::style::Color::Gray));
pub static DEFAULT_HEADER_STYLE: Lazy<tui::style::Style> = pub static DEFAULT_HEADER_STYLE: Lazy<tui::style::Style> =
@ -359,18 +354,16 @@ pub const BASIC_MEM_HELP_TEXT: [&str; 2] = [
"% Toggle between values and percentages for memory usage", "% Toggle between values and percentages for memory usage",
]; ];
pub static HELP_TEXT: Lazy<Vec<Vec<&'static str>>> = Lazy::new(|| { pub const HELP_TEXT: &[&[&str]] = &[
vec![ &HELP_CONTENTS_TEXT,
HELP_CONTENTS_TEXT.to_vec(), &GENERAL_HELP_TEXT,
GENERAL_HELP_TEXT.to_vec(), &CPU_HELP_TEXT,
CPU_HELP_TEXT.to_vec(), &PROCESS_HELP_TEXT,
PROCESS_HELP_TEXT.to_vec(), &SEARCH_HELP_TEXT,
SEARCH_HELP_TEXT.to_vec(), &SORT_HELP_TEXT,
SORT_HELP_TEXT.to_vec(), &BATTERY_HELP_TEXT,
BATTERY_HELP_TEXT.to_vec(), &BASIC_MEM_HELP_TEXT,
BASIC_MEM_HELP_TEXT.to_vec(), ];
]
});
// Default layouts // Default layouts
pub const DEFAULT_LAYOUT: &str = r##" pub const DEFAULT_LAYOUT: &str = r##"
@ -546,7 +539,7 @@ pub const CONFIG_TEXT: &str = r##"# This is a default config file for bottom. A
# [[row.child.child]] represents a widget. # [[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 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: # The default widget layout:
#[[row]] #[[row]]
# ratio=30 # ratio=30
@ -631,7 +624,7 @@ pub const CONFIG_LAYOUT_HEAD: &str = r##"
# [[row.child.child]] represents a widget. # [[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 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##" pub const CONFIG_FILTER_HEAD: &str = r##"