Skip to content

Commit

Permalink
suggestion: added style
Browse files Browse the repository at this point in the history
supports reverse highlighting using nu.config

```nushell
style: {
    text: white
    selected_text: {
        attr: r
    }
    description_text: white_dimmed
}
```
  • Loading branch information
rsteube committed Dec 30, 2023
1 parent f396223 commit 7cfac29
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/completion/base.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use nu_ansi_term::Style;

/// A span of source code, with positions in bytes
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct Span {
Expand Down Expand Up @@ -60,6 +62,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
22 changes: 13 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 @@ -99,6 +99,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 @@ -173,15 +174,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 @@ -371,20 +372,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
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
2 changes: 1 addition & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ impl Reedline {
self.get_history_session_id(),
)))
.unwrap_or_else(|_| Vec::new())
.get(0)
.first()
.and_then(|history| history.command_line.split_whitespace().next_back())
.map(|token| (parsed.remainder.len(), indicator.len(), token.to_string())),
});
Expand Down
25 changes: 19 additions & 6 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,16 @@ 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,
self.color.description_style.reverse().prefix(),
self.color.selected_text_style.prefix(),
description
.chars()
.take(right_text_size)
Expand All @@ -358,7 +365,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,9 +384,9 @@ 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,
RESET, // TODO affects space suffix of value
self.color.description_style.prefix(),
description
.chars()
Expand All @@ -388,9 +400,9 @@ impl ColumnarMenu {
} else {
format!(
"{}{}{}{}{:>empty$}{}{}",
self.color.text_style.prefix(),
suggestion.style.unwrap_or(self.color.text_style).prefix(),
&suggestion.value,
RESET,
RESET, // TODO affects space suffix of value
self.color.description_style.prefix(),
"",
RESET,
Expand Down Expand Up @@ -806,6 +818,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 @@ -488,6 +488,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 @@ -507,6 +508,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 7cfac29

Please sign in to comment.