From 0a4544a608d17b6293132bca8c21ddf84ff8d50b Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Wed, 11 Jan 2023 23:54:14 +0100 Subject: [PATCH] launcher: Add ctrl k/u handling --- src/keyboard/keysyms.rs | 5 ++-- src/widgets/launcher.rs | 54 ++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/keyboard/keysyms.rs b/src/keyboard/keysyms.rs index 276584a..bbde9a2 100644 --- a/src/keyboard/keysyms.rs +++ b/src/keyboard/keysyms.rs @@ -13,7 +13,8 @@ pub const XKB_KEY_Down: u32 = 0xff54; /* Move down, down arrow */ pub const XKB_KEY_End: u32 = 0xff57; /* EOL */ //pub const XKB_KEY_ISO_Left_Tab: u32 = 0xfe20; -pub const XKB_KEY_e: u32 = 0x0065; /* U+0065 LATIN SMALL LETTER E */ pub const XKB_KEY_a: u32 = 0x0061; /* U+0061 LATIN SMALL LETTER A */ +pub const XKB_KEY_e: u32 = 0x0065; /* U+0065 LATIN SMALL LETTER E */ //pub const XKB_KEY_c: u32 = 0x0063; /* U+0063 LATIN SMALL LETTER C */ -//pub const XKB_KEY_u: u32 = 0x0075; /* U+0075 LATIN SMALL LETTER U */ +pub const XKB_KEY_k: u32 = 0x006b; /* U+006B LATIN SMALL LETTER K */ +pub const XKB_KEY_u: u32 = 0x0075; /* U+0075 LATIN SMALL LETTER U */ diff --git a/src/widgets/launcher.rs b/src/widgets/launcher.rs index b4c0a8f..0da7e35 100644 --- a/src/widgets/launcher.rs +++ b/src/widgets/launcher.rs @@ -130,20 +130,46 @@ impl Prompt { fn backspace(&mut self) { let mut indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); - if !indices.is_empty() && self.cursor > 0 { - self.cursor -= 1; - indices.remove(self.cursor); - self.input = indices.iter().fold("".into(), |acc, el| acc + el.1); + if indices.is_empty() { + self.mode = PromptMode::Normal; + return; + } + if self.cursor == 0 { + return; + } + self.cursor -= 1; + indices.remove(self.cursor); + self.input = indices.iter().fold("".into(), |acc, el| acc + el.1); + } + + fn delete(&mut self) { + let mut indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); + if indices.is_empty() { + self.mode = PromptMode::Normal; + return; + } + if self.cursor >= indices.len() { + return; + } + + indices.remove(self.cursor); + self.input = indices.iter().fold("".into(), |acc, el| acc + el.1); + } + + fn clear_right(&mut self) { + let indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); + if !indices.is_empty() { + self.input = indices[0..self.cursor].iter().fold("".into(), |acc, el| acc + el.1); } else { self.mode = PromptMode::Normal; } } - fn delete(&mut self) { - let mut indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); - if !indices.is_empty() && self.cursor < indices.len() { - indices.remove(self.cursor); - self.input = indices.iter().fold("".into(), |acc, el| acc + el.1); + fn clear_left(&mut self) { + let indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); + if !indices.is_empty() { + self.input = indices[self.cursor..indices.len()].iter().fold("".into(), |acc, el| acc + el.1); + self.cursor = 0; } else { self.mode = PromptMode::Normal; } @@ -524,6 +550,16 @@ impl Widget for Interface { match event.keysym { keysyms::XKB_KEY_a if event.modifiers.ctrl => self.inner.prompt.home(), keysyms::XKB_KEY_e if event.modifiers.ctrl => self.inner.prompt.end(), + keysyms::XKB_KEY_u if event.modifiers.ctrl => { + self.inner.prompt.clear_left(); + self.inner.selection = 0; + widget.update(&self.inner); + }, + keysyms::XKB_KEY_k if event.modifiers.ctrl => { + self.inner.prompt.clear_right(); + self.inner.selection = 0; + widget.update(&self.inner); + }, keysyms::XKB_KEY_Home => self.inner.prompt.home(), keysyms::XKB_KEY_End => self.inner.prompt.end(), keysyms::XKB_KEY_BackSpace => {