fix(tui): keep keybind footers on one line
This commit is contained in:
1 parent
63b4adaca8
commit
59707b77d2
7 files changed
+48
-109
No files matched your search
+2
-2
@@ -25,8 +25,8 @@ expression: ui.screen()
|
|||||||
"│ ││ │"
|
"│ ││ │"
|
||||||
"│ ││ │"
|
"│ ││ │"
|
||||||
"│ ││ │"
|
"│ ││ │"
|
||||||
"│ ││[l] launch [⏎] content [Shift+⏎] open dir [Esc] kill [a] add [m] modpacks│"
|
"│ ││ │"
|
||||||
"│ │╰────────────────────────────────[u] update [d] delete [r] rename [/] search╯"
|
"│ │╰[l] launch [⏎] content [Shift+⏎] open dir [Esc] kill [a] add [m] modpacks╯"
|
||||||
"│ │╭Accounts─────────╮╭Settings───────────────────────────╮╭Overview──────────────╮"
|
"│ │╭Accounts─────────╮╭Settings───────────────────────────╮╭Overview──────────────╮"
|
||||||
"│ ││No accounts. ││▸ instance default ││Ready │"
|
"│ ││No accounts. ││▸ instance default ││Ready │"
|
||||||
"│ ││ ││ ││ │"
|
"│ ││ ││ ││ │"
|
||||||
|
|||||||
+2
-2
@@ -25,8 +25,8 @@ expression: ui.screen()
|
|||||||
"│ ││ │"
|
"│ ││ │"
|
||||||
"│ ││ │"
|
"│ ││ │"
|
||||||
"│ ││ │"
|
"│ ││ │"
|
||||||
"│ ││[l] launch [⏎] content [Shift+⏎] open dir [Esc] kill [a] add [m] modpacks│"
|
"│ ││ │"
|
||||||
"│ │╰────────────────────────────────[u] update [d] delete [r] rename [/] search╯"
|
"│ │╰[l] launch [⏎] content [Shift+⏎] open dir [Esc] kill [a] add [m] modpacks╯"
|
||||||
"│ │╭Accounts─────────╮╭Settings───────────────────────────╮╭Overview──────────────╮"
|
"│ │╭Accounts─────────╮╭Settings───────────────────────────╮╭Overview──────────────╮"
|
||||||
"│ ││No accounts. ││▸ instance default ││Ready │"
|
"│ ││No accounts. ││▸ instance default ││Ready │"
|
||||||
"│ ││ ││ ││ │"
|
"│ ││ ││ ││ │"
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
use super::{
|
use super::{
|
||||||
confirm::{ConfirmTarget, confirm_popup_area},
|
confirm::{ConfirmTarget, confirm_popup_area},
|
||||||
keybind_lines_wrapped, word_wrap_size,
|
keybind_line_fitted, word_wrap_size,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn keybind_wrapping_uses_terminal_width_for_unicode_keys() {
|
fn fitted_keybinds_use_terminal_width_and_omit_overflow() {
|
||||||
let lines = keybind_lines_wrapped(&[("⏎", " select"), ("a", " add")], 19);
|
let line = keybind_line_fitted(&[("⏎", " select"), ("a", " add")], 10);
|
||||||
|
|
||||||
assert_eq!(lines.len(), 1);
|
assert_eq!(line.width(), 10);
|
||||||
assert_eq!(lines[0].width(), 19);
|
assert_eq!(line.to_string(), "[⏎] select");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -220,21 +220,20 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut A
|
|||||||
.border_type(BORDER_STYLE.to_border_type())
|
.border_type(BORDER_STYLE.to_border_type())
|
||||||
.border_style(Style::default().fg(color));
|
.border_style(Style::default().fg(color));
|
||||||
|
|
||||||
let keybind_lines = if focused == FocusedArea::Account {
|
let keybind_line = if focused == FocusedArea::Account {
|
||||||
super::popups::keybind_lines_wrapped(
|
Some(super::popups::keybind_line_fitted(
|
||||||
&[("⏎", " select"), ("a", " add"), ("d", " del")],
|
&[("⏎", " select"), ("a", " add"), ("d", " del")],
|
||||||
area.width.saturating_sub(2),
|
area.width.saturating_sub(2),
|
||||||
)
|
))
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
None
|
||||||
};
|
};
|
||||||
if let Some(line) = keybind_lines.last() {
|
if let Some(line) = keybind_line {
|
||||||
block = block.title_bottom(line.clone());
|
block = block.title_bottom(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut inner = block.inner(area);
|
let inner = block.inner(area);
|
||||||
frame.render_widget(block, area);
|
frame.render_widget(block, area);
|
||||||
inner = super::popups::render_keybind_overflow(frame, inner, &keybind_lines);
|
|
||||||
|
|
||||||
if state.store.accounts.is_empty() {
|
if state.store.accounts.is_empty() {
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
|
|||||||
@@ -345,7 +345,7 @@ pub fn render(
|
|||||||
("Shift+⏎", " open dir"),
|
("Shift+⏎", " open dir"),
|
||||||
("h/l", " tabs"),
|
("h/l", " tabs"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " discover"),
|
("Tab", " discovery"),
|
||||||
],
|
],
|
||||||
(ContentMode::Installed, ContentTab::Worlds) if open_world_datapacks.is_some() => &[
|
(ContentMode::Installed, ContentTab::Worlds) if open_world_datapacks.is_some() => &[
|
||||||
("j/k", " navigate"),
|
("j/k", " navigate"),
|
||||||
@@ -364,7 +364,7 @@ pub fn render(
|
|||||||
("Shift+⏎", " open dir"),
|
("Shift+⏎", " open dir"),
|
||||||
("h/l", " tabs"),
|
("h/l", " tabs"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " discover"),
|
("Tab", " discovery"),
|
||||||
],
|
],
|
||||||
(ContentMode::Installed, ContentTab::Worlds) => &[
|
(ContentMode::Installed, ContentTab::Worlds) => &[
|
||||||
("j/k", " navigate"),
|
("j/k", " navigate"),
|
||||||
@@ -373,7 +373,7 @@ pub fn render(
|
|||||||
("Shift+⏎", " open dir"),
|
("Shift+⏎", " open dir"),
|
||||||
("h/l", " tabs"),
|
("h/l", " tabs"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " discover"),
|
("Tab", " discovery"),
|
||||||
],
|
],
|
||||||
(ContentMode::Installed, ContentTab::DataPacks) => &[],
|
(ContentMode::Installed, ContentTab::DataPacks) => &[],
|
||||||
(ContentMode::Installed, ContentTab::Screenshots) => &[
|
(ContentMode::Installed, ContentTab::Screenshots) => &[
|
||||||
@@ -383,7 +383,7 @@ pub fn render(
|
|||||||
("Shift+⏎", " open dir"),
|
("Shift+⏎", " open dir"),
|
||||||
("h/l", " tabs"),
|
("h/l", " tabs"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " discover"),
|
("Tab", " discovery"),
|
||||||
],
|
],
|
||||||
(ContentMode::Installed, ContentTab::Logs) => {
|
(ContentMode::Installed, ContentTab::Logs) => {
|
||||||
if logs_state.viewer_focused {
|
if logs_state.viewer_focused {
|
||||||
@@ -393,7 +393,7 @@ pub fn render(
|
|||||||
("d", " delete"),
|
("d", " delete"),
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " discover"),
|
("Tab", " discovery"),
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
&[
|
&[
|
||||||
@@ -402,7 +402,7 @@ pub fn render(
|
|||||||
("d", " delete"),
|
("d", " delete"),
|
||||||
("h/l", " tabs"),
|
("h/l", " tabs"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " discover"),
|
("Tab", " discovery"),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -428,16 +428,14 @@ pub fn render(
|
|||||||
if is_focused && mode == ContentMode::Installed && !has_updates {
|
if is_focused && mode == ContentMode::Installed && !has_updates {
|
||||||
keybinds.retain(|(key, _)| *key != "u");
|
keybinds.retain(|(key, _)| *key != "u");
|
||||||
}
|
}
|
||||||
let keybind_lines =
|
// The main panel footer stays on its border; lower-priority hints are omitted when narrow.
|
||||||
crate::tui::widgets::popups::keybind_lines_wrapped(&keybinds, area.width.saturating_sub(2));
|
block = block.title_bottom(crate::tui::widgets::popups::keybind_line_fitted(
|
||||||
if let Some(line) = keybind_lines.last() {
|
&keybinds,
|
||||||
block = block.title_bottom(line.clone());
|
area.width.saturating_sub(2),
|
||||||
}
|
));
|
||||||
|
|
||||||
let mut content_area = block.inner(area);
|
let content_area = block.inner(area);
|
||||||
frame.render_widget(block, area);
|
frame.render_widget(block, area);
|
||||||
content_area =
|
|
||||||
crate::tui::widgets::popups::render_keybind_overflow(frame, content_area, &keybind_lines);
|
|
||||||
|
|
||||||
let downloadable_state = match tab {
|
let downloadable_state = match tab {
|
||||||
ContentTab::Mods => Some((mods_state, mods_discovery_state)),
|
ContentTab::Mods => Some((mods_state, mods_discovery_state)),
|
||||||
|
|||||||
@@ -11,12 +11,7 @@ pub mod new_instance;
|
|||||||
|
|
||||||
pub use load_state::LoadState;
|
pub use load_state::LoadState;
|
||||||
|
|
||||||
use ratatui::{
|
use ratatui::{layout::Rect, text::Span};
|
||||||
Frame,
|
|
||||||
layout::{Alignment, Constraint, Layout, Rect},
|
|
||||||
text::{Line, Span},
|
|
||||||
widgets::Paragraph,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) fn compare_game_versions(a: &str, b: &str) -> std::cmp::Ordering {
|
pub(crate) fn compare_game_versions(a: &str, b: &str) -> std::cmp::Ordering {
|
||||||
let parse_parts = |version: &str| {
|
let parse_parts = |version: &str| {
|
||||||
@@ -106,74 +101,19 @@ pub fn keybind_line(binds: &[(&str, &str)]) -> ratatui::text::Line<'static> {
|
|||||||
Line::from(spans)
|
Line::from(spans)
|
||||||
}
|
}
|
||||||
|
|
||||||
// same as keybind_line but wraps to multiple rows when the panel is too narrow
|
pub fn keybind_line_fitted(binds: &[(&str, &str)], max_width: u16) -> ratatui::text::Line<'static> {
|
||||||
// to fit everything on one line
|
let mut width = 0;
|
||||||
pub fn keybind_lines_wrapped(
|
let mut count = 0;
|
||||||
binds: &[(&str, &str)],
|
for (key, label) in binds {
|
||||||
max_width: u16,
|
let item_width = Span::raw(format!("[{key}]{label}")).width() + usize::from(count > 0) * 2;
|
||||||
) -> Vec<ratatui::text::Line<'static>> {
|
if width + item_width > max_width as usize {
|
||||||
use crate::config::theme::THEME;
|
break;
|
||||||
use ratatui::{
|
|
||||||
style::{Modifier, Style},
|
|
||||||
text::{Line, Span},
|
|
||||||
};
|
|
||||||
let theme = THEME.as_ref();
|
|
||||||
let key_style = Style::default()
|
|
||||||
.fg(theme.accent())
|
|
||||||
.add_modifier(Modifier::BOLD);
|
|
||||||
let label_style = Style::default().fg(theme.text());
|
|
||||||
|
|
||||||
let mut rows: Vec<Line<'static>> = Vec::new();
|
|
||||||
let mut current_spans: Vec<Span<'static>> = Vec::new();
|
|
||||||
let mut current_width: usize = 0;
|
|
||||||
|
|
||||||
for (i, (key, label)) in binds.iter().enumerate() {
|
|
||||||
let sep_w = if i > 0 && !current_spans.is_empty() {
|
|
||||||
2
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
};
|
|
||||||
let item_w = Span::raw(format!("[{key}]{label}")).width();
|
|
||||||
let needed = sep_w + item_w;
|
|
||||||
|
|
||||||
if !current_spans.is_empty() && current_width + needed > max_width as usize {
|
|
||||||
rows.push(Line::from(current_spans).right_aligned());
|
|
||||||
current_spans = Vec::new();
|
|
||||||
current_width = 0;
|
|
||||||
}
|
}
|
||||||
|
width += item_width;
|
||||||
if !current_spans.is_empty() {
|
count += 1;
|
||||||
current_spans.push(Span::styled(" ", label_style));
|
|
||||||
current_width += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_spans.push(Span::styled(format!("[{}]", key), key_style));
|
|
||||||
if !label.is_empty() {
|
|
||||||
current_spans.push(Span::styled(label.to_string(), label_style));
|
|
||||||
}
|
|
||||||
current_width += item_w;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !current_spans.is_empty() {
|
keybind_line(&binds[..count]).right_aligned()
|
||||||
rows.push(Line::from(current_spans).right_aligned());
|
|
||||||
}
|
|
||||||
|
|
||||||
rows
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render_keybind_overflow(frame: &mut Frame, area: Rect, lines: &[Line<'static>]) -> Rect {
|
|
||||||
let overflow = lines.len().saturating_sub(1);
|
|
||||||
if overflow == 0 {
|
|
||||||
return area;
|
|
||||||
}
|
|
||||||
|
|
||||||
let [content, footer] =
|
|
||||||
Layout::vertical([Constraint::Min(0), Constraint::Length(overflow as u16)]).areas(area);
|
|
||||||
frame.render_widget(
|
|
||||||
Paragraph::new(lines[..overflow].to_vec()).alignment(Alignment::Right),
|
|
||||||
footer,
|
|
||||||
);
|
|
||||||
content
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ pub fn render(
|
|||||||
.border_type(BORDER_STYLE.to_border_type())
|
.border_type(BORDER_STYLE.to_border_type())
|
||||||
.border_style(Style::default().fg(color));
|
.border_style(Style::default().fg(color));
|
||||||
|
|
||||||
let keybind_lines = if focused == FocusedArea::Settings {
|
let keybind_line = if focused == FocusedArea::Settings {
|
||||||
let keybinds: &[(&str, &str)] = match state.pane {
|
let keybinds: &[(&str, &str)] = match state.pane {
|
||||||
SettingsPane::Profile => &[
|
SettingsPane::Profile => &[
|
||||||
("⏎", " select"),
|
("⏎", " select"),
|
||||||
@@ -226,17 +226,19 @@ pub fn render(
|
|||||||
("h/l", " tab"),
|
("h/l", " tab"),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
super::popups::keybind_lines_wrapped(keybinds, area.width.saturating_sub(2))
|
Some(super::popups::keybind_line_fitted(
|
||||||
|
keybinds,
|
||||||
|
area.width.saturating_sub(2),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
None
|
||||||
};
|
};
|
||||||
if let Some(line) = keybind_lines.last() {
|
if let Some(line) = keybind_line {
|
||||||
block = block.title_bottom(line.clone());
|
block = block.title_bottom(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut inner = block.inner(area);
|
let inner = block.inner(area);
|
||||||
frame.render_widget(block, area);
|
frame.render_widget(block, area);
|
||||||
inner = super::popups::render_keybind_overflow(frame, inner, &keybind_lines);
|
|
||||||
|
|
||||||
if inner.width >= 42 {
|
if inner.width >= 42 {
|
||||||
let chunks = Layout::default()
|
let chunks = Layout::default()
|
||||||
|
|||||||
Reference in new issue
Block a user