From 9a516e0663bb3844ff449ede3fc0b00eea05450f Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 25 Nov 2019 14:47:58 +0200 Subject: [PATCH] ui/text_editing: add Ctrl-{f,b,u} readline shortcuts --- ui/src/components/utilities/widgets.rs | 39 +++++++++++++++++++++++++- ui/src/terminal/text_editing.rs | 19 +++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/ui/src/components/utilities/widgets.rs b/ui/src/components/utilities/widgets.rs index 29538b2b..48a6883d 100644 --- a/ui/src/components/utilities/widgets.rs +++ b/ui/src/components/utilities/widgets.rs @@ -128,7 +128,7 @@ impl Component for Field { if let UIEvent::InsertInput(Key::Char('\t')) = event { if let Some(suggestion) = auto_complete.get_suggestion() { *s = UText::new(suggestion); - let len = s.as_str().len().saturating_sub(1); + let len = s.as_str().len(); s.set_cursor(len); return true; } @@ -199,6 +199,43 @@ impl Component for Field { s.set_cursor(0); } } + UIEvent::InsertInput(Key::Ctrl('b')) => { + /* Backward one character */ + if let Text(ref mut s, _) = self { + s.cursor_dec(); + } + } + UIEvent::InsertInput(Key::Ctrl('f')) => { + /* Forward one character */ + if let Text(ref mut s, _) = self { + s.cursor_inc(); + } + } + UIEvent::InsertInput(Key::Ctrl('w')) => { + // FIXME: Use Unicode word-breaking algorithm. + /* Cut previous word */ + if let Text(ref mut s, _) = self { + while s.as_str()[..s.cursor_pos()] + .last_grapheme() + .map(|(_, graph)| !graph.is_empty() && graph.trim().is_empty()) + .unwrap_or(false) + { + s.backspace(); + } + while s.as_str()[..s.cursor_pos()] + .last_grapheme() + .map(|(_, graph)| !graph.is_empty() && !graph.trim().is_empty()) + .unwrap_or(false) + { + s.backspace(); + } + } + } + UIEvent::InsertInput(Key::Ctrl('u')) => { + if let Text(ref mut s, _) = self { + s.cut_left() + } + } UIEvent::InsertInput(Key::Ctrl('e')) => { if let Text(ref mut s, _) = self { s.set_cursor(s.as_str().len()); diff --git a/ui/src/terminal/text_editing.rs b/ui/src/terminal/text_editing.rs index f46c11e1..7821928f 100644 --- a/ui/src/terminal/text_editing.rs +++ b/ui/src/terminal/text_editing.rs @@ -89,6 +89,10 @@ impl UText { * self.cursor_pos */ pub fn insert_char(&mut self, k: char) { + if k.is_control() { + return; + } + self.content.insert(self.cursor_pos, k); self.cursor_pos += k.len_utf8(); self.grapheme_cursor_pos += 1; @@ -132,4 +136,19 @@ impl UText { self.content.drain(offset..offset + graph_len).count(); } + + pub fn cut_left(&mut self) { + if self.content.is_empty() { + return; + } + let offset = { + let (left, _) = self.content.split_at(self.cursor_pos); + left.last_grapheme() + .map(|(offset, graph)| offset + graph.len()) + .unwrap_or(0) + }; + self.cursor_pos = 0; + self.grapheme_cursor_pos = 0; + self.content.drain(..offset).count(); + } }