generated from GarandPLG/rust-flake-template
Refactor keybindings and add validated CLI arguments
Extracted common quit/esc handling into `common_keybindings`. Introduced `Group::CtrlMovement` for Ctrl‑arrow keys and updated keybinding definitions. Removed duplicated quit logic in main_menu and skirmish modules. Bumped clap to 4.6.0 and updated Cargo.lock. Added range‑checked parsers for map size, resources, supply limit, XP modifier and skill points in the CLI.
This commit is contained in:
+5
-7
@@ -69,21 +69,19 @@ pub fn handle_input_events(tx: mpsc::Sender<Event>) {
|
||||
loop {
|
||||
match event::read() {
|
||||
Ok(ev) => match ev {
|
||||
event::Event::Key(key_event) => {
|
||||
if tx.send(Event::Input(key_event)).is_err() {
|
||||
event::Event::Key(key) => {
|
||||
if tx.send(Event::Input(key)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
event::Event::Resize(cols, rows) => {
|
||||
if tx.send(Event::Resize(cols, rows)).is_err() {
|
||||
event::Event::Resize(c, r) => {
|
||||
if tx.send(Event::Resize(c, r)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,16 @@ use crate::app::{
|
||||
};
|
||||
use ratatui::crossterm::event::KeyEvent;
|
||||
|
||||
pub fn default_keybindings(app: &mut App, key_event: &KeyEvent) {
|
||||
if let Some(action) = event_to_action(&key_event) {
|
||||
match action {
|
||||
Action::Quit => app.exit = true,
|
||||
Action::Quit2 => app.exit = true,
|
||||
Action::Esc => app.view = View::MainMenu,
|
||||
_ => (),
|
||||
}
|
||||
pub fn common_keybindings(app: &mut App, action: Action) {
|
||||
match action {
|
||||
Action::Quit | Action::Quit2 => app.exit = true,
|
||||
Action::Esc => app.view = View::MainMenu,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_keybindings(app: &mut App, key_event: &KeyEvent) {
|
||||
if let Some(action) = event_to_action(&key_event) {
|
||||
common_keybindings(app, action);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ pub enum Action {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
|
||||
pub enum Group {
|
||||
CtrlMovement,
|
||||
Movement,
|
||||
Scroll,
|
||||
Select,
|
||||
@@ -100,7 +101,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
||||
code: KeyCode::Up,
|
||||
kind: KeyEventKind::Press,
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
group: Group::Movement,
|
||||
group: Group::CtrlMovement,
|
||||
symbol: "Ctrl + ↑",
|
||||
description: "Scroll Up ",
|
||||
},
|
||||
@@ -109,7 +110,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
||||
code: KeyCode::Down,
|
||||
kind: KeyEventKind::Press,
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
group: Group::Movement,
|
||||
group: Group::CtrlMovement,
|
||||
symbol: "Ctrl + ↓",
|
||||
description: "Scroll Down",
|
||||
},
|
||||
@@ -118,7 +119,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
||||
code: KeyCode::Left,
|
||||
kind: KeyEventKind::Press,
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
group: Group::Movement,
|
||||
group: Group::CtrlMovement,
|
||||
symbol: "Ctrl + ←",
|
||||
description: "Scroll Left",
|
||||
},
|
||||
@@ -127,7 +128,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
||||
code: KeyCode::Right,
|
||||
kind: KeyEventKind::Press,
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
group: Group::Movement,
|
||||
group: Group::CtrlMovement,
|
||||
symbol: "Ctrl + →",
|
||||
description: "Scroll Right",
|
||||
},
|
||||
@@ -156,7 +157,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
||||
modifiers: KeyModifiers::NONE,
|
||||
group: Group::Movement,
|
||||
symbol: "Esc",
|
||||
description: "Go back",
|
||||
description: "Go back to main menu",
|
||||
},
|
||||
KeyBinding {
|
||||
action: Action::Backspace,
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
use crate::app::{
|
||||
App, View,
|
||||
keybindings::{Action, event_to_action},
|
||||
keybindings::{Action, common_keybindings, event_to_action},
|
||||
};
|
||||
use ratatui::crossterm::event::KeyEvent;
|
||||
|
||||
pub fn main_menu_keybindings(app: &mut App, event: &KeyEvent) {
|
||||
if let Some(action) = event_to_action(&event) {
|
||||
common_keybindings(app, action);
|
||||
match action {
|
||||
Action::Quit => app.exit = true,
|
||||
Action::Quit2 => app.exit = true,
|
||||
Action::Up => {
|
||||
app.states.main_menu.selected_view =
|
||||
app.states.main_menu.selected_view.saturating_sub(1).max(1);
|
||||
|
||||
@@ -3,7 +3,7 @@ pub mod keybindings;
|
||||
pub mod main_menu;
|
||||
pub mod skirmish;
|
||||
|
||||
pub use default::default_keybindings;
|
||||
pub use default::{common_keybindings, default_keybindings};
|
||||
pub use keybindings::{Action, Group, KEYBINDINGS, KeyBinding, binding_for, event_to_action};
|
||||
pub use main_menu::main_menu_keybindings;
|
||||
pub use skirmish::skirmish_keybindings;
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
use crate::app::{
|
||||
App, View,
|
||||
keybindings::{Action, event_to_action},
|
||||
App,
|
||||
keybindings::{Action, common_keybindings, event_to_action},
|
||||
};
|
||||
use ratatui::crossterm::event::KeyEvent;
|
||||
|
||||
pub fn skirmish_keybindings(app: &mut App, key_event: &KeyEvent) {
|
||||
if let Some(action) = event_to_action(&key_event) {
|
||||
common_keybindings(app, action);
|
||||
match action {
|
||||
Action::ScrollUp => app.states.skirmish.vertical_offset.prev(),
|
||||
Action::ScrollDown => app.states.skirmish.vertical_offset.next(),
|
||||
Action::ScrollLeft => app.states.skirmish.horizontal_offset.prev(),
|
||||
Action::ScrollRight => app.states.skirmish.horizontal_offset.next(),
|
||||
Action::Quit => app.exit = true,
|
||||
Action::Quit2 => app.exit = true,
|
||||
Action::Esc => app.view = View::MainMenu,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user