rusttuicrossterm

Crossterm not clearing screen properly


I have a main method that looks like this:

use std::io::stdout;
use crossterm as ct;
use crossterm::terminal as ct_terminal;

pub mod tui;

fn main() -> () {
    let user_result: usize = tui::select_choice("", &[]);
}

The function that's called (tui::select_choice()) looks like this:

use std::{thread::sleep, time::Duration};
use crossterm::terminal::{Clear, ClearType::Purge};
use crossterm::ExecutableCommand;
use std::io::stdout;

pub fn select_choice(pre_text: &str, options: &[&str]) -> usize {
    println!("Na");
    sleep(Duration::from_secs(5));
    stdout().execute(Clear(Purge));
    println!("ima do my own thing");
    sleep(Duration::from_secs(5));
    2 as usize
}

The program runs but the terminal won't clear. It just prints on the next line:

Na
ima do my own thing

H:\Code\Rust\GTFO>

Solution

  • Contrary to the documentation (ClearType::Purge - "All plus history"), Purge clears only history, not the screen. It also sends ANSI escape ESC[3J, which means "erase history", not "clear screen" (although confusingly, on Windows <10 it does seem to clear to screen - doesn't have a machine to test that on, though).

    If you want to clear history + screen, you need to send both Purge and All.