summaryrefslogtreecommitdiffstats
path: root/src/app.rs
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2021-02-16 18:07:41 -0500
committerGitHub <noreply@github.com>2021-02-16 18:07:41 -0500
commitcf14abe37d202a885a4f7eb14348927359aef9fc (patch)
treed54b867ca610cb4598e51f8b388205569477f33c /src/app.rs
parente437b14922c688b1f5bbfcb4d13387564be34be0 (diff)
feature: Add ctrl-w and ctrl-h support in the search (#409)
Ctrl-w deletes one word backwards from the current cursor location. Ctrl-h is just an alias for backspace.
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs94
1 files changed, 89 insertions, 5 deletions
diff --git a/src/app.rs b/src/app.rs
index b10570b8..7b9d0758 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -725,17 +725,22 @@ impl App {
.current_search_query
.len()
{
+ let current_cursor = proc_widget_state.get_search_cursor_position();
proc_widget_state
+ .search_walk_forward(proc_widget_state.get_search_cursor_position());
+
+ let _removed_chars: String = proc_widget_state
.process_search_state
.search_state
.current_search_query
- .remove(proc_widget_state.get_search_cursor_position());
+ .drain(current_cursor..proc_widget_state.get_search_cursor_position())
+ .collect();
proc_widget_state
.process_search_state
.search_state
.grapheme_cursor = GraphemeCursor::new(
- proc_widget_state.get_search_cursor_position(),
+ current_cursor,
proc_widget_state
.process_search_state
.search_state
@@ -769,14 +774,16 @@ impl App {
.is_enabled
&& proc_widget_state.get_search_cursor_position() > 0
{
+ let current_cursor = proc_widget_state.get_search_cursor_position();
proc_widget_state
.search_walk_back(proc_widget_state.get_search_cursor_position());
- let removed_char = proc_widget_state
+ let removed_chars: String = proc_widget_state
.process_search_state
.search_state
.current_search_query
- .remove(proc_widget_state.get_search_cursor_position());
+ .drain(proc_widget_state.get_search_cursor_position()..current_cursor)
+ .collect();
proc_widget_state
.process_search_state
@@ -794,7 +801,8 @@ impl App {
proc_widget_state
.process_search_state
.search_state
- .char_cursor_position -= UnicodeWidthChar::width(removed_char).unwrap_or(0);
+ .char_cursor_position -= UnicodeWidthStr::width(removed_chars.as_str());
+
proc_widget_state
.process_search_state
.search_state
@@ -1167,6 +1175,82 @@ impl App {
}
}
+ pub fn clear_previous_word(&mut self) {
+ if let BottomWidgetType::ProcSearch = self.current_widget.widget_type {
+ if let Some(proc_widget_state) = self
+ .proc_state
+ .widget_states
+ .get_mut(&(self.current_widget.widget_id - 1))
+ {
+ // Traverse backwards from the current cursor location until you hit non-whitespace characters,
+ // then continue to traverse (and delete) backwards until you hit a whitespace character. Halt.
+
+ // So... first, let's get our current cursor position using graphemes...
+ let end_index = proc_widget_state.get_char_cursor_position();
+
+ // Then, let's crawl backwards until we hit our location, and store the "head"...
+ let query = proc_widget_state.get_current_search_query();
+ let mut start_index = 0;
+ let mut saw_non_whitespace = false;
+
+ for (itx, c) in query
+ .chars()
+ .rev()
+ .enumerate()
+ .skip(query.len() - end_index)
+ {
+ if c.is_whitespace() {
+ if saw_non_whitespace {
+ start_index = query.len() - itx;
+ break;
+ }
+ } else {
+ saw_non_whitespace = true;
+ }
+ }
+
+ let removed_chars: String = proc_widget_state
+ .process_search_state
+ .search_state
+ .current_search_query
+ .drain(start_index..end_index)
+ .collect();
+
+ proc_widget_state
+ .process_search_state
+ .search_state
+ .grapheme_cursor = GraphemeCursor::new(
+ start_index,
+ proc_widget_state
+ .process_search_state
+ .search_state
+ .current_search_query
+ .len(),
+ true,
+ );
+
+ proc_widget_state
+ .process_search_state
+ .search_state
+ .char_cursor_position -= UnicodeWidthStr::width(removed_chars.as_str());
+
+ proc_widget_state
+ .process_search_state
+ .search_state
+ .cursor_direction = CursorDirection::Left;
+
+ proc_widget_state.update_query();
+ self.proc_state.force_update = Some(self.current_widget.widget_id - 1);
+
+ // Now, convert this range into a String-friendly range and remove it all at once!
+
+ // Now make sure to also update our current cursor positions...
+
+ self.proc_state.force_update = Some(self.current_widget.widget_id - 1);
+ }
+ }
+ }
+
pub fn start_dd(&mut self) {
self.reset_multi_tap_keys();