generated from GarandPLG/rust-flake-template
Refactor to View enum and add keybindings
Replace the `default_window` string field with a `View` enum throughout the application. Introduce a new keybinding module providing `Action`, `KeyBinding` structs and helper functions, and integrate a keybindings widget into the main menu. Update the CLI to accept a `View` value enum and adjust imports and event handling accordingly. Add a release profile in `Cargo.toml` with LTO, single codegen unit, higher optimisation level and binary stripping for smaller, faster builds.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
use ratatui::{
|
||||
crossterm::event::KeyCode,
|
||||
layout::Alignment,
|
||||
style::Stylize,
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
};
|
||||
|
||||
use crate::app::{View, binding_for_view};
|
||||
|
||||
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::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 ]"),
|
||||
)
|
||||
}
|
||||
@@ -1,47 +1,42 @@
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Alignment, Constraint, Layout, Rect},
|
||||
style::{Color, Stylize},
|
||||
text::Line,
|
||||
style::Color,
|
||||
widgets::{Block, Borders, Paragraph, Widget},
|
||||
};
|
||||
|
||||
use crate::app::{View, widgets::keybindings_widget};
|
||||
|
||||
pub fn main_menu_widget(area: Rect, buf: &mut Buffer) {
|
||||
let vertical_layout: Layout =
|
||||
Layout::vertical([Constraint::Percentage(90), Constraint::Percentage(10)]);
|
||||
|
||||
let [main_menu_area, keybindings_area] = vertical_layout.areas(area);
|
||||
|
||||
let title_text: String = vec![
|
||||
r" __ __ _ _____ _ ",
|
||||
r"/ / /\ \ \__ _ _ __ (_)_ __ /__ \_ _ _ __ _ __ ___| |___",
|
||||
r"\ \/ \/ / _` | '__| | | '_ \ / /\/ | | | '_ \| '_ \ / _ \ / __|",
|
||||
r" \ /\ / (_| | | | | | | | / / | |_| | | | | | | | __/ \__ \",
|
||||
r" \/ \/ \__,_|_| |_|_| |_| \/ \__,_|_| |_|_| |_|\___|_|___/",
|
||||
]
|
||||
.join("\n");
|
||||
{
|
||||
let title_text: String = vec![
|
||||
r" __ __ _ _____ _ ",
|
||||
r"/ / /\ \ \__ _ _ __ (_)_ __ /__ \_ _ _ __ _ __ ___| |___",
|
||||
r"\ \/ \/ / _` | '__| | | '_ \ / /\/ | | | '_ \| '_ \ / _ \ / __|",
|
||||
r" \ /\ / (_| | | | | | | | / / | |_| | | | | | | | __/ \__ \",
|
||||
r" \/ \/ \__,_|_| |_|_| |_| \/ \__,_|_| |_|_| |_|\___|_|___/",
|
||||
]
|
||||
.join("\n");
|
||||
|
||||
let title: Paragraph<'_> = Paragraph::new(title_text)
|
||||
.alignment(Alignment::Center)
|
||||
.style(Color::Yellow)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::LEFT | Borders::RIGHT | Borders::TOP)
|
||||
.style(Color::Gray),
|
||||
);
|
||||
let title: Paragraph<'_> = Paragraph::new(title_text)
|
||||
.alignment(Alignment::Center)
|
||||
.style(Color::Yellow)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::LEFT | Borders::RIGHT | Borders::TOP)
|
||||
.style(Color::Gray),
|
||||
);
|
||||
|
||||
title.render(main_menu_area, buf);
|
||||
title.render(main_menu_area, buf);
|
||||
}
|
||||
|
||||
let keybindings_text: Vec<Line<'_>> =
|
||||
vec![Line::from_iter(["q".bold().red(), "\t - Quit".into()])];
|
||||
|
||||
let keybindings: Paragraph<'_> = Paragraph::new(keybindings_text)
|
||||
.alignment(Alignment::Left)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title("[ Keybinding ]"),
|
||||
);
|
||||
|
||||
keybindings.render(keybindings_area, buf);
|
||||
{
|
||||
let keybindings: Paragraph<'_> = keybindings_widget(View::MainMenu);
|
||||
keybindings.render(keybindings_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod keybindings;
|
||||
pub mod main_menu;
|
||||
|
||||
pub use keybindings::keybindings_widget;
|
||||
pub use main_menu::main_menu_widget;
|
||||
|
||||
Reference in New Issue
Block a user