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
@@ -11,12 +11,7 @@ pub mod new_instance;
|
||||
|
||||
pub use load_state::LoadState;
|
||||
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Alignment, Constraint, Layout, Rect},
|
||||
text::{Line, Span},
|
||||
widgets::Paragraph,
|
||||
};
|
||||
use ratatui::{layout::Rect, text::Span};
|
||||
|
||||
pub(crate) fn compare_game_versions(a: &str, b: &str) -> std::cmp::Ordering {
|
||||
let parse_parts = |version: &str| {
|
||||
@@ -106,74 +101,19 @@ pub fn keybind_line(binds: &[(&str, &str)]) -> ratatui::text::Line<'static> {
|
||||
Line::from(spans)
|
||||
}
|
||||
|
||||
// same as keybind_line but wraps to multiple rows when the panel is too narrow
|
||||
// to fit everything on one line
|
||||
pub fn keybind_lines_wrapped(
|
||||
binds: &[(&str, &str)],
|
||||
max_width: u16,
|
||||
) -> Vec<ratatui::text::Line<'static>> {
|
||||
use crate::config::theme::THEME;
|
||||
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;
|
||||
pub fn keybind_line_fitted(binds: &[(&str, &str)], max_width: u16) -> ratatui::text::Line<'static> {
|
||||
let mut width = 0;
|
||||
let mut count = 0;
|
||||
for (key, label) in binds {
|
||||
let item_width = Span::raw(format!("[{key}]{label}")).width() + usize::from(count > 0) * 2;
|
||||
if width + item_width > max_width as usize {
|
||||
break;
|
||||
}
|
||||
|
||||
if !current_spans.is_empty() {
|
||||
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;
|
||||
width += item_width;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
if !current_spans.is_empty() {
|
||||
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
|
||||
keybind_line(&binds[..count]).right_aligned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user