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
Show file tree
Hide file tree
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
Next Next commit
Slight refactoring for characters greater than 1 byte
  • Loading branch information
ClementTsang committed Feb 16, 2021
commit cc62d9f78cde856d2466b40b897da3182d1f514f
43 changes: 38 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1167,6 +1175,31 @@ 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_range = proc_widget_state.get_search_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());

// 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();

Expand Down
15 changes: 3 additions & 12 deletions src/canvas/widgets/process_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl ProcessTableWidget for Painter {
})
.collect::<Vec<_>>();

if cursor_position >= query.len() {
if cursor_position == query.len() {
res.push(Span::styled(" ", currently_selected_text_style))
}

Expand All @@ -558,17 +558,7 @@ impl ProcessTableWidget for Painter {
// This is easier - we just need to get a range of graphemes, rather than
// dealing with possibly inserting a cursor (as none is shown!)

grapheme_indices
.filter_map(|grapheme| {
current_grapheme_posn += UnicodeWidthStr::width(grapheme.1);
if current_grapheme_posn <= start_position {
None
} else {
let styled = Span::styled(grapheme.1, text_style);
Some(styled)
}
})
.collect::<Vec<_>>()
vec![Span::styled(query.to_string(), text_style)]
}
}

Expand Down Expand Up @@ -622,6 +612,7 @@ impl ProcessTableWidget for Painter {
},
)];
search_vec.extend(query_with_cursor);

search_vec
})];

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub fn handle_key_event_or_break(
KeyCode::Char('a') => app.skip_cursor_beginning(),
KeyCode::Char('e') => app.skip_cursor_end(),
KeyCode::Char('u') => app.clear_search(),
KeyCode::Char('w') => app.clear_previous_word(),
KeyCode::Char('h') => app.on_backspace(),
// KeyCode::Char('j') => {}, // Move down
// KeyCode::Char('k') => {}, // Move up
// KeyCode::Char('h') => {}, // Move right
Expand Down