From 82cab6279087d96e289043c1b329504a5c0083b0 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Sun, 9 Feb 2020 21:27:02 -0500 Subject: Made dialog screens affected by colour choice. --- src/canvas.rs | 225 +++++++++++++++++++++++++++---------------------------- src/constants.rs | 40 ++++++++++ src/main.rs | 2 +- 3 files changed, 153 insertions(+), 114 deletions(-) (limited to 'src') diff --git a/src/canvas.rs b/src/canvas.rs index 1b9fcb67..c8cd0d0a 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -29,75 +29,6 @@ const FORCE_MIN_THRESHOLD: usize = 5; lazy_static! { static ref DEFAULT_TEXT_STYLE: Style = Style::default().fg(Color::Gray); static ref DEFAULT_HEADER_STYLE: Style = Style::default().fg(Color::LightBlue); - static ref GENERAL_HELP_TEXT: [Text<'static>; 14] = [ - Text::styled("General Keybindings\n\n", *DEFAULT_HEADER_STYLE), - Text::styled("Esc Close dialog box\n", *DEFAULT_TEXT_STYLE), - Text::styled("q, Ctrl-c Quit bottom\n", *DEFAULT_TEXT_STYLE), - Text::styled("Ctrl-r Reset all data\n", *DEFAULT_TEXT_STYLE), - Text::styled("f Freeze display\n", *DEFAULT_TEXT_STYLE), - Text::styled("Ctrl-Arrow Move currently selected widget\n", *DEFAULT_TEXT_STYLE), - Text::styled("Shift-Arrow Move currently selected widget\n", *DEFAULT_TEXT_STYLE), - Text::styled("Up, k Move cursor up\n", *DEFAULT_TEXT_STYLE), - Text::styled("Down, j Move cursor down\n", *DEFAULT_TEXT_STYLE), - Text::styled("Left, h Move cursor left\n", *DEFAULT_TEXT_STYLE), - Text::styled("Right, l Move cursor right\n", *DEFAULT_TEXT_STYLE), - Text::styled("? Open the help screen\n", *DEFAULT_TEXT_STYLE), - Text::styled( - "gg Skip to the first entry of a list\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled( - "G Skip to the last entry of a list\n", - *DEFAULT_TEXT_STYLE - ), - ]; - static ref PROCESS_HELP_TEXT : [Text<'static>; 8] = [ - Text::styled("Process Keybindings\n\n", *DEFAULT_HEADER_STYLE), - Text::styled( - "dd Kill the highlighted process\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled("c Sort by CPU usage\n", *DEFAULT_TEXT_STYLE), - Text::styled("m Sort by memory usage\n", *DEFAULT_TEXT_STYLE), - Text::styled("p Sort by PID\n", *DEFAULT_TEXT_STYLE), - Text::styled("n Sort by process name\n", *DEFAULT_TEXT_STYLE), - Text::styled( - "Tab Group together processes with the same name\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled( - "Ctrl-f, / Open up the search widget\n", - *DEFAULT_TEXT_STYLE - ), - ]; - static ref SEARCH_HELP_TEXT : [Text<'static>; 8] = [ - Text::styled("Search Keybindings\n\n", *DEFAULT_HEADER_STYLE), - Text::styled( - "Tab Toggle between searching for PID and name.\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled("Esc Close search widget\n", *DEFAULT_TEXT_STYLE), - Text::styled( - "Ctrl-a Skip to the start of search widget\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled( - "Ctrl-e Skip to the end of search widget\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled( - "Alt-c Toggle whether to ignore case\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled( - "Alt-m Toggle whether to match the whole word\n", - *DEFAULT_TEXT_STYLE - ), - Text::styled( - "Alt-r Toggle whether to use regex\n", - *DEFAULT_TEXT_STYLE - ) - ]; static ref DISK_HEADERS_LENS: Vec = DISK_HEADERS .iter() .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) @@ -159,9 +90,51 @@ pub struct Painter { cpu_chunk: Vec, network_chunk: Vec, pub colours: CanvasColours, + pub styled_general_help_text: Vec>, + pub styled_process_help_text: Vec>, + pub styled_search_help_text: Vec>, } impl Painter { + /// Must be run once before drawing, but after setting colours. + /// This is to set some remaining styles and text. + /// This bypasses some logic checks (size > 2, for example) but this + /// assumes that you, the programmer, are sane and do not do stupid things. + pub fn initialize(&mut self) { + self.styled_general_help_text.push(Text::Styled( + GENERAL_HELP_TEXT[0].into(), + self.colours.table_header_style, + )); + self.styled_general_help_text.extend( + GENERAL_HELP_TEXT[1..] + .iter() + .map(|&text| Text::Styled(text.into(), self.colours.text_style)) + .collect::>(), + ); + + self.styled_process_help_text.push(Text::Styled( + PROCESS_HELP_TEXT[0].into(), + self.colours.table_header_style, + )); + self.styled_process_help_text.extend( + PROCESS_HELP_TEXT[1..] + .iter() + .map(|&text| Text::Styled(text.into(), self.colours.text_style)) + .collect::>(), + ); + + self.styled_search_help_text.push(Text::Styled( + SEARCH_HELP_TEXT[0].into(), + self.colours.table_header_style, + )); + self.styled_search_help_text.extend( + SEARCH_HELP_TEXT[1..] + .iter() + .map(|&text| Text::Styled(text.into(), self.colours.text_style)) + .collect::>(), + ); + } + pub fn draw_data( &mut self, terminal: &mut Terminal, app_state: &mut app::App, ) -> error::Result<()> { @@ -195,26 +168,37 @@ impl Painter { ) .split(vertical_dialog_chunk[1]); - const HELP_BASE : &str = " Help ── 1: General ─── 2: Processes ─── 3: Search ─── Esc to close "; - let repeat_num = max(0, middle_dialog_chunk[1].width as i32 - HELP_BASE.chars().count() as i32 - 2); - let help_title = format!(" Help ─{}─ 1: General ─── 2: Processes ─── 3: Search ─── Esc to close ", "─".repeat(repeat_num as usize)); - - Paragraph::new(match app_state.help_dialog_state.current_category { - app::AppHelpCategory::General => (*GENERAL_HELP_TEXT).to_vec(), - app::AppHelpCategory::Process => (*PROCESS_HELP_TEXT).to_vec(), - app::AppHelpCategory::Search => (*SEARCH_HELP_TEXT).to_vec(), - }.iter()) - .block( - Block::default() - .title(&help_title) - .title_style(self.colours.widget_title_style) - .style(self.colours.border_style) - .borders(Borders::ALL), - ) - .style(self.colours.text_style) - .alignment(Alignment::Left) - .wrap(true) - .render(&mut f, middle_dialog_chunk[1]); + const HELP_BASE: &str = + " Help ── 1: General ─── 2: Processes ─── 3: Search ─── Esc to close "; + let repeat_num = max( + 0, + middle_dialog_chunk[1].width as i32 - HELP_BASE.chars().count() as i32 - 2, + ); + let help_title = format!( + " Help ─{}─ 1: General ─── 2: Processes ─── 3: Search ─── Esc to close ", + "─".repeat(repeat_num as usize) + ); + + Paragraph::new( + match app_state.help_dialog_state.current_category { + app::AppHelpCategory::General => &self.styled_general_help_text, + app::AppHelpCategory::Process => &self.styled_process_help_text, + app::AppHelpCategory::Search => &self.styled_search_help_text, + } + .iter(), + ) + .block( + Block::default() + .title(&help_title) + .title_style(self.colours.border_style) + .style(self.colours.border_style) + .borders(Borders::ALL) + .border_style(self.colours.border_style), + ) + .style(self.colours.text_style) + .alignment(Alignment::Left) + .wrap(true) + .render(&mut f, middle_dialog_chunk[1]); } else if app_state.delete_dialog_state.is_showing_dd { let vertical_dialog_chunk = Layout::default() .direction(Direction::Vertical) @@ -248,17 +232,22 @@ impl Painter { dd_err ))]; - const ERROR_BASE : &str = " Error ── Esc to close "; - let repeat_num = max(0, middle_dialog_chunk[1].width as i32 - ERROR_BASE.chars().count() as i32 - 2); - let error_title = format!(" Error ─{}─ Esc to close ", "─".repeat(repeat_num as usize)); + const ERROR_BASE: &str = " Error ── Esc to close "; + let repeat_num = max( + 0, + middle_dialog_chunk[1].width as i32 - ERROR_BASE.chars().count() as i32 - 2, + ); + let error_title = + format!(" Error ─{}─ Esc to close ", "─".repeat(repeat_num as usize)); Paragraph::new(dd_text.iter()) .block( Block::default() .title(&error_title) - .title_style(self.colours.text_style) + .title_style(self.colours.border_style) .style(self.colours.border_style) - .borders(Borders::ALL), + .borders(Borders::ALL) + .border_style(self.colours.border_style), ) .style(self.colours.text_style) .alignment(Alignment::Center) @@ -297,20 +286,27 @@ impl Painter { } else { Text::styled("No", self.colours.currently_selected_text_style) }, - ]; - const DD_BASE : &str = " Confirm Kill Process ── Esc to close "; - let repeat_num = max(0, middle_dialog_chunk[1].width as i32 - DD_BASE.chars().count() as i32 - 2); - let dd_title = format!(" Confirm Kill Process ─{}─ Esc to close ", "─".repeat(repeat_num as usize)); + const DD_BASE: &str = " Confirm Kill Process ── Esc to close "; + let repeat_num = max( + 0, + middle_dialog_chunk[1].width as i32 + - DD_BASE.chars().count() as i32 - 2, + ); + let dd_title = format!( + " Confirm Kill Process ─{}─ Esc to close ", + "─".repeat(repeat_num as usize) + ); Paragraph::new(dd_text.iter()) .block( Block::default() .title(&dd_title) - .title_style(self.colours.widget_title_style) + .title_style(self.colours.border_style) .style(self.colours.border_style) - .borders(Borders::ALL), + .borders(Borders::ALL) + .border_style(self.colours.border_style), ) .style(self.colours.text_style) .alignment(Alignment::Center) @@ -1000,26 +996,29 @@ impl Painter { search_text.extend(query_with_cursor); search_text.extend(option_text); - const TITLE_BASE : &str = " Esc to close "; - let repeat_num = max(0, draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2); + const TITLE_BASE: &str = " Esc to close "; + let repeat_num = max( + 0, + draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, + ); let title = format!("{} Esc to close ", "─".repeat(repeat_num as usize)); + let current_border_style: Style = if app_state.get_current_regex_matcher().is_err() { + Style::default().fg(Color::Rgb(255, 0, 0)) + } else { + match app_state.current_widget_selected { + app::WidgetPosition::ProcessSearch => self.colours.highlighted_border_style, + _ => self.colours.border_style, + } + }; + Paragraph::new(search_text.iter()) .block( Block::default() .borders(Borders::ALL) .title(&title) - .title_style(self.colours.widget_title_style) - .border_style(if app_state.get_current_regex_matcher().is_err() { - Style::default().fg(Color::Red) - } else { - match app_state.current_widget_selected { - app::WidgetPosition::ProcessSearch => { - self.colours.highlighted_border_style - } - _ => self.colours.border_style, - } - }), + .title_style(current_border_style) + .border_style(current_border_style), ) .style(self.colours.text_style) .alignment(Alignment::Left) diff --git a/src/constants.rs b/src/constants.rs index 19b9d1d6..d01e63ff 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -10,3 +10,43 @@ pub const DEFAULT_UNIX_CONFIG_FILE_PATH: &str = "~/.config/btm/btm.toml"; // TODO: [CONF] Default windows path? pub const DEFAULT_WINDOWS_CONFIG_FILE_PATH: &str = ""; + +// Help text +pub const GENERAL_HELP_TEXT: [&'static str; 14] = [ + "General Keybindings\n\n", + "Esc Close dialog box\n", + "q, Ctrl-c Quit bottom\n", + "Ctrl-r Reset all data\n", + "f Freeze display\n", + "Ctrl-Arrow Move currently selected widget\n", + "Shift-Arrow Move currently selected widget\n", + "Up, k Move cursor up\n", + "Down, j Move cursor down\n", + "Left, h Move cursor left\n", + "Right, l Move cursor right\n", + "? Open the help screen\n", + "gg Skip to the first entry of a list\n", + "G Skip to the last entry of a list\n", +]; + +pub const PROCESS_HELP_TEXT: [&'static str; 8] = [ + "Process Keybindings\n\n", + "dd Kill the highlighted process\n", + "c Sort by CPU usage\n", + "m Sort by memory usage\n", + "p Sort by PID\n", + "n Sort by process name\n", + "Tab Group together processes with the same name\n", + "Ctrl-f, / Open up the search widget\n", +]; + +pub const SEARCH_HELP_TEXT: [&'static str; 8] = [ + "Search Keybindings\n\n", + "Tab Toggle between searching for PID and name.\n", + "Esc Close search widget\n", + "Ctrl-a Skip to the start of search widget\n", + "Ctrl-e Skip to the end of search widget\n", + "Alt-c Toggle whether to ignore case\n", + "Alt-m Toggle whether to match the whole word\n", + "Alt-r Toggle whether to use regex\n", +]; diff --git a/src/main.rs b/src/main.rs index 39cf344c..f0426430 100644 --- a/src/main.rs +++ b/src/main.rs @@ -373,8 +373,8 @@ fn main() -> error::Result<()> { cleanup_terminal(&mut terminal)?; return Err(config_check); } - painter.colours.generate_remaining_cpu_colours(); + painter.initialize(); loop { // TODO: [OPT] this should not block... -- cgit v1.2.3