From 1d0b79ec4811dc320e37a7fce3e333a2bb666e34 Mon Sep 17 00:00:00 2001 From: sebashwa Date: Sun, 15 May 2022 22:29:55 +0200 Subject: [PATCH] Use early returns in input util key matching --- src/components/utils/input.rs | 63 +++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/src/components/utils/input.rs b/src/components/utils/input.rs index 31c0610..efe4a5c 100644 --- a/src/components/utils/input.rs +++ b/src/components/utils/input.rs @@ -39,48 +39,53 @@ impl Input { return (Some(key), true); } Key::Delete | Key::Backspace => { - if value_str.width() > 0 && !self.value.is_empty() && self.cursor_index > 0 { - let last_c = self.value.remove(self.cursor_index - 1); - self.cursor_index -= 1; - self.cursor_position -= compute_character_width(last_c); - return (Some(key), true); + if value_str.width() == 0 || self.value.is_empty() || self.cursor_index == 0 { + return (Some(key), false); } - return (Some(key), false); + + let last_c = self.value.remove(self.cursor_index - 1); + self.cursor_index -= 1; + self.cursor_position -= compute_character_width(last_c); + return (Some(key), true); } Key::Left => { - if !self.value.is_empty() && self.cursor_index > 0 { - self.cursor_index -= 1; - self.cursor_position = self - .cursor_position - .saturating_sub(compute_character_width(self.value[self.cursor_index])); - return (Some(key), true); + if self.value.is_empty() || self.cursor_index == 0 { + return (Some(key), false); } - return (Some(key), false); + + self.cursor_index -= 1; + self.cursor_position = self + .cursor_position + .saturating_sub(compute_character_width(self.value[self.cursor_index])); + return (Some(key), true); } Key::Right => { - if self.cursor_index < self.value.len() { - let next_c = self.value[self.cursor_index]; - self.cursor_index += 1; - self.cursor_position += compute_character_width(next_c); - return (Some(key), true); + if self.cursor_index == self.value.len() { + return (Some(key), false); } - return (Some(key), false); + + let next_c = self.value[self.cursor_index]; + self.cursor_index += 1; + self.cursor_position += compute_character_width(next_c); + return (Some(key), true); } Key::Ctrl('a') => { - if !self.value.is_empty() && self.cursor_index > 0 { - self.cursor_index = 0; - self.cursor_position = 0; - return (Some(key), true); + if self.value.is_empty() || self.cursor_index == 0 { + return (Some(key), false); } - return (Some(key), false); + + self.cursor_index = 0; + self.cursor_position = 0; + return (Some(key), true); } Key::Ctrl('e') => { - if self.cursor_index < self.value.len() { - self.cursor_index = self.value.len(); - self.cursor_position = self.value_str().width() as u16; - return (Some(key), true); + if self.cursor_index == self.value.len() { + return (Some(key), false); } - return (Some(key), false); + + self.cursor_index = self.value.len(); + self.cursor_position = self.value_str().width() as u16; + return (Some(key), true); } _ => (None, false), }