refactor: changed how we set help text on resize and init

This commit is contained in:
Clement Tsang 2020-04-25 17:32:31 -04:00 committed by GitHub
parent 863e780f2f
commit 3a6f7a6750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 161 deletions

View File

@ -58,21 +58,20 @@ pub struct AppDeleteDialogState {
pub is_on_yes: bool, // Defaults to "No" pub is_on_yes: bool, // Defaults to "No"
} }
pub enum AppHelpCategory {
General,
Process,
Search,
}
#[derive(Default)]
pub struct AppHelpDialogState { pub struct AppHelpDialogState {
pub is_showing_help: bool, pub is_showing_help: bool,
pub scroll_state: ParagraphScrollState, pub scroll_state: ParagraphScrollState,
pub general_index: u16, pub index_shortcuts: Vec<u16>,
pub cpu_index: u16, }
pub process_index: u16,
pub search_index: u16, impl Default for AppHelpDialogState {
pub battery_index: u16, fn default() -> Self {
AppHelpDialogState {
is_showing_help: false,
scroll_state: ParagraphScrollState::default(),
index_shortcuts: vec![0; constants::HELP_TEXT.len()],
}
}
} }
/// AppConfigFields is meant to cover basic fields that would normally be set /// AppConfigFields is meant to cover basic fields that would normally be set
@ -583,11 +582,16 @@ impl App {
pub fn on_esc(&mut self) { pub fn on_esc(&mut self) {
self.reset_multi_tap_keys(); self.reset_multi_tap_keys();
if self.is_in_dialog() { if self.is_in_dialog() {
self.help_dialog_state.is_showing_help = false; if self.help_dialog_state.is_showing_help {
self.delete_dialog_state.is_showing_dd = false; self.help_dialog_state.is_showing_help = false;
self.delete_dialog_state.is_on_yes = false; self.help_dialog_state.scroll_state.current_scroll_index = 0;
self.to_delete_process_list = None; } else {
self.dd_err = None; self.delete_dialog_state.is_showing_dd = false;
self.delete_dialog_state.is_on_yes = false;
self.to_delete_process_list = None;
self.dd_err = None;
}
self.is_force_redraw = true; self.is_force_redraw = true;
} else if self.is_filtering_or_searching() { } else if self.is_filtering_or_searching() {
match self.current_widget.widget_type { match self.current_widget.widget_type {
@ -1441,11 +1445,11 @@ impl App {
// more obvious that we are separating dialog logic and normal logic IMO. // more obvious that we are separating dialog logic and normal logic IMO.
// This is even more so as most logic already checks for dialog state. // This is even more so as most logic already checks for dialog state.
match caught_char { match caught_char {
'1' => self.help_scroll_to_or_max(self.help_dialog_state.general_index), '1' => self.help_scroll_to_or_max(self.help_dialog_state.index_shortcuts[1]),
'2' => self.help_scroll_to_or_max(self.help_dialog_state.cpu_index), '2' => self.help_scroll_to_or_max(self.help_dialog_state.index_shortcuts[2]),
'3' => self.help_scroll_to_or_max(self.help_dialog_state.process_index), '3' => self.help_scroll_to_or_max(self.help_dialog_state.index_shortcuts[3]),
'4' => self.help_scroll_to_or_max(self.help_dialog_state.search_index), '4' => self.help_scroll_to_or_max(self.help_dialog_state.index_shortcuts[4]),
'5' => self.help_scroll_to_or_max(self.help_dialog_state.battery_index), '5' => self.help_scroll_to_or_max(self.help_dialog_state.index_shortcuts[5]),
'j' | 'k' | 'g' | 'G' => self.handle_char(caught_char), 'j' | 'k' | 'g' | 'G' => self.handle_char(caught_char),
_ => {} _ => {}
} }

View File

@ -161,81 +161,34 @@ impl Painter {
self.is_mac_os = cfg!(target_os = "macos"); self.is_mac_os = cfg!(target_os = "macos");
// Init help text: // Init help text:
// ToC (*HELP_TEXT).iter().enumerate().for_each(|(itx, section)| {
self.styled_help_text.extend( if itx == 0 {
HELP_CONTENTS_TEXT self.styled_help_text.extend(
.iter() section
.map(|&text| Text::Styled(text.into(), self.colours.text_style)) .iter()
.collect::<Vec<_>>(), .map(|&text| Text::Styled(text.into(), self.colours.text_style))
); .collect::<Vec<_>>(),
);
// General } else {
self.styled_help_text.push(Text::Raw("\n\n".into())); // Not required check but it runs only a few times... so whatever ig, prevents me from
self.styled_help_text.push(Text::Styled( // being dumb and leaving a help text section only one line long.
GENERAL_HELP_TEXT[0].into(), if section.len() > 1 {
self.colours.table_header_style, self.styled_help_text.push(Text::Raw("\n\n".into()));
)); self.styled_help_text.push(Text::Styled(
self.styled_help_text.extend( section[0].into(),
GENERAL_HELP_TEXT[1..] self.colours.table_header_style,
.iter() ));
.map(|&text| Text::Styled(text.into(), self.colours.text_style)) self.styled_help_text.extend(
.collect::<Vec<_>>(), section[1..]
); .iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
// CPU .collect::<Vec<_>>(),
self.styled_help_text.push(Text::Raw("\n\n".into())); );
self.styled_help_text.push(Text::Styled( }
CPU_HELP_TEXT[0].into(), }
self.colours.table_header_style, });
));
self.styled_help_text.extend(
CPU_HELP_TEXT[1..]
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
// Proc
self.styled_help_text.push(Text::Raw("\n\n".into()));
self.styled_help_text.push(Text::Styled(
PROCESS_HELP_TEXT[0].into(),
self.colours.table_header_style,
));
self.styled_help_text.extend(
PROCESS_HELP_TEXT[1..]
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
// Proc Search
self.styled_help_text.push(Text::Raw("\n\n".into()));
self.styled_help_text.push(Text::Styled(
SEARCH_HELP_TEXT[0].into(),
self.colours.table_header_style,
));
self.styled_help_text.extend(
SEARCH_HELP_TEXT[1..]
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
// Battery
self.styled_help_text.push(Text::Raw("\n\n".into()));
self.styled_help_text.push(Text::Styled(
BATTERY_HELP_TEXT[0].into(),
self.colours.table_header_style,
));
self.styled_help_text.extend(
BATTERY_HELP_TEXT[1..]
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
} }
// TODO: [FEATURE] Auto-resizing dialog sizes.
pub fn draw_data<B: Backend>( pub fn draw_data<B: 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<()> {
@ -256,8 +209,6 @@ impl Painter {
terminal.autoresize()?; terminal.autoresize()?;
terminal.draw(|mut f| { terminal.draw(|mut f| {
if app_state.help_dialog_state.is_showing_help { if app_state.help_dialog_state.is_showing_help {
// TODO: [RESIZE] Scrolling dialog boxes is ideal. This is currently VERY temporary!
// The width is currently not good and can wrap... causing this to not go so well!
let gen_help_len = GENERAL_HELP_TEXT.len() as u16 + 3; let gen_help_len = GENERAL_HELP_TEXT.len() as u16 + 3;
let border_len = (max(0, f.size().height as i64 - gen_help_len as i64)) as u16 / 2; let border_len = (max(0, f.size().height as i64 - gen_help_len as i64)) as u16 / 2;
let vertical_dialog_chunk = Layout::default() let vertical_dialog_chunk = Layout::default()

View File

@ -32,75 +32,43 @@ impl HelpDialog for Painter {
// We must also recalculate how many lines are wrapping to properly get scrolling to work on // We must also recalculate how many lines are wrapping to properly get scrolling to work on
// small terminal sizes... oh joy. // small terminal sizes... oh joy.
// TODO: Make this more automated and easier to add.
let mut overflow_buffer = 0; let mut overflow_buffer = 0;
let paragraph_width = draw_loc.width - 2; let paragraph_width = draw_loc.width - 2;
constants::HELP_CONTENTS_TEXT.iter().for_each(|text_line| { let mut prev_section_len = 0;
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
// General constants::HELP_TEXT
app_state.help_dialog_state.general_index = .iter()
constants::HELP_CONTENTS_TEXT.len() as u16 + 1 + overflow_buffer; .enumerate()
constants::GENERAL_HELP_TEXT.iter().for_each(|text_line| { .for_each(|(itx, section)| {
overflow_buffer += let mut buffer = 0;
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
// CPU if itx == 0 {
app_state.help_dialog_state.cpu_index = section.iter().for_each(|text_line| {
(constants::HELP_CONTENTS_TEXT.len() + constants::GENERAL_HELP_TEXT.len()) as u16 buffer += UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16
+ 2 / paragraph_width;
+ overflow_buffer; });
constants::CPU_HELP_TEXT.iter().for_each(|text_line| {
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
// Processes app_state.help_dialog_state.index_shortcuts[itx] = 0;
app_state.help_dialog_state.process_index = (constants::HELP_CONTENTS_TEXT.len() prev_section_len = section.len() as u16 + buffer;
+ constants::GENERAL_HELP_TEXT.len() overflow_buffer += buffer;
+ constants::CPU_HELP_TEXT.len()) } else {
as u16 section.iter().for_each(|text_line| {
+ 3 buffer += UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16
+ overflow_buffer; / paragraph_width;
constants::PROCESS_HELP_TEXT.iter().for_each(|text_line| { });
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
// Search app_state.help_dialog_state.index_shortcuts[itx] =
app_state.help_dialog_state.search_index = (constants::HELP_CONTENTS_TEXT.len() app_state.help_dialog_state.index_shortcuts[itx - 1]
+ constants::GENERAL_HELP_TEXT.len() + 1
+ constants::CPU_HELP_TEXT.len() + prev_section_len;
+ constants::PROCESS_HELP_TEXT.len()) prev_section_len = section.len() as u16 + buffer;
as u16 overflow_buffer += buffer;
+ 4 }
+ overflow_buffer; });
constants::SEARCH_HELP_TEXT.iter().for_each(|text_line| {
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
// Battery
app_state.help_dialog_state.battery_index = (constants::HELP_CONTENTS_TEXT.len()
+ constants::GENERAL_HELP_TEXT.len()
+ constants::CPU_HELP_TEXT.len()
+ constants::PROCESS_HELP_TEXT.len()
+ constants::SEARCH_HELP_TEXT.len())
as u16
+ 5
+ overflow_buffer;
constants::BATTERY_HELP_TEXT.iter().for_each(|text_line| {
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
app_state.help_dialog_state.scroll_state.max_scroll_index = app_state.help_dialog_state.scroll_state.max_scroll_index =
(self.styled_help_text.len() as u16 (self.styled_help_text.len() as u16
+ (constants::NUM_CATEGORIES - 3) + (constants::HELP_TEXT.len() as u16 - 3)
+ overflow_buffer) + overflow_buffer)
.saturating_sub(draw_loc.height); .saturating_sub(draw_loc.height);

View File

@ -36,8 +36,6 @@ lazy_static! {
} }
// Help text // Help text
pub const NUM_CATEGORIES: u16 = 6;
pub const HELP_CONTENTS_TEXT: [&str; 6] = [ pub const HELP_CONTENTS_TEXT: [&str; 6] = [
"Press the corresponding numbers to jump to the section, or scroll:\n", "Press the corresponding numbers to jump to the section, or scroll:\n",
"1 - General bindings\n", "1 - General bindings\n",
@ -108,6 +106,17 @@ pub const BATTERY_HELP_TEXT: [&str; 3] = [
"Right Go to next battery", "Right Go to next battery",
]; ];
lazy_static! {
pub static ref HELP_TEXT: Vec<Vec<&'static str>> = 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(),
BATTERY_HELP_TEXT.to_vec(),
];
}
// Config and flags // Config and flags
pub const DEFAULT_UNIX_CONFIG_FILE_PATH: &str = ".config/bottom/bottom.toml"; pub const DEFAULT_UNIX_CONFIG_FILE_PATH: &str = ".config/bottom/bottom.toml";
pub const DEFAULT_WINDOWS_CONFIG_FILE_PATH: &str = "bottom/bottom.toml"; pub const DEFAULT_WINDOWS_CONFIG_FILE_PATH: &str = "bottom/bottom.toml";

View File

@ -91,7 +91,6 @@ fn get_matches() -> clap::ArgMatches<'static> {
(@arg DEFAULT_WIDGET_COUNT: --default_widget_count +takes_value "Which number of the selected widget type to select, from left to right, top to bottom. Defaults to 1.") (@arg DEFAULT_WIDGET_COUNT: --default_widget_count +takes_value "Which number of the selected widget type to select, from left to right, top to bottom. Defaults to 1.")
(@arg USE_OLD_NETWORK_LEGEND: --use_old_network_legend "Use the older (pre-0.4) network widget legend.") (@arg USE_OLD_NETWORK_LEGEND: --use_old_network_legend "Use the older (pre-0.4) network widget legend.")
(@arg HIDE_TABLE_GAP: --hide_table_gap "Hides the spacing between the table headers and entries.") (@arg HIDE_TABLE_GAP: --hide_table_gap "Hides the spacing between the table headers and entries.")
//(@arg TURNED_OFF_CPUS: -t ... +takes_value "Hides CPU data points by default") // TODO: [FEATURE] Enable disabling cores in config/flags
) )
.get_matches() .get_matches()
} }

View File

@ -42,7 +42,6 @@ pub struct ConfigFlags {
pub default_widget_count: Option<u64>, pub default_widget_count: Option<u64>,
pub use_old_network_legend: Option<bool>, pub use_old_network_legend: Option<bool>,
pub hide_table_gap: Option<bool>, pub hide_table_gap: Option<bool>,
//disabled_cpu_cores: Option<Vec<u64>>, // TODO: [FEATURE] Enable disabling cores in config/flags
} }
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]