generated from GarandPLG/rust-flake-template
Drop crossterm delete App add CLI defaults
Remove the crossterm dependency and its input handling, delete the now‑unused App module, and provide default values for all command‑line options.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
use ratatui::{
|
||||
DefaultTerminal, Frame,
|
||||
crossterm::event::{self, KeyCode, KeyEvent, KeyEventKind},
|
||||
};
|
||||
use std::{
|
||||
io::Result,
|
||||
sync::mpsc::{self, Receiver},
|
||||
};
|
||||
|
||||
pub struct App {
|
||||
pub exit: bool,
|
||||
pub default_window: String,
|
||||
pub username: String,
|
||||
pub game_mode: String,
|
||||
pub map_width: u8,
|
||||
pub map_height: u8,
|
||||
pub perk_deck: String,
|
||||
pub starting_wood: u8,
|
||||
pub starting_iron: u8,
|
||||
pub supply_limit: u8,
|
||||
pub xp_modifier: f32,
|
||||
pub skill_points_limit: u16,
|
||||
}
|
||||
|
||||
pub enum Event {
|
||||
Input(KeyEvent),
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn run(&mut self, terminal: &mut DefaultTerminal, rx: Receiver<Event>) -> Result<()> {
|
||||
while !self.exit {
|
||||
terminal.draw(|frame: &mut Frame<'_>| self.draw(frame))?;
|
||||
|
||||
let event: Event = match rx.recv() {
|
||||
Ok(ev) => ev,
|
||||
Err(_) => break,
|
||||
};
|
||||
match event {
|
||||
Event::Input(key_event) => self.handle_key_event(key_event)?,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&self, frame: &mut Frame<'_>) {
|
||||
frame.render_widget(self, frame.area());
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, key_event: KeyEvent) -> Result<()> {
|
||||
if key_event.kind == KeyEventKind::Press && key_event.code == KeyCode::Char('q') {
|
||||
self.exit = true;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_input_events(tx: mpsc::Sender<Event>) {
|
||||
loop {
|
||||
match event::read() {
|
||||
Ok(ev) => {
|
||||
if let event::Event::Key(key_event) = ev {
|
||||
if tx.send(Event::Input(key_event)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod app;
|
||||
pub mod widget;
|
||||
pub mod widgets;
|
||||
|
||||
pub use app::{App, Event, handle_input_events};
|
||||
@@ -0,0 +1,14 @@
|
||||
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
|
||||
|
||||
use crate::app::{App, widgets::main_menu_widget};
|
||||
|
||||
impl Widget for &App {
|
||||
fn render(self, area: Rect, buf: &mut Buffer)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if self.default_window == "main_menu" {
|
||||
main_menu_widget(area, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Alignment, Constraint, Layout, Rect},
|
||||
style::{Color, Stylize},
|
||||
text::Line,
|
||||
widgets::{Block, Borders, Paragraph, 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: 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);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod main_menu;
|
||||
|
||||
pub use main_menu::main_menu_widget;
|
||||
Reference in New Issue
Block a user