Skip to content

Commit

Permalink
suggestion: added display
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jan 24, 2024
1 parent 6c18df1 commit b14255b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/completion/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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<String>,
/// Optional description for the replacement
pub description: Option<String>,
/// Optional style for the replacement
Expand Down
4 changes: 4 additions & 0 deletions src/completion/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Completer for DefaultCompleter {

Suggestion {
value: format!("{span_line}{ext}"),
display: None,
description: None,
style: None,
extra: None,
Expand Down Expand Up @@ -374,6 +375,7 @@ mod tests {
[
Suggestion {
value: "null".into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -382,6 +384,7 @@ mod tests {
},
Suggestion {
value: "number".into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -390,6 +393,7 @@ mod tests {
},
Suggestion {
value: "nushell".into(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
1 change: 1 addition & 0 deletions src/completion/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<'menu> HistoryCompleter<'menu> {

Suggestion {
value: value.to_string(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
22 changes: 14 additions & 8 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl ColumnarMenu {
.reverse()

Check warning on line 351 in src/menu/columnar_menu.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, bashisms)

Diff in /home/runner/work/reedline/reedline/src/menu/columnar_menu.rs

Check warning on line 351 in src/menu/columnar_menu.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, default)

Diff in /home/runner/work/reedline/reedline/src/menu/columnar_menu.rs

Check warning on line 351 in src/menu/columnar_menu.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, sqlite)

Diff in /home/runner/work/reedline/reedline/src/menu/columnar_menu.rs

Check warning on line 351 in src/menu/columnar_menu.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, basqlite)

Diff in /home/runner/work/reedline/reedline/src/menu/columnar_menu.rs

Check warning on line 351 in src/menu/columnar_menu.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, external_printer)

Diff in /home/runner/work/reedline/reedline/src/menu/columnar_menu.rs
.prefix(),
self.color.selected_text_style.prefix(),
&suggestion.value,
suggestion.display.as_ref().unwrap_or(&suggestion.value),
RESET,
self.color.description_style.reverse().prefix(),
self.color.selected_text_style.prefix(),
Expand All @@ -373,7 +373,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),
Expand All @@ -386,7 +386,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
Expand All @@ -402,7 +402,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(),
"",
Expand All @@ -419,7 +419,7 @@ impl ColumnarMenu {
format!(
"{}{:max$}{}{}",
marker,
&suggestion.value,
suggestion.display.as_ref().unwrap_or(&suggestion.value),
description
.chars()
.take(empty_space)
Expand All @@ -436,7 +436,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()),
Expand Down Expand Up @@ -590,10 +590,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 {
Expand Down Expand Up @@ -825,6 +830,7 @@ mod tests {
fn fake_suggestion(name: &str, pos: usize) -> Suggestion {
Suggestion {
value: name.to_string(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
2 changes: 2 additions & 0 deletions src/menu/menu_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ mod tests {
.into_iter()
.map(|s| Suggestion {
value: s.into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -516,6 +517,7 @@ mod tests {
.into_iter()
.map(|s| Suggestion {
value: s.into(),
display: None,
description: None,
style: None,
extra: None,
Expand Down

0 comments on commit b14255b

Please sign in to comment.