Browse Source

ui/text_editing: add Ctrl-{f,b,u} readline shortcuts

tags/alpha-0.4.1
Manos Pitsidianakis 3 months ago
parent
commit
9a516e0663
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS. GPG Key ID: 73627C2F690DF710
2 changed files with 57 additions and 1 deletions
  1. +38
    -1
      ui/src/components/utilities/widgets.rs
  2. +19
    -0
      ui/src/terminal/text_editing.rs

+ 38
- 1
ui/src/components/utilities/widgets.rs View File

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


+ 19
- 0
ui/src/terminal/text_editing.rs View File

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

Loading…
Cancel
Save