generated from GarandPLG/rust-flake-template
Remove AppChannels, use separate receivers
Refactor App::run to accept individual Receiver arguments instead of an AppChannels struct. Delete the AppChannels definition and its re‑export, and update main.rs to create each channel manually and pass the receivers to App::run. Rename the CLI field `sound_track` to `soundtrack` and adjust all references. Clean up imports and formatting accordingly.
This commit is contained in:
+17
-10
@@ -1,13 +1,14 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::{
|
app::{GameStates, handle_keybindings, threads::AudioCmd, view::View},
|
||||||
GameStates, handle_keybindings,
|
|
||||||
threads::{AppChannels, AudioCmd},
|
|
||||||
view::View,
|
|
||||||
},
|
|
||||||
cli::Cli,
|
cli::Cli,
|
||||||
};
|
};
|
||||||
use ratatui::{DefaultTerminal, Frame, crossterm::event::KeyEvent, layout::Rect};
|
use ratatui::{DefaultTerminal, Frame, crossterm::event::KeyEvent, layout::Rect};
|
||||||
use std::{io::Result, sync::mpsc::Sender, thread::sleep, time::Duration};
|
use std::{
|
||||||
|
io::Result,
|
||||||
|
sync::mpsc::{Receiver, Sender},
|
||||||
|
thread::sleep,
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub exit: bool,
|
pub exit: bool,
|
||||||
@@ -38,15 +39,21 @@ impl App {
|
|||||||
self.states.as_mut()
|
self.states.as_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self, terminal: &mut DefaultTerminal, channels: AppChannels) -> Result<()> {
|
pub fn run(
|
||||||
|
&mut self,
|
||||||
|
terminal: &mut DefaultTerminal,
|
||||||
|
input_rx: Receiver<KeyEvent>,
|
||||||
|
resize_rx: Receiver<(u16, u16)>,
|
||||||
|
tick_rx: Receiver<()>,
|
||||||
|
) -> Result<()> {
|
||||||
while !self.exit {
|
while !self.exit {
|
||||||
terminal.draw(|frame: &mut Frame<'_>| self.draw(frame))?;
|
terminal.draw(|frame: &mut Frame<'_>| self.draw(frame))?;
|
||||||
|
|
||||||
if let Ok(key) = channels.input_rx.try_recv() {
|
if let Ok(key) = input_rx.try_recv() {
|
||||||
self.handle_key_event(key)?;
|
self.handle_key_event(key)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok((_, _)) = channels.resize_rx.try_recv() {
|
if let Ok((_, _)) = resize_rx.try_recv() {
|
||||||
let window_area: Rect = self.window_area;
|
let window_area: Rect = self.window_area;
|
||||||
if let Some(state) = self.states_mut() {
|
if let Some(state) = self.states_mut() {
|
||||||
state
|
state
|
||||||
@@ -56,7 +63,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(()) = channels.tick_rx.try_recv() {
|
if let Ok(()) = tick_rx.try_recv() {
|
||||||
self.update()?;
|
self.update()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
use crate::app::threads::AudioCmd;
|
|
||||||
use ratatui::crossterm::event::KeyEvent;
|
|
||||||
use std::sync::mpsc::{Receiver, Sender, channel};
|
|
||||||
|
|
||||||
pub struct AppChannels {
|
|
||||||
pub input_tx: Sender<KeyEvent>,
|
|
||||||
pub input_rx: Receiver<KeyEvent>,
|
|
||||||
|
|
||||||
pub resize_tx: Sender<(u16, u16)>,
|
|
||||||
pub resize_rx: Receiver<(u16, u16)>,
|
|
||||||
|
|
||||||
pub tick_tx: Sender<()>,
|
|
||||||
pub tick_rx: Receiver<()>,
|
|
||||||
|
|
||||||
pub audio_tx: Sender<AudioCmd>,
|
|
||||||
pub audio_rx: Receiver<AudioCmd>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AppChannels {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let (input_tx, input_rx) = channel::<KeyEvent>();
|
|
||||||
let (resize_tx, resize_rx) = channel::<(u16, u16)>();
|
|
||||||
let (tick_tx, tick_rx) = channel::<()>();
|
|
||||||
let (audio_tx, audio_rx) = channel::<AudioCmd>();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
input_tx,
|
|
||||||
input_rx,
|
|
||||||
resize_tx,
|
|
||||||
resize_rx,
|
|
||||||
tick_tx,
|
|
||||||
tick_rx,
|
|
||||||
audio_tx,
|
|
||||||
audio_rx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
mod app_channels;
|
|
||||||
mod audio;
|
mod audio;
|
||||||
mod events;
|
mod events;
|
||||||
mod tick;
|
mod tick;
|
||||||
|
|
||||||
pub use app_channels::AppChannels;
|
|
||||||
pub use audio::{AudioCmd, SoundrackParts, Soundtrack, handle_audio};
|
pub use audio::{AudioCmd, SoundrackParts, Soundtrack, handle_audio};
|
||||||
pub use events::handle_ct_events;
|
pub use events::handle_ct_events;
|
||||||
pub use tick::handle_tick_event;
|
pub use tick::handle_tick_event;
|
||||||
|
|||||||
+1
-1
@@ -148,7 +148,7 @@ pub struct Cli {
|
|||||||
default_value_t = Soundtrack::Default,
|
default_value_t = Soundtrack::Default,
|
||||||
value_enum
|
value_enum
|
||||||
)]
|
)]
|
||||||
pub sound_track: Soundtrack,
|
pub soundtrack: Soundtrack,
|
||||||
|
|
||||||
/// Mute soundtrack (default: disabled).
|
/// Mute soundtrack (default: disabled).
|
||||||
#[arg(
|
#[arg(
|
||||||
|
|||||||
+9
-13
@@ -1,14 +1,13 @@
|
|||||||
use ratatui::{Terminal, crossterm::event::KeyEvent, prelude::CrosstermBackend};
|
use ratatui::{Terminal, crossterm::event::KeyEvent, prelude::CrosstermBackend};
|
||||||
use std::{
|
use std::{
|
||||||
io::{Result, Stdout},
|
io::{Result, Stdout},
|
||||||
mem::replace,
|
sync::mpsc::channel,
|
||||||
sync::mpsc::{Receiver, Sender, channel},
|
|
||||||
thread,
|
thread,
|
||||||
};
|
};
|
||||||
use war_in_tunnels::{
|
use war_in_tunnels::{
|
||||||
app::{
|
app::{
|
||||||
App,
|
App,
|
||||||
threads::{AppChannels, AudioCmd, handle_audio, handle_ct_events, handle_tick_event},
|
threads::{AudioCmd, handle_audio, handle_ct_events, handle_tick_event},
|
||||||
},
|
},
|
||||||
cli::{Cli, get_args},
|
cli::{Cli, get_args},
|
||||||
};
|
};
|
||||||
@@ -23,24 +22,21 @@ const TICK_MS: u8 = 33;
|
|||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let args: Cli = get_args();
|
let args: Cli = get_args();
|
||||||
|
|
||||||
let mut channels: AppChannels = AppChannels::new();
|
let (input_tx, input_rx) = channel::<KeyEvent>();
|
||||||
|
let (resize_tx, resize_rx) = channel::<(u16, u16)>();
|
||||||
|
let (tick_tx, tick_rx) = channel::<()>();
|
||||||
|
let (audio_tx, audio_rx) = channel::<AudioCmd>();
|
||||||
|
|
||||||
let input_tx: Sender<KeyEvent> = channels.input_tx.clone();
|
|
||||||
let resize_tx: Sender<(u16, u16)> = channels.resize_tx.clone();
|
|
||||||
thread::spawn(move || handle_ct_events(input_tx, resize_tx));
|
thread::spawn(move || handle_ct_events(input_tx, resize_tx));
|
||||||
|
|
||||||
let tick_tx: Sender<()> = channels.tick_tx.clone();
|
|
||||||
thread::spawn(move || handle_tick_event(tick_tx, TICK_MS));
|
thread::spawn(move || handle_tick_event(tick_tx, TICK_MS));
|
||||||
|
|
||||||
let audio_rx: Receiver<AudioCmd> = replace(&mut channels.audio_rx, channel().1);
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
handle_audio(audio_rx, args.mute, args.sound_track);
|
handle_audio(audio_rx, args.mute, args.soundtrack);
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut terminal: Terminal<CrosstermBackend<Stdout>> = ratatui::init();
|
let mut terminal: Terminal<CrosstermBackend<Stdout>> = ratatui::init();
|
||||||
let mut app: App = App::new(args, channels.audio_tx.clone());
|
let mut app: App = App::new(args, audio_tx);
|
||||||
|
|
||||||
let app_result: Result<()> = app.run(&mut terminal, channels);
|
let app_result: Result<()> = app.run(&mut terminal, input_rx, resize_rx, tick_rx);
|
||||||
|
|
||||||
ratatui::restore();
|
ratatui::restore();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user