diff options
Diffstat (limited to 'src/canvas.rs')
-rw-r--r-- | src/canvas.rs | 225 |
1 files changed, 112 insertions, 113 deletions
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<usize> = DISK_HEADERS .iter() .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) @@ -159,9 +90,51 @@ pub struct Painter { cpu_chunk: Vec<Rect>, network_chunk: Vec<Rect>, pub colours: CanvasColours, + pub styled_general_help_text: Vec<Text<'static>>, + pub styled_process_help_text: Vec<Text<'static>>, + pub styled_search_help_text: Vec<Text<'static>>, } 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::<Vec<_>>(), + ); + + 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::<Vec<_>>(), + ); + + 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::<Vec<_>>(), + ); + } + pub fn draw_data<B: backend::Backend>( &mut self, terminal: &mut Terminal<B>, 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) |