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 enum AppHelpCategory {
General,
Process,
Search,
}
#[derive(Default)]
pub struct AppHelpDialogState {
pub is_showing_help: bool,
pub scroll_state: ParagraphScrollState,
pub general_index: u16,
pub cpu_index: u16,
pub process_index: u16,
pub search_index: u16,
pub battery_index: u16,
pub index_shortcuts: Vec<u16>,
}
impl Default for AppHelpDialogState {
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
@ -583,11 +582,16 @@ impl App {
pub fn on_esc(&mut self) {
self.reset_multi_tap_keys();
if self.is_in_dialog() {
self.help_dialog_state.is_showing_help = false;
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;
if self.help_dialog_state.is_showing_help {
self.help_dialog_state.is_showing_help = false;
self.help_dialog_state.scroll_state.current_scroll_index = 0;
} else {
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;
} else if self.is_filtering_or_searching() {
match self.current_widget.widget_type {
@ -1441,11 +1445,11 @@ impl App {
// 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.
match caught_char {
'1' => self.help_scroll_to_or_max(self.help_dialog_state.general_index),
'2' => self.help_scroll_to_or_max(self.help_dialog_state.cpu_index),
'3' => self.help_scroll_to_or_max(self.help_dialog_state.process_index),
'4' => self.help_scroll_to_or_max(self.help_dialog_state.search_index),
'5' => self.help_scroll_to_or_max(self.help_dialog_state.battery_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.index_shortcuts[2]),
'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.index_shortcuts[4]),
'5' => self.help_scroll_to_or_max(self.help_dialog_state.index_shortcuts[5]),
'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");
// Init help text:
// ToC
self.styled_help_text.extend(
HELP_CONTENTS_TEXT
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
// General
self.styled_help_text.push(Text::Raw("\n\n".into()));
self.styled_help_text.push(Text::Styled(
GENERAL_HELP_TEXT[0].into(),
self.colours.table_header_style,
));
self.styled_help_text.extend(
GENERAL_HELP_TEXT[1..]
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
// CPU
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<_>>(),
);
(*HELP_TEXT).iter().enumerate().for_each(|(itx, section)| {
if itx == 0 {
self.styled_help_text.extend(
section
.iter()
.map(|&text| Text::Styled(text.into(), self.colours.text_style))
.collect::<Vec<_>>(),
);
} else {
// Not required check but it runs only a few times... so whatever ig, prevents me from
// being dumb and leaving a help text section only one line long.
if section.len() > 1 {
self.styled_help_text.push(Text::Raw("\n\n".into()));
self.styled_help_text.push(Text::Styled(
section[0].into(),
self.colours.table_header_style,
));
self.styled_help_text.extend(
section[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>(
&mut self, terminal: &mut Terminal<B>, app_state: &mut app::App,
) -> error::Result<()> {
@ -256,8 +209,6 @@ impl Painter {
terminal.autoresize()?;
terminal.draw(|mut f| {
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 border_len = (max(0, f.size().height as i64 - gen_help_len as i64)) as u16 / 2;
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
// small terminal sizes... oh joy.
// TODO: Make this more automated and easier to add.
let mut overflow_buffer = 0;
let paragraph_width = draw_loc.width - 2;
constants::HELP_CONTENTS_TEXT.iter().for_each(|text_line| {
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
let mut prev_section_len = 0;
// General
app_state.help_dialog_state.general_index =
constants::HELP_CONTENTS_TEXT.len() as u16 + 1 + overflow_buffer;
constants::GENERAL_HELP_TEXT.iter().for_each(|text_line| {
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
constants::HELP_TEXT
.iter()
.enumerate()
.for_each(|(itx, section)| {
let mut buffer = 0;
// CPU
app_state.help_dialog_state.cpu_index =
(constants::HELP_CONTENTS_TEXT.len() + constants::GENERAL_HELP_TEXT.len()) as u16
+ 2
+ overflow_buffer;
constants::CPU_HELP_TEXT.iter().for_each(|text_line| {
overflow_buffer +=
UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
});
if itx == 0 {
section.iter().for_each(|text_line| {
buffer += UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16
/ paragraph_width;
});
// Processes
app_state.help_dialog_state.process_index = (constants::HELP_CONTENTS_TEXT.len()
+ constants::GENERAL_HELP_TEXT.len()
+ constants::CPU_HELP_TEXT.len())
as u16
+ 3
+ overflow_buffer;
constants::PROCESS_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.index_shortcuts[itx] = 0;
prev_section_len = section.len() as u16 + buffer;
overflow_buffer += buffer;
} else {
section.iter().for_each(|text_line| {
buffer += UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16
/ paragraph_width;
});
// Search
app_state.help_dialog_state.search_index = (constants::HELP_CONTENTS_TEXT.len()
+ constants::GENERAL_HELP_TEXT.len()
+ constants::CPU_HELP_TEXT.len()
+ constants::PROCESS_HELP_TEXT.len())
as u16
+ 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.index_shortcuts[itx] =
app_state.help_dialog_state.index_shortcuts[itx - 1]
+ 1
+ prev_section_len;
prev_section_len = section.len() as u16 + buffer;
overflow_buffer += buffer;
}
});
app_state.help_dialog_state.scroll_state.max_scroll_index =
(self.styled_help_text.len() as u16
+ (constants::NUM_CATEGORIES - 3)
+ (constants::HELP_TEXT.len() as u16 - 3)
+ overflow_buffer)
.saturating_sub(draw_loc.height);

View File

@ -36,8 +36,6 @@ lazy_static! {
}
// Help text
pub const NUM_CATEGORIES: u16 = 6;
pub const HELP_CONTENTS_TEXT: [&str; 6] = [
"Press the corresponding numbers to jump to the section, or scroll:\n",
"1 - General bindings\n",
@ -108,6 +106,17 @@ pub const BATTERY_HELP_TEXT: [&str; 3] = [
"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
pub const DEFAULT_UNIX_CONFIG_FILE_PATH: &str = ".config/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 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 TURNED_OFF_CPUS: -t ... +takes_value "Hides CPU data points by default") // TODO: [FEATURE] Enable disabling cores in config/flags
)
.get_matches()
}

View File

@ -42,7 +42,6 @@ pub struct ConfigFlags {
pub default_widget_count: Option<u64>,
pub use_old_network_legend: 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)]