COMP6991UNSW / termgame

A library to make simple games on the terminal with Rust
8 stars 3 forks source link

termgame panicing during on_tick leaves mouse inputs enabled in the terminal #3

Open Nilaos opened 1 year ago

Nilaos commented 1 year ago

I noticed during some testing that if a panic (via panic!, todo!, etc) occurred, inside a running Controller, then mouse inputs are being read by the terminal after the controller exits. While a panic is obviously not expected behaviour, having a side effect that renders a terminal unusable on most/all modern computers is undesirable.

MVE:

use termgame::{SimpleEvent, Controller, Game, GameEvent, GameSettings, StyledCharacter, run_game, KeyCode};
use std::error::Error;
use std::time::Duration;

struct MyGame {}

impl Controller for MyGame {
    fn on_start(&mut self, game: &mut Game) {
    }

    fn on_event(&mut self, game: &mut Game, event: GameEvent) {
        match event.into() {
            SimpleEvent::Just(KeyCode::Char(ch)) => {
                game.set_screen_char(1, 1, Some(StyledCharacter::new(ch)))
            },
            _ => {}
        }

    }

    fn on_tick(&mut self, _game: &mut Game) { panic!("This shouldn't break terminals!") }
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut controller = MyGame {};

    run_game(
        &mut controller,
        GameSettings::new()
            // The below are the defaults, but shown so you can edit them.
            .tick_duration(Duration::from_millis(50))
            .quit_event(Some(SimpleEvent::WithControl(KeyCode::Char('c')).into()))
    )?;

    println!("Game Ended!");

    Ok(())
}
tfpk commented 1 year ago

Hiya! If we were to fix this I think we'd just need something like a panic_unwind to fix the terminal.

I'll look into it, but I'm not going to fix it before we're done using termgame for the term.

Thanks for the bug report!

insou22 commented 1 year ago

@tfpk see: https://github.com/insou22/flymark/blob/main/src/term/mod.rs for fix