From a55f0fe3e7382e79ec161962917f3c5d8609771d Mon Sep 17 00:00:00 2001 From: sebashwa Date: Mon, 16 May 2022 08:55:11 +0200 Subject: [PATCH] Change interface for moving cursor to whitespace --- src/components/utils/input.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/components/utils/input.rs b/src/components/utils/input.rs index 8767b8b..847dd5d 100644 --- a/src/components/utils/input.rs +++ b/src/components/utils/input.rs @@ -31,12 +31,13 @@ impl Input { self.cursor_position = 0; } - fn find_whitespace_backwards(&self) -> Option { - let mut result = None; - for i in (0..self.cursor_index).rev() { - if (i < self.cursor_index - 1) && self.value[i].is_whitespace() { - result = Some(i); + fn cursor_index_backwards_to_whitespace(&self) -> usize { + let mut result = 0; + + for i in (0..self.cursor_index - 1).rev() { + if self.value[i].is_whitespace() { + result = i + 1; break; } } @@ -109,11 +110,7 @@ impl Input { return (Some(key), false); } - let new_cursor_index = match self.find_whitespace_backwards() { - Some(i) => i + 1, - None => 0, - }; - + let new_cursor_index = self.cursor_index_backwards_to_whitespace(); let mut tail = self.value.to_vec().drain(self.cursor_index..).collect(); self.cursor_index = new_cursor_index;