Refactor keybindings with groups and symbols

Introduce a `Group` enum to categorize actions and add `group` and
`symbol`
fields to `KeyBinding`. Extend `Action` with `Up`, `Down`, `Space`, and
`Esc`, updating all keybinding definitions accordingly.

Update `binding_for_view` to return the new actions per view and handle
`Esc` by returning to the main menu.

Rewrite the keybindings widget as a custom `Widget` that renders grouped
paragraphs with proper layout.

Adjust the main menu layout percentages to accommodate the new widget.
This commit is contained in:
2026-03-12 16:00:45 +01:00
parent f481f5dc87
commit 71b9d44a77
6 changed files with 126 additions and 43 deletions
+67 -28
View File
@@ -1,33 +1,72 @@
use crate::app::{View, keybindings::binding_for_view};
use crate::app::{
View,
keybindings::{Group, KeyBinding, binding_for_view},
};
use ratatui::{
crossterm::event::KeyCode,
layout::Alignment,
buffer::Buffer,
layout::{Alignment, Constraint, Layout, Rect},
style::Stylize,
text::{Line, Span},
widgets::{Block, Borders, Paragraph},
text::Line,
widgets::{Block, Borders, Padding, Paragraph, Widget},
};
pub fn keybindings_widget(view: View) -> Paragraph<'static> {
let lines: Vec<Line> = binding_for_view(view)
.iter()
.map(|b| {
let key_span = match b.code {
KeyCode::Up => Span::raw("".to_string()),
KeyCode::Down => Span::raw("".to_string()),
KeyCode::Char(' ') => Span::raw("Space".to_string()),
KeyCode::Char(c) => Span::raw(c.to_string()),
other => Span::raw(format!("{:?}", other)),
}
.bold()
.red();
Line::from_iter([key_span, "\t - ".into(), b.description.into()])
})
.collect();
Paragraph::new(lines).alignment(Alignment::Left).block(
Block::default()
.borders(Borders::ALL)
.title("[ Keybindings ]"),
)
pub struct KeybindingsWidget {
grouped: Vec<Paragraph<'static>>,
}
impl Widget for KeybindingsWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
let count: u16 = self.grouped.len() as u16;
if count == 0 {
return;
}
let block: Block<'_> = Block::default()
.borders(Borders::ALL)
.title("[ Keybindings ]");
let inner: Rect = block.inner(area);
block.render(area, buf);
let base: u16 = if count == 0 { 0 } else { 10 };
let constraints: Vec<Constraint> = vec![Constraint::Percentage(base); count as usize];
let chunks: Vec<Rect> = Layout::horizontal(constraints).split(inner).to_vec();
for (paragraph, chunk) in self.grouped.into_iter().zip(chunks.into_iter()) {
paragraph.render(chunk, buf);
}
}
}
pub fn keybindings_widget(view: View) -> KeybindingsWidget {
let keybindings: Vec<&'static KeyBinding> = binding_for_view(view);
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
for (i, group) in Group::iter().enumerate() {
let grouped_lines: Vec<Line<'_>> = keybindings
.iter()
.filter(|b| b.group == group)
.map(|b| Line::from_iter([b.symbol.red().bold(), " - ".into(), b.description.into()]))
.collect();
let mut block: Block<'_> = Block::default().padding(Padding::new(1, 1, 0, 0));
if i != Group::len() - 1 {
block = block.borders(Borders::RIGHT);
}
grouped_keybindings.push(
Paragraph::new(grouped_lines)
.alignment(Alignment::Center)
.block(block),
);
}
KeybindingsWidget {
grouped: grouped_keybindings,
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ fn view_options() -> Vec<(usize, String)> {
pub fn main_menu_widget(app: &App, area: Rect, buf: &mut Buffer) {
let vertical_layout: Layout =
Layout::vertical([Constraint::Percentage(88), Constraint::Percentage(12)]);
Layout::vertical([Constraint::Percentage(92), Constraint::Percentage(8)]);
let [main_menu_area, keybindings_area] = vertical_layout.areas(area);