Skip to content

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

@Nilaos

Description

@Nilaos

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(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions