rustcrossterm

Is it possible to print in the same line from right to left in Crossterm


I try to print a couple of words from right to left but my last word overwrite previous and erase line to the end. I got in my output just Second word, What I'm doing wrong ? Thank you.

fn render(&self, stdout: &mut Stdout) {
    stdout
        .execute(Clear(ClearType::All))
        .unwrap()
        .execute(MoveTo(30, 20))
        .unwrap()
        .execute(Print("First"))
        .unwrap()
        .execute(MoveTo(20, 20))
        .unwrap()
        .execute(Print("Second"))
        .unwrap();
}

Solution

  • Your program is likely exiting just after printing "Second" and your shell is overwriting everything after the current position of the cursor, which is just after the text "Second" because that's the last thing the function does. If you add a sleep call before exiting:

        ...
            .execute(Print("Second"))
            .unwrap();
        std::thread::sleep(std::time::Duration::from_secs(5));
    

    you'll see the following output:

    <lots of blank lines>
                        Second|    First
    

    where | is the position of the cursor. After the program exits, all text after the cursor is erased (tested on both zsh and bash).

    You can make the shell keep that line by moving to the next line before exiting:

    use std::io::stdout;
    use crossterm::{cursor::*, style::*, terminal::*, ExecutableCommand};
    
    fn main() {
        let mut stdout = stdout();
        stdout
            .execute(Clear(ClearType::All))
            .unwrap()
            .execute(MoveTo(30, 20))
            .unwrap()
            .execute(Print("First"))
            .unwrap()
            .execute(MoveTo(20, 20))
            .unwrap()
            .execute(Print("Second"))
            .unwrap()
            .execute(MoveTo(0, 21))
            .unwrap();
    }