crossterm-rs / crossterm

Cross platform terminal library rust
MIT License
3.28k stars 281 forks source link

using raw terminal globally having queue frozen #813

Open bgkillas opened 1 year ago

bgkillas commented 1 year ago

Describe the bug if i have raw terminal enabled until program is shutdown instead of everytime i read a single char and i paste 1025 characters my program seems to freeze until i type something else than it unfreezes for a bit and can refreeze sometimes

To Reproduce Steps to reproduce the behavior: example program

use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::terminal;
use std::io::{stdout, Write};
fn main() {
    terminal::enable_raw_mode().unwrap();
    loop {
        let c = read_single_char();
        print!("{}", c);
        stdout().flush().unwrap();
    }
}
pub fn read_single_char() -> char {
    let result = match match read() {
        Ok(c) => c,
        Err(_) => return '\0',
    } {
        Event::Key(KeyEvent {
            code, modifiers, ..
        }) => match (code, modifiers) {
            (KeyCode::Char(c), KeyModifiers::NONE | KeyModifiers::SHIFT) => c,
            (KeyCode::Char('c'), KeyModifiers::CONTROL) => '\x14',
            _ => '\0',
        },
        _ => '\0',
    };
    if result == '\x14' {
        terminal::disable_raw_mode().unwrap();
        std::process::exit(130);
    }
    result
}

but if you have it enable and disable raw mode in the read single char function it wont freeze

example input fdsfdsfsdfsdfsdfsdfjsgfiasjdfidsjfgaaosidgjdaoigjasdofisjadfoisdfdfssdatgagatatacggagggagatatgtcgtcatttgattacctgaaaactgccatcaagcaacagggctgcacgctacagcaggtagctgatgccagcggtatgaccaaagggtatttaagccagttactgaatgccaaaatcaaaagccccagcgcgcaaaagctggaggcgttgcaccgttttttggggcttgagtttccccggcagaagaaaacgatcggtgtcgtattcggtaagttctacccactgcataccggacatatctaccttatccagcgcgcctgtagccaggttgacgagctgcatatcattatgggttttgacgatacccgtgaccgcgcgttgttcgaagacagtgccatgtcgcagcagccgaccgtgccggatcgtctgcgttggttattgcaaacttttaaatatcagaaaaatattcgcattcatgctttcaacgaagagggcatggagccgtatccgcacggctgggatgtgtggagcaacggcatcaaaaagtttatggctgaaaaagggatccagccggatctgatctacacctcggaagaagccgatgcgccacagtatatggaacatctggggatcgagacggtgctggtcgatccgaaacgtacctttatgagtatcagcggtgcgcagatccgcgaaaacccgttccgctactgggaatatattcctaccgaagtgaagccgttttttgtgcgtaccgtggcgatccttggcggcgfdsafgdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfdsfdsf

Expected behavior not to freeze

OS arch Linux Terminal/Console kitty/foot