Skip to content

Commit

Permalink
Ensure kitty setup runs after terminal setup (#6)
Browse files Browse the repository at this point in the history
* make sure kitty setup runs after terminal setup

* ensure correct order of cleanup during normal exit

a panic will still skip the cleanup system, borking the terminal
  • Loading branch information
cxreiff authored Jun 7, 2024
1 parent 89e618b commit bb99198
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/kitty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use crossterm::{
ExecutableCommand,
};

use crate::terminal;

pub struct KittyPlugin;

impl Plugin for KittyPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Startup, setup.after(terminal::setup));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! fn main() {
//! let wait_duration = std::time::Duration::from_secs_f64(1. / 60.); // 60 FPS
//! App::new()
//! .add_plugins(RatatuiPlugins)
//! .add_plugins(RatatuiPlugins::default())
//! .add_plugins(ScheduleRunnerPlugin::run_loop(wait_duration))
//! .add_systems(PreUpdate, keyboard_input_system)
//! .add_systems(Update, hello_world.pipe(exit_on_error))
Expand Down
4 changes: 2 additions & 2 deletions src/ratatui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{error, event, kitty, mouse, terminal};
/// use bevy::prelude::*;
/// use bevy_ratatui::RatatuiPlugins;
///
/// App::new().add_plugins(RatatuiPlugins);
/// App::new().add_plugins(RatatuiPlugins::default());
/// ```
pub struct RatatuiPlugins {
pub enable_kitty_protocol: bool,
Expand All @@ -34,7 +34,7 @@ impl Default for RatatuiPlugins {
/// use bevy::prelude::*;
/// use bevy_ratatui::RatatuiPlugins;
///
/// App::new().add_plugins(RatatuiPlugins);
/// App::new().add_plugins(RatatuiPlugins::default());
/// ```
impl PluginGroup for RatatuiPlugins {
fn build(self) -> PluginGroupBuilder {
Expand Down
16 changes: 13 additions & 3 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! and leaves the alternate screen.
use std::io::{self, stdout, Stdout};

use bevy::prelude::*;
use bevy::{app::AppExit, prelude::*};
use color_eyre::Result;
use crossterm::{
cursor,
Expand All @@ -16,7 +16,7 @@ use crossterm::{
};
use ratatui::backend::CrosstermBackend;

use crate::error::exit_on_error;
use crate::{error::exit_on_error, kitty::KittyEnabled, mouse::MouseCaptureEnabled};

/// A plugin that sets up the terminal.
///
Expand All @@ -26,7 +26,8 @@ pub struct TerminalPlugin;

impl Plugin for TerminalPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup.pipe(exit_on_error));
app.add_systems(Startup, setup.pipe(exit_on_error))
.add_systems(PostUpdate, cleanup_system);
}
}

Expand All @@ -37,6 +38,15 @@ pub fn setup(mut commands: Commands) -> Result<()> {
Ok(())
}

/// A cleanup system that ensures terminal enhancements are cleaned up in the correct order.
pub fn cleanup_system(mut commands: Commands, mut exit_reader: EventReader<AppExit>) {
for _ in exit_reader.read() {
commands.remove_resource::<KittyEnabled>();
commands.remove_resource::<MouseCaptureEnabled>();
commands.remove_resource::<RatatuiContext>();
}
}

/// A wrapper around ratatui::Terminal that automatically enters and leaves the alternate screen.
///
/// This resource is used to draw to the terminal. It automatically enters the alternate screen when
Expand Down

0 comments on commit bb99198

Please sign in to comment.