From c97cb063d142b8190e55e930d437ce3895335637 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Sat, 15 Nov 2025 13:56:10 -0500 Subject: [PATCH] other: clean up help text spacing + help text dialog sizing (#1865) * other: clean up help text spacing + help text dialog sizing * use 'safer' saturating, I think it gets optimized out...? * semicolon + typo --- src/canvas.rs | 68 ++++++++++----- src/constants.rs | 220 +++++++++++++++++++++++------------------------ 2 files changed, 155 insertions(+), 133 deletions(-) diff --git a/src/canvas.rs b/src/canvas.rs index 463f37ec..41dd2490 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -11,7 +11,7 @@ mod widgets; use tui::{ Frame, Terminal, backend::Backend, - layout::{Constraint, Direction, Layout, Rect}, + layout::{Constraint, Direction, Flex, Layout, Rect}, text::Span, widgets::Paragraph, }; @@ -122,35 +122,61 @@ impl Painter { if app_state.help_dialog_state.is_showing_help { let gen_help_len = GENERAL_HELP_TEXT.len() as u16 + 3; let border_len = terminal_height.saturating_sub(gen_help_len) / 2; - let vertical_dialog_chunk = Layout::default() + let [_, vertical_dialog_chunk, _] = Layout::default() .direction(Direction::Vertical) .constraints([ Constraint::Length(border_len), Constraint::Length(gen_help_len), Constraint::Length(border_len), ]) - .split(terminal_size); + .areas(terminal_size); - let middle_dialog_chunk = Layout::default() - .direction(Direction::Horizontal) - .constraints(if terminal_width < 100 { - // TODO: [REFACTOR] The point we start changing size at currently hard-coded - // in. - [ - Constraint::Percentage(0), - Constraint::Percentage(100), - Constraint::Percentage(0), - ] + // An approximate proxy for the max line length to use. + const MAX_TEXT_LENGTH: u16 = const { + let mut max = 0; + + let mut i = 0; + while i < HELP_TEXT.len() { + let section = HELP_TEXT[i]; + let mut j = 0; + while j < section.len() { + let line = section[j]; + if line.len() > max { + max = line.len(); + } + + j += 1; + } + + i += 1; + } + + max as u16 + }; + + let dialog_width = vertical_dialog_chunk.width; + let [middle_dialog_chunk] = if dialog_width < MAX_TEXT_LENGTH { + Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Percentage(100)]) + .areas(vertical_dialog_chunk) + } else { + // We calculate this so that the margins never have to split an odd number. + let len = if (dialog_width.saturating_sub(MAX_TEXT_LENGTH)) % 2 == 0 { + MAX_TEXT_LENGTH } else { - [ - Constraint::Percentage(15), - Constraint::Percentage(70), - Constraint::Percentage(15), - ] - }) - .split(vertical_dialog_chunk[1]); + // It can only be 1 if the difference is greater than 1, so this is fine. + MAX_TEXT_LENGTH + 1 + }; - self.draw_help_dialog(f, app_state, middle_dialog_chunk[1]); + Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Length(len)]) + .flex(Flex::SpaceAround) + .areas(vertical_dialog_chunk) + }; + + self.draw_help_dialog(f, app_state, middle_dialog_chunk); } else if app_state.process_kill_dialog.is_open() { // FIXME: For width, just limit to a max size or full width. For height, not sure. Maybe pass max and let child handle? let horizontal_padding = if terminal_width < 100 { 0 } else { 5 }; diff --git a/src/constants.rs b/src/constants.rs index f1d609ef..927697f5 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -25,159 +25,155 @@ const HELP_CONTENTS_TEXT: [&str; 10] = [ // TODO [Help]: Search in help? // TODO [Help]: Move to using tables for easier formatting? -pub(crate) const GENERAL_HELP_TEXT: [&str; 32] = [ +pub(crate) const GENERAL_HELP_TEXT: [&str; 28] = [ "1 - General", - "q, Ctrl-c Quit", - "Esc Close dialog windows, search, widgets, or exit expanded mode", - "Ctrl-r Reset display and any collected data", - "f Freeze/unfreeze updating with new data", - "Ctrl-Left, ", - "Shift-Left, Move widget selection left", - "H, A ", - "Ctrl-Right, ", - "Shift-Right, Move widget selection right", - "L, D ", - "Ctrl-Up, ", - "Shift-Up, Move widget selection up", - "K, W ", - "Ctrl-Down, ", - "Shift-Down, Move widget selection down", - "J, S ", - "Left, h Move left within widget", - "Down, j Move down within widget", - "Up, k Move up within widget", - "Right, l Move right within widget", - "? Open help menu", - "gg Jump to the first entry", - "G Jump to the last entry", - "e Toggle expanding the currently selected widget", - "+ Zoom in on chart (decrease time range)", - "- Zoom out on chart (increase time range)", - "= Reset zoom", - "PgUp, PgDown Scroll up/down a table by a page", - "Ctrl-u, Ctrl-d Scroll up/down a table by half a page", - "Mouse scroll Scroll through the tables or zoom in/out of charts by scrolling up/down", - "Mouse click Selects the clicked widget, table entry, dialog option, or tab", + "q, Ctrl-c Quit", + "Esc Close dialog windows, search, widgets, or exit expanded mode", + "Ctrl-r Reset display and any collected data", + "f Freeze/unfreeze updating with new data", + "Ctrl-Left, ", + "Shift-Left, H, A Move widget selection left", + "Ctrl-Right, ", + "Shift-Right, L, D Move widget selection right", + "Ctrl-Up, ", + "Shift-Up, K, W Move widget selection up", + "Ctrl-Down, ", + "Shift-Down, J, S Move widget selection down", + "Left, h Move left within widget", + "Down, j Move down within widget", + "Up, k Move up within widget", + "Right, l Move right within widget", + "? Open help menu", + "gg Jump to the first entry", + "G Jump to the last entry", + "e Toggle expanding the currently selected widget", + "+ Zoom in on chart (decrease time range)", + "- Zoom out on chart (increase time range)", + "= Reset zoom", + "PgUp, PgDown Scroll up/down a table by a page", + "Ctrl-u, Ctrl-d Scroll up/down a table by half a page", + "Mouse scroll Scroll through the tables or zoom in/out of charts by scrolling up/down", + "Mouse click Selects the clicked widget, table entry, dialog option, or tab", ]; const CPU_HELP_TEXT: [&str; 2] = [ "2 - CPU widget", - "Mouse scroll Scrolling over an CPU core/average shows only that entry on the chart", + "Mouse scroll Scrolling over a CPU core/average shows only that entry on the chart", ]; const PROCESS_HELP_TEXT: [&str; 20] = [ "3 - Process widget", - "dd, F9, Delete Kill the selected process", - "c Sort by CPU usage, press again to reverse", - "m Sort by memory usage, press again to reverse", - "p Sort by PID name, press again to reverse", - "n Sort by process name, press again to reverse", - "Tab Group/un-group processes with the same name", - "Ctrl-f, / Open process search widget", - "P Toggle between showing the full command or just the process name", - "s, F6 Open process sort widget", - "I Invert current sort", - "% Toggle between values and percentages for memory usage", - "t, F5 Toggle tree mode", - "Right Collapse a branch while in tree mode", - "Left Expand a branch while in tree mode", - "+, -, click Toggle whether a branch is expanded or collapsed in tree mode", - "click on header Sorts the entries by that column, click again to invert the sort", - "C Sort by GPU usage, press again to reverse", - "M Sort by GPU memory usage, press again to reverse", - "z Toggle the display of kernel threads", + "dd, F9, Delete Kill the selected process", + "c Sort by CPU usage, press again to reverse", + "m Sort by memory usage, press again to reverse", + "p Sort by PID name, press again to reverse", + "n Sort by process name, press again to reverse", + "Tab Group/un-group processes with the same name", + "Ctrl-f, / Open process search widget", + "P Toggle between showing the full command or just the process name", + "s, F6 Open process sort widget", + "I Invert current sort", + "% Toggle between values and percentages for memory usage", + "t, F5 Toggle tree mode", + "Right Collapse a branch while in tree mode", + "Left Expand a branch while in tree mode", + "+, -, click Toggle whether a branch is expanded or collapsed in tree mode", + "click on header Sorts the entries by that column, click again to invert the sort", + "C Sort by GPU usage, press again to reverse", + "M Sort by GPU memory usage, press again to reverse", + "z Toggle the display of kernel threads", ]; const SEARCH_HELP_TEXT: [&str; 51] = [ "4 - Process search widget", - "Esc Close the search widget (retains the filter)", - "Ctrl-a Skip to the start of the search query", - "Ctrl-e Skip to the end of the search query", - "Ctrl-u Clear the current search query", - "Ctrl-w Delete a word behind the cursor", - "Ctrl-h Delete the character behind the cursor", - "Backspace Delete the character behind the cursor", - "Delete Delete the character at the cursor", - "Alt-c, F1 Toggle matching case", - "Alt-w, F2 Toggle matching the entire word", - "Alt-r, F3 Toggle using regex", - "Left, Alt-h Move cursor left", - "Right, Alt-l Move cursor right", + "Esc Close the search widget (retains the filter)", + "Ctrl-a Skip to the start of the search query", + "Ctrl-e Skip to the end of the search query", + "Ctrl-u Clear the current search query", + "Ctrl-w Delete a word behind the cursor", + "Ctrl-h Delete the character behind the cursor", + "Backspace Delete the character behind the cursor", + "Delete Delete the character at the cursor", + "Alt-c, F1 Toggle matching case", + "Alt-w, F2 Toggle matching the entire word", + "Alt-r, F3 Toggle using regex", + "Left, Alt-h Move cursor left", + "Right, Alt-l Move cursor right", "", "Supported search types:", - " ex: btm", - "pid ex: pid 825", - "cpu, cpu% ex: cpu > 4.2", - "mem, mem% ex: mem < 4.2", - "memb ex: memb < 100 kb", - "read, r/s, rps ex: read >= 1 b", - "write, w/s, wps ex: write <= 1 tb", - "tread, t.read ex: tread = 1", - "twrite, t.write ex: twrite = 1", - "user ex: user = root", - "state ex: state = running", - "gpu% ex: gpu% < 4.2", - "gmem ex: gmem < 100 kb", - "gmem% ex: gmem% < 4.2", + " ex: btm", + "pid ex: pid 825", + "cpu, cpu% ex: cpu > 4.2", + "mem, mem% ex: mem < 4.2", + "memb ex: memb < 100 kb", + "read, r/s, rps ex: read >= 1 b", + "write, w/s, wps ex: write <= 1 tb", + "tread, t.read ex: tread = 1", + "twrite, t.write ex: twrite = 1", + "user ex: user = root", + "state ex: state = running", + "gpu% ex: gpu% < 4.2", + "gmem ex: gmem < 100 kb", + "gmem% ex: gmem% < 4.2", "", "Comparison operators:", - "= ex: cpu = 1", - "> ex: cpu > 1", - "< ex: cpu < 1", - ">= ex: cpu >= 1", - "<= ex: cpu <= 1", + "= ex: cpu = 1", + "> ex: cpu > 1", + "< ex: cpu < 1", + ">= ex: cpu >= 1", + "<= ex: cpu <= 1", "", "Logical operators:", - "and, &&, ex: btm and cpu > 1 and mem > 1", - "or, || ex: btm or firefox", + "and, &&, ex: btm and cpu > 1 and mem > 1", + "or, || ex: btm or firefox", "", "Supported units:", - "B ex: read > 1 b", - "KB ex: read > 1 kb", - "MB ex: read > 1 mb", - "TB ex: read > 1 tb", - "KiB ex: read > 1 kib", - "MiB ex: read > 1 mib", - "GiB ex: read > 1 gib", - "TiB ex: read > 1 tib", + "B ex: read > 1 b", + "KB ex: read > 1 kb", + "MB ex: read > 1 mb", + "TB ex: read > 1 tb", + "KiB ex: read > 1 kib", + "MiB ex: read > 1 mib", + "GiB ex: read > 1 gib", + "TiB ex: read > 1 tib", ]; const SORT_HELP_TEXT: [&str; 6] = [ "5 - Sort widget", - "Down, 'j' Scroll down in list", - "Up, 'k' Scroll up in list", - "Mouse scroll Scroll through sort widget", - "Esc Close the sort widget", - "Enter Sort by current selected column", + "Down, 'j' Scroll down in list", + "Up, 'k' Scroll up in list", + "Mouse scroll Scroll through sort widget", + "Esc Close the sort widget", + "Enter Sort by current selected column", ]; const TEMP_HELP_WIDGET: [&str; 3] = [ "6 - Temperature widget", - "'s' Sort by sensor name, press again to reverse", - "'t' Sort by temperature, press again to reverse", + "'s' Sort by sensor name, press again to reverse", + "'t' Sort by temperature, press again to reverse", ]; const DISK_HELP_WIDGET: [&str; 9] = [ "7 - Disk widget", - "'d' Sort by disk name, press again to reverse", - "'m' Sort by disk mount, press again to reverse", - "'u' Sort by disk usage, press again to reverse", - "'n' Sort by disk free space, press again to reverse", - "'t' Sort by total disk space, press again to reverse", - "'p' Sort by disk usage percentage, press again to reverse", - "'r' Sort by disk read activity, press again to reverse", - "'w' Sort by disk write activity, press again to reverse", + "'d' Sort by disk name, press again to reverse", + "'m' Sort by disk mount, press again to reverse", + "'u' Sort by disk usage, press again to reverse", + "'n' Sort by disk free space, press again to reverse", + "'t' Sort by total disk space, press again to reverse", + "'p' Sort by disk usage percentage, press again to reverse", + "'r' Sort by disk read activity, press again to reverse", + "'w' Sort by disk write activity, press again to reverse", ]; const BATTERY_HELP_TEXT: [&str; 3] = [ "8 - Battery widget", - "Left Go to previous battery", - "Right Go to next battery", + "Left Go to previous battery", + "Right Go to next battery", ]; const BASIC_MEM_HELP_TEXT: [&str; 2] = [ "9 - Basic memory widget", - "% Toggle between values and percentages for memory usage", + "% Toggle between values and percentages for memory usage", ]; pub(crate) const HELP_TEXT: [&[&str]; HELP_CONTENTS_TEXT.len()] = [