Skip to content

Commit

Permalink
suggestion: added style (#691)
Browse files Browse the repository at this point in the history
* suggestion: added style

supports reverse highlighting using nu.config

```nushell
style: {
    text: white
    selected_text: {
        attr: r
    }
    description_text: white_dimmed
}
```

* added reset

* fix clippy
  • Loading branch information
rsteube authored Jan 24, 2024
1 parent 7d08fc8 commit 7adffd5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/completion/base.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nu_ansi_term::Style;
use std::ops::Range;

/// A span of source code, with positions in bytes
Expand Down Expand Up @@ -79,6 +80,8 @@ pub struct Suggestion {
pub value: String,
/// Optional description for the replacement
pub description: Option<String>,
/// Optional style for the replacement
pub style: Option<Style>,
/// Optional vector of strings in the suggestion. These can be used to
/// represent examples coming from a suggestion
pub extra: Option<Vec<String>>,
Expand Down
25 changes: 16 additions & 9 deletions src/completion/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ impl Completer for DefaultCompleter {
/// assert_eq!(
/// completions.complete("bat",3),
/// vec![
/// Suggestion {value: "batcave".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batcave".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// ]);
///
/// assert_eq!(
/// completions.complete("to the bat",10),
/// vec![
/// Suggestion {value: "batcave".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batcave".into(), description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// ]);
/// ```
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
Expand Down Expand Up @@ -102,6 +102,7 @@ impl Completer for DefaultCompleter {
Suggestion {
value: format!("{span_line}{ext}"),
description: None,
style: None,
extra: None,
span,
append_whitespace: false,
Expand Down Expand Up @@ -176,15 +177,15 @@ impl DefaultCompleter {
/// completions.insert(vec!["test-hyphen","test_underscore"].iter().map(|s| s.to_string()).collect());
/// assert_eq!(
/// completions.complete("te",2),
/// vec![Suggestion {value: "test".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}]);
/// vec![Suggestion {value: "test".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}]);
///
/// let mut completions = DefaultCompleter::with_inclusions(&['-', '_']);
/// completions.insert(vec!["test-hyphen","test_underscore"].iter().map(|s| s.to_string()).collect());
/// assert_eq!(
/// completions.complete("te",2),
/// vec![
/// Suggestion {value: "test-hyphen".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
/// Suggestion {value: "test_underscore".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
/// Suggestion {value: "test-hyphen".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
/// Suggestion {value: "test_underscore".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
/// ]);
/// ```
pub fn with_inclusions(incl: &[char]) -> Self {
Expand Down Expand Up @@ -374,20 +375,23 @@ mod tests {
Suggestion {
value: "null".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 3 },
append_whitespace: false,
},
Suggestion {
value: "number".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 3 },
append_whitespace: false,
},
Suggestion {
value: "nushell".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 3 },
append_whitespace: false,
Expand Down Expand Up @@ -415,20 +419,23 @@ mod tests {
Suggestion {
value: "test".into(),
description: None,
style: None,
extra: None,
span: Span { start: 8, end: 9 },
append_whitespace: false,
},
Suggestion {
value: "this is the reedline crate".into(),
description: None,
style: None,
extra: None,
span: Span { start: 8, end: 9 },
append_whitespace: false,
},
Suggestion {
value: "this is the reedline crate".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 9 },
append_whitespace: false,
Expand Down
1 change: 1 addition & 0 deletions src/completion/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<'menu> HistoryCompleter<'menu> {
Suggestion {
value: value.to_string(),
description: None,
style: None,
extra: None,
span,
append_whitespace: false,
Expand Down
22 changes: 18 additions & 4 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,17 @@ impl ColumnarMenu {
let left_text_size = self.longest_suggestion + self.default_details.col_padding;
let right_text_size = self.get_width().saturating_sub(left_text_size);
format!(
"{}{:max$}{}{}{}",
"{}{}{:max$}{}{}{}{}{}{}",
suggestion
.style
.unwrap_or(self.color.text_style)
.reverse()
.prefix(),
self.color.selected_text_style.prefix(),
&suggestion.value,
RESET,
self.color.description_style.reverse().prefix(),
self.color.selected_text_style.prefix(),
description
.chars()
.take(right_text_size)
Expand All @@ -358,7 +366,12 @@ impl ColumnarMenu {
)
} else {
format!(
"{}{}{}{:>empty$}{}",
"{}{}{}{}{:>empty$}{}",
suggestion
.style
.unwrap_or(self.color.text_style)
.reverse()
.prefix(),
self.color.selected_text_style.prefix(),
&suggestion.value,
RESET,
Expand All @@ -372,7 +385,7 @@ impl ColumnarMenu {
let right_text_size = self.get_width().saturating_sub(left_text_size);
format!(
"{}{:max$}{}{}{}{}{}",
self.color.text_style.prefix(),
suggestion.style.unwrap_or(self.color.text_style).prefix(),
&suggestion.value,
RESET,
self.color.description_style.prefix(),
Expand All @@ -388,7 +401,7 @@ impl ColumnarMenu {
} else {
format!(
"{}{}{}{}{:>empty$}{}{}",
self.color.text_style.prefix(),
suggestion.style.unwrap_or(self.color.text_style).prefix(),
&suggestion.value,
RESET,
self.color.description_style.prefix(),
Expand Down Expand Up @@ -813,6 +826,7 @@ mod tests {
Suggestion {
value: name.to_string(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: pos },
append_whitespace: false,
Expand Down
1 change: 1 addition & 0 deletions src/menu/ide_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@ mod tests {
Suggestion {
value: name.to_string(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: pos },
append_whitespace: false,
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 @@ -497,6 +497,7 @@ mod tests {
.map(|s| Suggestion {
value: s.into(),
description: None,
style: None,
extra: None,
span: Span::new(0, s.len()),
append_whitespace: false,
Expand All @@ -516,6 +517,7 @@ mod tests {
.map(|s| Suggestion {
value: s.into(),
description: None,
style: None,
extra: None,
span: Span::new(0, s.len()),
append_whitespace: false,
Expand Down

0 comments on commit 7adffd5

Please sign in to comment.