From d0e403dc8bf9eaa6db43536d97865f5ad69dafe2 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 30 Dec 2023 12:12:05 +0100 Subject: [PATCH] suggestion: added display --- src/completion/base.rs | 2 ++ src/completion/default.rs | 4 ++++ src/completion/history.rs | 1 + src/menu/columnar_menu.rs | 22 ++++++++++++++-------- src/menu/menu_functions.rs | 2 ++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/completion/base.rs b/src/completion/base.rs index 46937892..4b622471 100644 --- a/src/completion/base.rs +++ b/src/completion/base.rs @@ -60,6 +60,8 @@ pub trait Completer: Send { pub struct Suggestion { /// String replacement that will be introduced to the the buffer pub value: String, + /// Optional display value for the replacement + pub display: Option, /// Optional description for the replacement pub description: Option, /// Optional style for the replacement diff --git a/src/completion/default.rs b/src/completion/default.rs index 693885b9..55b970c3 100644 --- a/src/completion/default.rs +++ b/src/completion/default.rs @@ -98,6 +98,7 @@ impl Completer for DefaultCompleter { Suggestion { value: format!("{span_line}{ext}"), + display: None, description: None, style: None, extra: None, @@ -371,6 +372,7 @@ mod tests { [ Suggestion { value: "null".into(), + display: None, description: None, style: None, extra: None, @@ -379,6 +381,7 @@ mod tests { }, Suggestion { value: "number".into(), + display: None, description: None, style: None, extra: None, @@ -387,6 +390,7 @@ mod tests { }, Suggestion { value: "nushell".into(), + display: None, description: None, style: None, extra: None, diff --git a/src/completion/history.rs b/src/completion/history.rs index 82c6a89b..9f4a2be3 100644 --- a/src/completion/history.rs +++ b/src/completion/history.rs @@ -58,6 +58,7 @@ impl<'menu> HistoryCompleter<'menu> { Suggestion { value: value.to_string(), + display: None, description: None, style: None, extra: None, diff --git a/src/menu/columnar_menu.rs b/src/menu/columnar_menu.rs index 22e7e2f6..960185d7 100644 --- a/src/menu/columnar_menu.rs +++ b/src/menu/columnar_menu.rs @@ -351,7 +351,7 @@ impl ColumnarMenu { .reverse() .prefix(), self.color.selected_text_style.prefix(), - &suggestion.value, + suggestion.display.as_ref().unwrap_or(&suggestion.value), self.color.description_style.reverse().prefix(), self.color.selected_text_style.prefix(), description @@ -372,7 +372,7 @@ impl ColumnarMenu { .reverse() .prefix(), self.color.selected_text_style.prefix(), - &suggestion.value, + suggestion.display.as_ref().unwrap_or(&suggestion.value), RESET, "", self.end_of_line(column), @@ -385,7 +385,7 @@ impl ColumnarMenu { format!( "{}{:max$}{}{}{}{}{}", suggestion.style.unwrap_or(self.color.text_style).prefix(), - &suggestion.value, + suggestion.display.as_ref().unwrap_or(&suggestion.value), RESET, self.color.description_style.prefix(), description @@ -401,7 +401,7 @@ impl ColumnarMenu { format!( "{}{}{}{}{:>empty$}{}{}", suggestion.style.unwrap_or(self.color.text_style).prefix(), - &suggestion.value, + suggestion.display.as_ref().unwrap_or(&suggestion.value), RESET, self.color.description_style.prefix(), "", @@ -418,7 +418,7 @@ impl ColumnarMenu { format!( "{}{:max$}{}{}", marker, - &suggestion.value, + suggestion.display.as_ref().unwrap_or(&suggestion.value), description .chars() .take(empty_space) @@ -435,7 +435,7 @@ impl ColumnarMenu { format!( "{}{}{:>empty$}{}", marker, - &suggestion.value, + suggestion.display.as_ref().unwrap_or(&suggestion.value), "", self.end_of_line(column), empty = empty_space.saturating_sub(marker.len()), @@ -582,10 +582,15 @@ impl Menu for ColumnarMenu { self.working_details.col_width = painter.screen_width() as usize; self.longest_suggestion = self.get_values().iter().fold(0, |prev, suggestion| { - if prev >= suggestion.value.len() { + let suggestion_length = suggestion + .display + .as_ref() + .unwrap_or(&suggestion.value) + .len(); + if prev >= suggestion_length { prev } else { - suggestion.value.len() + suggestion_length } }); } else { @@ -817,6 +822,7 @@ mod tests { fn fake_suggestion(name: &str, pos: usize) -> Suggestion { Suggestion { value: name.to_string(), + display: None, description: None, style: None, extra: None, diff --git a/src/menu/menu_functions.rs b/src/menu/menu_functions.rs index b381fa36..d09264db 100644 --- a/src/menu/menu_functions.rs +++ b/src/menu/menu_functions.rs @@ -487,6 +487,7 @@ mod tests { .into_iter() .map(|s| Suggestion { value: s.into(), + display: None, description: None, style: None, extra: None, @@ -507,6 +508,7 @@ mod tests { .into_iter() .map(|s| Suggestion { value: s.into(), + display: None, description: None, style: None, extra: None,