Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add ctrl-w and ctrl-h support in the search #409

Merged
merged 3 commits into from
Feb 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feature: Add Ctrl-h and Ctrl-w support to search
  • Loading branch information
ClementTsang committed Feb 16, 2021
commit 4a43347b8ed52bd81e61ebf65efe8916d01b5496
55 changes: 53 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,61 @@ impl App {
// 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_range = proc_widget_state.get_search_cursor_position();
let end_index = proc_widget_state.get_char_cursor_position();

// Then, let's crawl backwards until we hit our location, and store the "head"...
proc_widget_state.search_walk_back(proc_widget_state.get_search_cursor_position());
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!

Expand Down