Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnterAlternateScreen does not overwrite what is already on TTY/VT #930

Open
eatradish opened this issue Sep 23, 2024 · 1 comment
Open

Comments

@eatradish
Copy link

eatradish commented Sep 23, 2024

Describe the bug
EnterAlternateScreen does not overwrite what is already on TTY/VT

To Reproduce

  1. Run echo "hi" on Linux tty
  2. Run this program:
use std::{
    io::{self, stdout},
    time::Duration,
};

use crossterm::{
    cursor::position,
    event::{poll, read, Event, KeyCode},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
};

fn main() {
    enable_raw_mode().unwrap();
    execute!(stdout(), EnterAlternateScreen).unwrap();

    if let Err(e) = print_events() {
        println!("Error: {:?}\r", e);
    }

    disable_raw_mode().unwrap();
}

fn print_events() -> io::Result<()> {
    loop {
        // Wait up to 1s for another event
        if poll(Duration::from_millis(1_000))? {
            // It's guaranteed that read() won't block if `poll` returns `Ok(true)`
            let event = read()?;

            println!("Event::{:?}\r", event);

            if event == Event::Key(KeyCode::Char('c').into()) {
                println!("Cursor position: {:?}\r", position());
            }

            if event == Event::Key(KeyCode::Esc.into()) {
                break;
            }
        } else {
            // Timeout expired, no event for 1s
            println!(".\r");
        }
    }

    Ok(())
}
  1. On tty, it seems that EnterAlternateScreen doesn't take effect (you can see the hi output directly), whereas on the terminal emulator it does.

图片

Expected behavior
EnterAlternateScreen has the same behavior as other terminal emulators under tty

OS
Linux

Terminal/Console

  • tty
@eatradish eatradish changed the title EnterAlternateScreen does not overwrite what is already on tty EnterAlternateScreen does not overwrite what is already on TTY/VT Sep 23, 2024
@joshka
Copy link
Collaborator

joshka commented Nov 20, 2024

Try running tput smcup to switch to the alt screen and tput rmcup to restore. If this does nothing, then your tty doesn't support the alternate screen (or it's disabled by config), and there's likely nothing we can do here. If it does then your tty likely has some alternative escape code to make this change. You'll need to dig into the termcap / terminfo to work out what's going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants