是否可以在Crossterm中从右到左在同一行中打印

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

我尝试从右到左打印几个单词,但我的最后一个单词覆盖了之前的单词并删除了最后一行。我在输出中得到的只是第二个词,我做错了什么?谢谢。

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

您的程序可能会在打印“Second”后立即退出,并且您的 shell 正在覆盖光标当前位置之后的所有内容,该位置就在文本“Second”之后,因为这是函数的最后一件事做。如果在退出前添加睡眠调用:

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

您将看到以下输出:

<lots of blank lines>
                    Second|    First

其中 | 是光标的位置。程序退出后,清除光标后的所有文本(在zshbash上测试)。

您可以 shell 在退出前移动到下一行以保留该行:

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