add cursor movement, deletion
This commit is contained in:
1 parent
07910f1d59
commit
4fe790dd0c
2 files changed
+87
-1
No files matched your search
@@ -2,10 +2,12 @@ use crate::input::{InputEvent, KeyCode, KeyEvent, Modifiers};
|
||||
use crate::surface::{Change, Position};
|
||||
use crate::terminal::Terminal;
|
||||
use failure::Fallible;
|
||||
use unicode_segmentation::GraphemeCursor;
|
||||
|
||||
pub struct LineEditor<T: Terminal> {
|
||||
terminal: T,
|
||||
line: String,
|
||||
cursor: usize,
|
||||
}
|
||||
|
||||
impl<T: Terminal> LineEditor<T> {
|
||||
@@ -13,6 +15,7 @@ impl<T: Terminal> LineEditor<T> {
|
||||
Self {
|
||||
terminal,
|
||||
line: String::new(),
|
||||
cursor: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +27,10 @@ impl<T: Terminal> LineEditor<T> {
|
||||
},
|
||||
Change::ClearToEndOfScreen(Default::default()),
|
||||
Change::Text(self.line.clone()),
|
||||
Change::CursorPosition {
|
||||
x: Position::Absolute(self.cursor),
|
||||
y: Position::NoChange,
|
||||
},
|
||||
])?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -38,20 +45,90 @@ impl<T: Terminal> LineEditor<T> {
|
||||
|
||||
fn read_line_impl(&mut self) -> Fallible<String> {
|
||||
self.line.clear();
|
||||
self.cursor = 0;
|
||||
|
||||
self.render()?;
|
||||
while let Some(event) = self.terminal.poll_input(None)? {
|
||||
match event {
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char('J'),
|
||||
modifiers: Modifiers::CTRL,
|
||||
})
|
||||
| InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char('M'),
|
||||
modifiers: Modifiers::CTRL,
|
||||
})
|
||||
| InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Enter,
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
break;
|
||||
}
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Backspace,
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
|
||||
if let Ok(Some(pos)) = cursor.prev_boundary(&self.line, 0) {
|
||||
self.line.remove(pos);
|
||||
self.cursor = pos;
|
||||
}
|
||||
}
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::LeftArrow,
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
|
||||
if let Ok(Some(pos)) = cursor.prev_boundary(&self.line, 0) {
|
||||
self.cursor = pos;
|
||||
}
|
||||
}
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char('A'),
|
||||
modifiers: Modifiers::CTRL,
|
||||
})
|
||||
| InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Home,
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
self.cursor = 0;
|
||||
}
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char('E'),
|
||||
modifiers: Modifiers::CTRL,
|
||||
})
|
||||
| InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::End,
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
let mut cursor =
|
||||
GraphemeCursor::new(self.line.len() - 1, self.line.len(), false);
|
||||
if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
|
||||
self.cursor = pos;
|
||||
}
|
||||
}
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::RightArrow,
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
|
||||
if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
|
||||
self.cursor = pos;
|
||||
}
|
||||
}
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char(c),
|
||||
modifiers: Modifiers::NONE,
|
||||
}) => {
|
||||
self.line.push(c);
|
||||
self.line.insert(self.cursor, c);
|
||||
let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
|
||||
if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
|
||||
self.cursor = pos;
|
||||
}
|
||||
}
|
||||
InputEvent::Paste(text) => {
|
||||
self.line.insert_str(self.cursor, &text);
|
||||
self.cursor += text.len();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -502,6 +502,15 @@ impl TerminfoRenderer {
|
||||
} if *n > 0 => {
|
||||
self.cursor_right(*n as u32, out)?;
|
||||
}
|
||||
Change::CursorPosition {
|
||||
x: Position::Absolute(n),
|
||||
y: Position::NoChange,
|
||||
} => {
|
||||
out.by_ref().write_all(b"\r")?;
|
||||
if *n > 0 {
|
||||
self.cursor_right(*n as u32, out)?;
|
||||
}
|
||||
}
|
||||
Change::CursorPosition {
|
||||
x: Position::Absolute(x),
|
||||
y: Position::Absolute(y),
|
||||
|
||||
Reference in new issue
Block a user