generated from GarandPLG/rust-flake-template
Refactor keybindings widget and reorder Group enum
- Introduce `KeybindingsWidget::new(view)` constructor and remove the standalone `keybindings_widget` function. - Update all imports and call sites to use `KeybindingsWidget` directly. - Reorder `Group` enum to place `Quit` after `Select` and adjust its iterator accordingly.
This commit is contained in:
1 parent
073e70441f
commit
70a8ac348e
5 files changed
+44
-40
No files matched your search
@@ -14,6 +14,43 @@ pub struct KeybindingsWidget {
|
||||
grouped: Vec<Paragraph<'static>>,
|
||||
}
|
||||
|
||||
impl KeybindingsWidget {
|
||||
pub fn new(view: View) -> Self {
|
||||
let (used_groups, keybindings) = binding_for_view(view);
|
||||
|
||||
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
|
||||
for (i, group) in Group::iter().enumerate() {
|
||||
if !used_groups.contains(&group) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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 != 0 {
|
||||
block = block.borders(Borders::LEFT);
|
||||
}
|
||||
|
||||
grouped_keybindings.push(
|
||||
Paragraph::new(grouped_lines)
|
||||
.alignment(Alignment::Center)
|
||||
.block(block),
|
||||
);
|
||||
}
|
||||
|
||||
Self {
|
||||
grouped: grouped_keybindings,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for KeybindingsWidget {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
let count: u16 = self.grouped.len() as u16;
|
||||
@@ -41,36 +78,3 @@ impl Widget for KeybindingsWidget {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keybindings_widget(view: View) -> KeybindingsWidget {
|
||||
let (used_groups, keybindings) = binding_for_view(view);
|
||||
|
||||
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
|
||||
for (i, group) in Group::iter().enumerate() {
|
||||
if !used_groups.contains(&group) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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 != 0 {
|
||||
block = block.borders(Borders::LEFT);
|
||||
}
|
||||
|
||||
grouped_keybindings.push(
|
||||
Paragraph::new(grouped_lines)
|
||||
.alignment(Alignment::Center)
|
||||
.block(block),
|
||||
);
|
||||
}
|
||||
|
||||
KeybindingsWidget {
|
||||
grouped: grouped_keybindings,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user