mirror of
https://github.com/nushell/nushell
synced 2024-12-26 04:53:09 +00:00
This change introduces new `search_result` style supported in the color config. The change also removes obsolete check for `config.ls_colors` for computing the style. `config.ls_colors` has been removed last year, so this removes the reference to the obsolete flag, along with a cleanup that removes all the code that used to rely on ls_colors for highlighting search results. # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
parent
3aab69110e
commit
55689ddb50
9 changed files with 69 additions and 97 deletions
|
@ -2,10 +2,7 @@ use crate::help_aliases::help_aliases;
|
|||
use crate::help_commands::help_commands;
|
||||
use crate::help_modules::help_modules;
|
||||
use fancy_regex::Regex;
|
||||
use nu_ansi_term::{
|
||||
Color::{Red, White},
|
||||
Style,
|
||||
};
|
||||
use nu_ansi_term::Style;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
|
@ -141,6 +138,7 @@ pub fn highlight_search_in_table(
|
|||
search_string: &str,
|
||||
searched_cols: &[&str],
|
||||
string_style: &Style,
|
||||
highlight_style: &Style,
|
||||
) -> Result<Vec<Value>, ShellError> {
|
||||
let orig_search_string = search_string;
|
||||
let search_string = search_string.to_lowercase();
|
||||
|
@ -164,7 +162,12 @@ pub fn highlight_search_in_table(
|
|||
if let Value::String { val: s, span } = val {
|
||||
if s.to_lowercase().contains(&search_string) {
|
||||
*val = Value::String {
|
||||
val: highlight_search_string(s, orig_search_string, string_style)?,
|
||||
val: highlight_search_string(
|
||||
s,
|
||||
orig_search_string,
|
||||
string_style,
|
||||
highlight_style,
|
||||
)?,
|
||||
span: *span,
|
||||
};
|
||||
Ok(true)
|
||||
|
@ -200,6 +203,7 @@ pub fn highlight_search_string(
|
|||
haystack: &str,
|
||||
needle: &str,
|
||||
string_style: &Style,
|
||||
highlight_style: &Style,
|
||||
) -> Result<String, ShellError> {
|
||||
let regex_string = format!("(?i){needle}");
|
||||
let regex = match Regex::new(®ex_string) {
|
||||
|
@ -217,7 +221,6 @@ pub fn highlight_search_string(
|
|||
// strip haystack to remove existing ansi style
|
||||
let stripped_haystack = nu_utils::strip_ansi_likely(haystack);
|
||||
let mut last_match_end = 0;
|
||||
let style = Style::new().fg(White).on(Red);
|
||||
let mut highlighted = String::new();
|
||||
|
||||
for cap in regex.captures_iter(stripped_haystack.as_ref()) {
|
||||
|
@ -236,7 +239,11 @@ pub fn highlight_search_string(
|
|||
.paint(&stripped_haystack[last_match_end..start])
|
||||
.to_string(),
|
||||
);
|
||||
highlighted.push_str(&style.paint(&stripped_haystack[start..end]).to_string());
|
||||
highlighted.push_str(
|
||||
&highlight_style
|
||||
.paint(&stripped_haystack[start..end])
|
||||
.to_string(),
|
||||
);
|
||||
last_match_end = end;
|
||||
}
|
||||
Err(e) => {
|
||||
|
|
|
@ -84,11 +84,18 @@ pub fn help_aliases(
|
|||
// Also note that this sample string is passed into user-written code (the closure that may or may not be
|
||||
// defined for "string").
|
||||
let string_style = style_computer.compute("string", &Value::string("search result", head));
|
||||
let highlight_style =
|
||||
style_computer.compute("search_result", &Value::string("search result", head));
|
||||
|
||||
if let Some(f) = find {
|
||||
let all_cmds_vec = build_help_aliases(engine_state, stack, head);
|
||||
let found_cmds_vec =
|
||||
highlight_search_in_table(all_cmds_vec, &f.item, &["name", "usage"], &string_style)?;
|
||||
let found_cmds_vec = highlight_search_in_table(
|
||||
all_cmds_vec,
|
||||
&f.item,
|
||||
&["name", "usage"],
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?;
|
||||
|
||||
return Ok(found_cmds_vec
|
||||
.into_iter()
|
||||
|
|
|
@ -65,6 +65,8 @@ pub fn help_commands(
|
|||
// Also note that this sample string is passed into user-written code (the closure that may or may not be
|
||||
// defined for "string").
|
||||
let string_style = style_computer.compute("string", &Value::string("search result", head));
|
||||
let highlight_style =
|
||||
style_computer.compute("search_result", &Value::string("search result", head));
|
||||
|
||||
if let Some(f) = find {
|
||||
let all_cmds_vec = build_help_commands(engine_state, head);
|
||||
|
@ -73,6 +75,7 @@ pub fn help_commands(
|
|||
&f.item,
|
||||
&["name", "usage", "search_terms"],
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?;
|
||||
|
||||
return Ok(found_cmds_vec
|
||||
|
|
|
@ -84,11 +84,18 @@ pub fn help_externs(
|
|||
// Also note that this sample string is passed into user-written code (the closure that may or may not be
|
||||
// defined for "string").
|
||||
let string_style = style_computer.compute("string", &Value::string("search result", head));
|
||||
let highlight_style =
|
||||
style_computer.compute("search_result", &Value::string("search result", head));
|
||||
|
||||
if let Some(f) = find {
|
||||
let all_cmds_vec = build_help_externs(engine_state, stack, head);
|
||||
let found_cmds_vec =
|
||||
highlight_search_in_table(all_cmds_vec, &f.item, &["name", "usage"], &string_style)?;
|
||||
let found_cmds_vec = highlight_search_in_table(
|
||||
all_cmds_vec,
|
||||
&f.item,
|
||||
&["name", "usage"],
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?;
|
||||
|
||||
return Ok(found_cmds_vec
|
||||
.into_iter()
|
||||
|
|
|
@ -90,11 +90,18 @@ pub fn help_modules(
|
|||
// Also note that this sample string is passed into user-written code (the closure that may or may not be
|
||||
// defined for "string").
|
||||
let string_style = style_computer.compute("string", &Value::string("search result", head));
|
||||
let highlight_style =
|
||||
style_computer.compute("search_result", &Value::string("search result", head));
|
||||
|
||||
if let Some(f) = find {
|
||||
let all_cmds_vec = build_help_modules(engine_state, stack, head);
|
||||
let found_cmds_vec =
|
||||
highlight_search_in_table(all_cmds_vec, &f.item, &["name", "usage"], &string_style)?;
|
||||
let found_cmds_vec = highlight_search_in_table(
|
||||
all_cmds_vec,
|
||||
&f.item,
|
||||
&["name", "usage"],
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?;
|
||||
|
||||
return Ok(found_cmds_vec
|
||||
.into_iter()
|
||||
|
|
|
@ -163,6 +163,7 @@ impl<'a> StyleComputer<'a> {
|
|||
("list".to_string(), ComputableStyle::Static(Color::White.normal())),
|
||||
("block".to_string(), ComputableStyle::Static(Color::White.normal())),
|
||||
("hints".to_string(), ComputableStyle::Static(Color::DarkGray.normal())),
|
||||
("search_result".to_string(), ComputableStyle::Static(Color::White.normal().on(Color::Red))),
|
||||
]);
|
||||
|
||||
for (key, value) in &config.color_config {
|
||||
|
|
|
@ -1,17 +1,15 @@
|
|||
use nu_cmd_lang::help::highlight_search_string;
|
||||
|
||||
use fancy_regex::Regex;
|
||||
use lscolors::{Color as LsColors_Color, LsColors, Style as LsColors_Style};
|
||||
use nu_ansi_term::{Color, Style};
|
||||
use nu_ansi_term::Style;
|
||||
use nu_color_config::StyleComputer;
|
||||
use nu_engine::{env_to_string, CallExt};
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Config, Example, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
|
||||
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
use nu_utils::get_ls_colors;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Find;
|
||||
|
@ -300,7 +298,7 @@ fn highlight_terms_in_record_with_search_columns(
|
|||
config: &Config,
|
||||
terms: &[Value],
|
||||
string_style: Style,
|
||||
ls_colors: &LsColors,
|
||||
highlight_style: Style,
|
||||
) -> Value {
|
||||
let cols_to_search = if search_cols.is_empty() {
|
||||
cols.to_vec()
|
||||
|
@ -316,18 +314,11 @@ fn highlight_terms_in_record_with_search_columns(
|
|||
let term_str = term.into_string("", config);
|
||||
let output_value =
|
||||
if contains_ignore_case(&val_str, &term_str) && cols_to_search.contains(cur_col) {
|
||||
let (value_to_highlight, highlight_string_style) = if config.use_ls_colors {
|
||||
// Get the original LS_COLORS color
|
||||
get_colored_value_and_string_style(ls_colors, &val_str, &string_style)
|
||||
} else {
|
||||
// No LS_COLORS support, so just use the original value
|
||||
(val_str.clone(), string_style)
|
||||
};
|
||||
|
||||
let highlighted_str = match highlight_search_string(
|
||||
&value_to_highlight,
|
||||
&val_str,
|
||||
&term_str,
|
||||
&highlight_string_style,
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
) {
|
||||
Ok(highlighted_str) => highlighted_str,
|
||||
Err(_) => string_style.paint(term_str).to_string(),
|
||||
|
@ -350,24 +341,6 @@ fn highlight_terms_in_record_with_search_columns(
|
|||
}
|
||||
}
|
||||
|
||||
fn get_colored_value_and_string_style(
|
||||
ls_colors: &LsColors,
|
||||
val_str: &str,
|
||||
string_style: &Style,
|
||||
) -> (String, Style) {
|
||||
let style = ls_colors.style_for_path(val_str);
|
||||
let ansi_style = style
|
||||
.map(LsColors_Style::to_nu_ansi_term_style)
|
||||
.unwrap_or_default();
|
||||
|
||||
let ls_colored_val = ansi_style.paint(val_str).to_string();
|
||||
|
||||
let ansi_term_style = style
|
||||
.map(to_nu_ansi_term_style)
|
||||
.unwrap_or_else(|| *string_style);
|
||||
(ls_colored_val, ansi_term_style)
|
||||
}
|
||||
|
||||
fn contains_ignore_case(string: &str, substring: &str) -> bool {
|
||||
string.to_lowercase().contains(&substring.to_lowercase())
|
||||
}
|
||||
|
@ -401,12 +374,8 @@ fn find_with_rest_and_highlight(
|
|||
// Also note that this sample string is passed into user-written code (the closure that may or may not be
|
||||
// defined for "string").
|
||||
let string_style = style_computer.compute("string", &Value::string("search result", span));
|
||||
|
||||
let ls_colors_env_str = match stack.get_env_var(&engine_state, "LS_COLORS") {
|
||||
Some(v) => Some(env_to_string("LS_COLORS", &v, &engine_state, stack)?),
|
||||
None => None,
|
||||
};
|
||||
let ls_colors = get_ls_colors(ls_colors_env_str);
|
||||
let highlight_style =
|
||||
style_computer.compute("search_result", &Value::string("search result", span));
|
||||
|
||||
let cols_to_search_in_map = match call.get_flag(&engine_state, stack, "columns")? {
|
||||
Some(cols) => cols,
|
||||
|
@ -429,7 +398,7 @@ fn find_with_rest_and_highlight(
|
|||
&config,
|
||||
&terms,
|
||||
string_style,
|
||||
&ls_colors,
|
||||
highlight_style,
|
||||
)
|
||||
}
|
||||
_ => x,
|
||||
|
@ -461,7 +430,7 @@ fn find_with_rest_and_highlight(
|
|||
&config,
|
||||
&terms,
|
||||
string_style,
|
||||
&ls_colors,
|
||||
highlight_style,
|
||||
)
|
||||
}
|
||||
_ => x,
|
||||
|
@ -504,6 +473,7 @@ fn find_with_rest_and_highlight(
|
|||
line,
|
||||
&term_str,
|
||||
&string_style,
|
||||
&highlight_style,
|
||||
)?,
|
||||
span,
|
||||
})
|
||||
|
@ -622,47 +592,6 @@ fn record_matches_term(
|
|||
})
|
||||
}
|
||||
|
||||
fn to_nu_ansi_term_style(style: &LsColors_Style) -> Style {
|
||||
fn to_nu_ansi_term_color(color: &LsColors_Color) -> Color {
|
||||
match *color {
|
||||
LsColors_Color::Fixed(n) => Color::Fixed(n),
|
||||
LsColors_Color::RGB(r, g, b) => Color::Rgb(r, g, b),
|
||||
LsColors_Color::Black => Color::Black,
|
||||
LsColors_Color::Red => Color::Red,
|
||||
LsColors_Color::Green => Color::Green,
|
||||
LsColors_Color::Yellow => Color::Yellow,
|
||||
LsColors_Color::Blue => Color::Blue,
|
||||
LsColors_Color::Magenta => Color::Magenta,
|
||||
LsColors_Color::Cyan => Color::Cyan,
|
||||
LsColors_Color::White => Color::White,
|
||||
|
||||
// Below items are a rough translations to 256 colors as
|
||||
// nu-ansi-term do not have bright variants
|
||||
LsColors_Color::BrightBlack => Color::Fixed(8),
|
||||
LsColors_Color::BrightRed => Color::Fixed(9),
|
||||
LsColors_Color::BrightGreen => Color::Fixed(10),
|
||||
LsColors_Color::BrightYellow => Color::Fixed(11),
|
||||
LsColors_Color::BrightBlue => Color::Fixed(12),
|
||||
LsColors_Color::BrightMagenta => Color::Fixed(13),
|
||||
LsColors_Color::BrightCyan => Color::Fixed(14),
|
||||
LsColors_Color::BrightWhite => Color::Fixed(15),
|
||||
}
|
||||
}
|
||||
|
||||
Style {
|
||||
foreground: style.foreground.as_ref().map(to_nu_ansi_term_color),
|
||||
background: style.background.as_ref().map(to_nu_ansi_term_color),
|
||||
is_bold: style.font_style.bold,
|
||||
is_dimmed: style.font_style.dimmed,
|
||||
is_italic: style.font_style.italic,
|
||||
is_underline: style.font_style.underline,
|
||||
is_blink: style.font_style.slow_blink || style.font_style.rapid_blink,
|
||||
is_reverse: style.font_style.reverse,
|
||||
is_hidden: style.font_style.hidden,
|
||||
is_strikethrough: style.font_style.strikethrough,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -52,6 +52,7 @@ let dark_theme = {
|
|||
list: white
|
||||
block: white
|
||||
hints: dark_gray
|
||||
search_result: {bg: red fg: white}
|
||||
|
||||
shape_and: purple_bold
|
||||
shape_binary: purple_bold
|
||||
|
@ -136,6 +137,7 @@ let light_theme = {
|
|||
list: white
|
||||
block: white
|
||||
hints: dark_gray
|
||||
search_result: {fg: white bg: red}
|
||||
|
||||
shape_and: purple_bold
|
||||
shape_binary: purple_bold
|
||||
|
|
Loading…
Reference in a new issue