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/search field arrows support #5549

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions mux/src/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ impl Default for Pattern {
}
}

impl Pattern {
/// Update string value in enum
pub fn set_string(&mut self, new_string: String) {
match self {
Pattern::CaseSensitiveString(ref mut s) => *s = new_string,
Pattern::CaseInSensitiveString(ref mut s) => *s = new_string,
Pattern::Regex(ref mut s) => *s = new_string,
}
}
}

impl std::ops::Deref for Pattern {
type Target = String;
fn deref(&self) -> &String {
Expand Down
122 changes: 109 additions & 13 deletions wezterm-gui/src/overlay/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use config::keyassignment::{
ClipboardCopyDestination, CopyModeAssignment, KeyAssignment, KeyTable, KeyTableEntry,
ScrollbackEraseMode, SelectionMode,
};
use finl_unicode::grapheme_clusters::Graphemes;
use mux::domain::DomainId;
use mux::pane::{
CachePolicy, ForEachPaneLogicalLine, LogicalLine, Pane, PaneId, Pattern,
Expand Down Expand Up @@ -66,6 +67,7 @@ struct CopyRenderable {

/// The text that the user entered
pattern: Pattern,
search_cursor_index: usize,
/// The most recently queried set of matches
results: Vec<SearchResult>,
by_line: HashMap<StableRowIndex, Vec<MatchResult>>,
Expand Down Expand Up @@ -125,6 +127,17 @@ impl CopyOverlay {
.clone()
.ok_or_else(|| anyhow::anyhow!("failed to clone window handle"))?;
let dims = pane.get_dimensions();
let pattern = if params.pattern.is_empty() {
SAVED_PATTERN
.lock()
.get(&tab_id)
.map(|p| p.clone())
.unwrap_or(params.pattern)
} else {
params.pattern
};

let search_cursor_index = pattern.chars().count();
let mut render = CopyRenderable {
cursor,
window,
Expand All @@ -139,15 +152,8 @@ impl CopyOverlay {
last_result_seqno: SEQ_ZERO,
last_bar_pos: None,
tab_id,
pattern: if params.pattern.is_empty() {
SAVED_PATTERN
.lock()
.get(&tab_id)
.map(|p| p.clone())
.unwrap_or(params.pattern)
} else {
params.pattern
},
pattern,
search_cursor_index,
editing_search: params.editing_search,
result_pos: None,
selection_mode: SelectionMode::Cell,
Expand Down Expand Up @@ -629,6 +635,7 @@ impl CopyRenderable {

fn clear_pattern(&mut self) {
self.pattern.clear();
self.search_cursor_index = 0;
self.update_search();
}

Expand Down Expand Up @@ -676,6 +683,7 @@ impl CopyRenderable {
Pattern::Regex(s) => Pattern::CaseSensitiveString(s.clone()),
};
self.pattern = pattern;
self.search_cursor_index = self.pattern.chars().count();
self.schedule_update_search();
}

Expand Down Expand Up @@ -1141,13 +1149,48 @@ impl Pane for CopyOverlay {
(KeyCode::Char(c), KeyModifiers::NONE)
| (KeyCode::Char(c), KeyModifiers::SHIFT) => {
// Type to add to the pattern
render.pattern.push(c);
if render.pattern.capacity() - render.pattern.len() == 0 {
render.pattern.reserve(10);
}
let position = render.search_cursor_index;
let new_pattern = insert_char_at(&render.pattern, position, c);
render.pattern.set_string(new_pattern);
render.search_cursor_index += 1;

render.schedule_update_search();
}
(KeyCode::Backspace, KeyModifiers::NONE) => {
// Backspace to edit the pattern
render.pattern.pop();
render.schedule_update_search();
if render.pattern.len() > 0
&& render.search_cursor_index > 0
{
let position = render.search_cursor_index;
let new_pattern = delete_char_at(&mut render.pattern, position - 1);
render.pattern.set_string(new_pattern);
if render.search_cursor_index > 0 {
render.search_cursor_index -= 1;
}
render.schedule_update_search();
}
}
(KeyCode::Delete, KeyModifiers::NONE) => {
// Delete to edit the pattern
if render.pattern.len() > 0 {
let position = render.search_cursor_index;
let new_pattern = delete_char_at(&mut render.pattern, position);
render.pattern.set_string(new_pattern);
render.schedule_update_search();
}
}
(KeyCode::LeftArrow, KeyModifiers::NONE) => {
if render.search_cursor_index > 0 {
render.search_cursor_index -= 1;
}
}
(KeyCode::RightArrow, KeyModifiers::NONE) => {
if render.search_cursor_index < render.pattern.chars().count() {
render.search_cursor_index += 1;
}
}
_ => {}
}
Expand Down Expand Up @@ -1260,7 +1303,7 @@ impl Pane for CopyOverlay {
if renderer.editing_search {
// place in the search box
StableCursorPosition {
x: 8 + wezterm_term::unicode_column_width(&renderer.pattern, None),
x: 8 + wezterm_term::unicode_column_width(first_n_chars(&renderer.pattern, renderer.search_cursor_index).as_str(), None),
y: renderer.compute_search_row(),
shape: termwiz::surface::CursorShape::SteadyBlock,
visibility: termwiz::surface::CursorVisibility::Visible,
Expand Down Expand Up @@ -1905,3 +1948,56 @@ pub fn copy_key_table() -> KeyTable {
}
table
}

fn insert_char_at(s: &str, position: usize, insert_char: char) -> String {
let graphemes = Graphemes::new(s).collect::<Vec<&str>>();

// Ensure that the insertion position does not exceed the range
let position = if position > graphemes.len() {
graphemes.len()
} else {
position
};

let mut result = String::new();

for (i, grapheme) in graphemes.iter().enumerate() {
if i == position {
result.push(insert_char);
}
result.push_str(grapheme);
}

// append the char at the end if the insertion position is exactly at the end of the string
if position == graphemes.len() {
result.push(insert_char);
}

result
}

fn delete_char_at(s: &str, position: usize) -> String {
let graphemes = Graphemes::new(s).collect::<Vec<&str>>();

if position >= graphemes.len() {
return s.to_string();
}

// Combine new strings, excluding characters at specified positions
graphemes.iter().enumerate()
.filter(|&(i, _)| i != position)
.map(|(_, &c)| c)
.collect()
}

fn first_n_chars(s: &str, n: usize) -> String {
let graphemes = Graphemes::new(s).collect::<Vec<&str>>();
if n >= graphemes.len() {
return s.to_string();
}
// Get the first n characters
graphemes.iter().enumerate()
.filter(|&(i, _)| i + 1 <= n)
.map(|(_, &c)| c)
.collect()
}