Made dialog screens affected by colour choice.

This commit is contained in:
ClementTsang 2020-02-09 21:27:02 -05:00
parent 4bc07debb4
commit 82cab62790
3 changed files with 152 additions and 113 deletions

View file

@ -29,75 +29,6 @@ const FORCE_MIN_THRESHOLD: usize = 5;
lazy_static! { lazy_static! {
static ref DEFAULT_TEXT_STYLE: Style = Style::default().fg(Color::Gray); static ref DEFAULT_TEXT_STYLE: Style = Style::default().fg(Color::Gray);
static ref DEFAULT_HEADER_STYLE: Style = Style::default().fg(Color::LightBlue); 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 static ref DISK_HEADERS_LENS: Vec<usize> = DISK_HEADERS
.iter() .iter()
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len())) .map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
@ -159,9 +90,51 @@ pub struct Painter {
cpu_chunk: Vec<Rect>, cpu_chunk: Vec<Rect>,
network_chunk: Vec<Rect>, network_chunk: Vec<Rect>,
pub colours: CanvasColours, 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 { 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>( pub fn draw_data<B: backend::Backend>(
&mut self, terminal: &mut Terminal<B>, app_state: &mut app::App, &mut self, terminal: &mut Terminal<B>, app_state: &mut app::App,
) -> error::Result<()> { ) -> error::Result<()> {
@ -195,26 +168,37 @@ impl Painter {
) )
.split(vertical_dialog_chunk[1]); .split(vertical_dialog_chunk[1]);
const HELP_BASE : &str = " Help ── 1: General ─── 2: Processes ─── 3: Search ─── Esc to close "; const HELP_BASE: &str =
let repeat_num = max(0, middle_dialog_chunk[1].width as i32 - HELP_BASE.chars().count() as i32 - 2); " Help ── 1: General ─── 2: Processes ─── 3: Search ─── Esc to close ";
let help_title = format!(" Help ─{}─ 1: General ─── 2: Processes ─── 3: Search ─── Esc to close ", "".repeat(repeat_num as usize)); 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 { Paragraph::new(
app::AppHelpCategory::General => (*GENERAL_HELP_TEXT).to_vec(), match app_state.help_dialog_state.current_category {
app::AppHelpCategory::Process => (*PROCESS_HELP_TEXT).to_vec(), app::AppHelpCategory::General => &self.styled_general_help_text,
app::AppHelpCategory::Search => (*SEARCH_HELP_TEXT).to_vec(), app::AppHelpCategory::Process => &self.styled_process_help_text,
}.iter()) app::AppHelpCategory::Search => &self.styled_search_help_text,
.block( }
Block::default() .iter(),
.title(&help_title) )
.title_style(self.colours.widget_title_style) .block(
.style(self.colours.border_style) Block::default()
.borders(Borders::ALL), .title(&help_title)
) .title_style(self.colours.border_style)
.style(self.colours.text_style) .style(self.colours.border_style)
.alignment(Alignment::Left) .borders(Borders::ALL)
.wrap(true) .border_style(self.colours.border_style),
.render(&mut f, middle_dialog_chunk[1]); )
.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 { } else if app_state.delete_dialog_state.is_showing_dd {
let vertical_dialog_chunk = Layout::default() let vertical_dialog_chunk = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
@ -248,17 +232,22 @@ impl Painter {
dd_err dd_err
))]; ))];
const ERROR_BASE : &str = " Error ── Esc to close "; 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 repeat_num = max(
let error_title = format!(" Error ─{}─ Esc to close ", "".repeat(repeat_num as usize)); 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()) Paragraph::new(dd_text.iter())
.block( .block(
Block::default() Block::default()
.title(&error_title) .title(&error_title)
.title_style(self.colours.text_style) .title_style(self.colours.border_style)
.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) .style(self.colours.text_style)
.alignment(Alignment::Center) .alignment(Alignment::Center)
@ -297,20 +286,27 @@ impl Painter {
} else { } else {
Text::styled("No", self.colours.currently_selected_text_style) Text::styled("No", self.colours.currently_selected_text_style)
}, },
]; ];
const DD_BASE : &str = " Confirm Kill Process ── Esc to close "; 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 repeat_num = max(
let dd_title = format!(" Confirm Kill Process ─{}─ Esc to close ", "".repeat(repeat_num as usize)); 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()) Paragraph::new(dd_text.iter())
.block( .block(
Block::default() Block::default()
.title(&dd_title) .title(&dd_title)
.title_style(self.colours.widget_title_style) .title_style(self.colours.border_style)
.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) .style(self.colours.text_style)
.alignment(Alignment::Center) .alignment(Alignment::Center)
@ -1000,26 +996,29 @@ impl Painter {
search_text.extend(query_with_cursor); search_text.extend(query_with_cursor);
search_text.extend(option_text); search_text.extend(option_text);
const TITLE_BASE : &str = " Esc to close "; 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 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 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()) Paragraph::new(search_text.iter())
.block( .block(
Block::default() Block::default()
.borders(Borders::ALL) .borders(Borders::ALL)
.title(&title) .title(&title)
.title_style(self.colours.widget_title_style) .title_style(current_border_style)
.border_style(if app_state.get_current_regex_matcher().is_err() { .border_style(current_border_style),
Style::default().fg(Color::Red)
} else {
match app_state.current_widget_selected {
app::WidgetPosition::ProcessSearch => {
self.colours.highlighted_border_style
}
_ => self.colours.border_style,
}
}),
) )
.style(self.colours.text_style) .style(self.colours.text_style)
.alignment(Alignment::Left) .alignment(Alignment::Left)

View file

@ -10,3 +10,43 @@ pub const DEFAULT_UNIX_CONFIG_FILE_PATH: &str = "~/.config/btm/btm.toml";
// TODO: [CONF] Default windows path? // TODO: [CONF] Default windows path?
pub const DEFAULT_WINDOWS_CONFIG_FILE_PATH: &str = ""; 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",
];

View file

@ -373,8 +373,8 @@ fn main() -> error::Result<()> {
cleanup_terminal(&mut terminal)?; cleanup_terminal(&mut terminal)?;
return Err(config_check); return Err(config_check);
} }
painter.colours.generate_remaining_cpu_colours(); painter.colours.generate_remaining_cpu_colours();
painter.initialize();
loop { loop {
// TODO: [OPT] this should not block... // TODO: [OPT] this should not block...