我正在尝试使用 termion 库实现一个简单的文本编辑器。当用户按下Backspace键时,我想删除一个字符,但是我无法弄清楚如何实现。
这个库似乎只提供了这些选项:
AfterCursor Clear everything after the cursor.
All Clear the entire screen.
BeforeCursor Clear everything before the cursor.
CurrentLine Clear the current line.
UntilNewline Clear from cursor to newline.
这些选项都不太合适。UntilNewLine(直到换行)
算是比较适用的一个,除非用户将光标移动到左侧,在这种情况下它会删除超过一个字符(基本上是该行剩余的所有字符)。
我的当前代码:
fn main() {
let stdin = io::stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
write!(stdout, "{}{}", termion::clear::All, termion::cursor::Goto(1, 1));
stdout.flush().unwrap();
for c in stdin.keys() {
match c.unwrap() {
Key::Esc => break,
Key::Left => {
let (x, y) = stdout.cursor_pos().unwrap();
write!(stdout, "{}", termion::cursor::Goto(x-1, y));
},
Key::Right => {
let (x, y) = stdout.cursor_pos().unwrap();
write!(stdout, "{}", termion::cursor::Goto(x+1, y));
},
Key::Up => {
let (x, y) = stdout.cursor_pos().unwrap();
write!(stdout, "{}", termion::cursor::Goto(x, y-1));
},
Key::Down => {
let (x, y) = stdout.cursor_pos().unwrap();
write!(stdout, "{}", termion::cursor::Goto(x, y+1));
},
Key::Backspace => {
let (x, y) = stdout.cursor_pos().unwrap();
write!(stdout, "{}{}", termion::cursor::Goto(x-1, y), termion::clear::UntilNewline );
},
Key::Char('\n') => { write!(stdout, "\r\n"); },
Key::Char(x) => { write!(stdout, "{}", x); },
_ => ()
}
stdout.flush().unwrap();
}
}
我想我有一个双重问题:
- 如何解决这个特定的问题(清除单个字符)?
- 如果解决方案在文档中,我该如何找到它?我尝试搜索删除 / 回退 / 清除 - 但那没用。我在这里是否遇到了库的限制,还是有其他方法可以做到?