mirror of
https://github.com/nushell/nushell.git
synced 2025-04-18 18:28:19 +02:00
allow record as text style (#5092)
This commit is contained in:
parent
80f21d37e0
commit
fa6ed7a40b
@ -5,7 +5,7 @@ use nu_color_config::lookup_ansi_color_style;
|
|||||||
use nu_engine::eval_block;
|
use nu_engine::eval_block;
|
||||||
use nu_parser::parse;
|
use nu_parser::parse;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
create_menus,
|
color_value_string, create_menus,
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
extract_value, Config, IntoPipelineData, ParsedKeybinding, ParsedMenu, PipelineData,
|
extract_value, Config, IntoPipelineData, ParsedKeybinding, ParsedMenu, PipelineData,
|
||||||
ShellError, Span, Value,
|
ShellError, Span, Value,
|
||||||
@ -152,6 +152,26 @@ fn add_menu(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! add_style {
|
||||||
|
// first arm match add!(1,2), add!(2,3) etc
|
||||||
|
($name:expr, $cols: expr, $vals:expr, $span:expr, $config: expr, $menu:expr, $f:expr) => {
|
||||||
|
$menu = match extract_value($name, $cols, $vals, $span) {
|
||||||
|
Ok(text) => {
|
||||||
|
let text = match text {
|
||||||
|
Value::String { val, .. } => val.clone(),
|
||||||
|
Value::Record { cols, vals, span } => {
|
||||||
|
color_value_string(span, cols, vals, $config).into_string("", $config)
|
||||||
|
}
|
||||||
|
_ => "green".to_string(),
|
||||||
|
};
|
||||||
|
let style = lookup_ansi_color_style(&text);
|
||||||
|
$f($menu, style)
|
||||||
|
}
|
||||||
|
Err(_) => $menu,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Adds a columnar menu to the editor engine
|
// Adds a columnar menu to the editor engine
|
||||||
pub(crate) fn add_columnar_menu(
|
pub(crate) fn add_columnar_menu(
|
||||||
line_editor: Reedline,
|
line_editor: Reedline,
|
||||||
@ -190,29 +210,33 @@ pub(crate) fn add_columnar_menu(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Value::Record { cols, vals, span } = &menu.style {
|
if let Value::Record { cols, vals, span } = &menu.style {
|
||||||
columnar_menu = match extract_value("text", cols, vals, span) {
|
add_style!(
|
||||||
Ok(text) => {
|
"text",
|
||||||
let text = text.into_string("", config);
|
cols,
|
||||||
columnar_menu.with_text_style(lookup_ansi_color_style(&text))
|
vals,
|
||||||
}
|
span,
|
||||||
Err(_) => columnar_menu,
|
config,
|
||||||
};
|
columnar_menu,
|
||||||
|
ColumnarMenu::with_text_style
|
||||||
columnar_menu = match extract_value("selected_text", cols, vals, span) {
|
);
|
||||||
Ok(selected) => {
|
add_style!(
|
||||||
let selected = selected.into_string("", config);
|
"selected_text",
|
||||||
columnar_menu.with_selected_text_style(lookup_ansi_color_style(&selected))
|
cols,
|
||||||
}
|
vals,
|
||||||
Err(_) => columnar_menu,
|
span,
|
||||||
};
|
config,
|
||||||
|
columnar_menu,
|
||||||
columnar_menu = match extract_value("description_text", cols, vals, span) {
|
ColumnarMenu::with_selected_text_style
|
||||||
Ok(description) => {
|
);
|
||||||
let description = description.into_string("", config);
|
add_style!(
|
||||||
columnar_menu.with_description_text_style(lookup_ansi_color_style(&description))
|
"description_text",
|
||||||
}
|
cols,
|
||||||
Err(_) => columnar_menu,
|
vals,
|
||||||
};
|
span,
|
||||||
|
config,
|
||||||
|
columnar_menu,
|
||||||
|
ColumnarMenu::with_description_text_style
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let marker = menu.marker.into_string("", config);
|
let marker = menu.marker.into_string("", config);
|
||||||
@ -272,29 +296,33 @@ pub(crate) fn add_list_menu(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Value::Record { cols, vals, span } = &menu.style {
|
if let Value::Record { cols, vals, span } = &menu.style {
|
||||||
list_menu = match extract_value("text", cols, vals, span) {
|
add_style!(
|
||||||
Ok(text) => {
|
"text",
|
||||||
let text = text.into_string("", config);
|
cols,
|
||||||
list_menu.with_text_style(lookup_ansi_color_style(&text))
|
vals,
|
||||||
}
|
span,
|
||||||
Err(_) => list_menu,
|
config,
|
||||||
};
|
list_menu,
|
||||||
|
ListMenu::with_text_style
|
||||||
list_menu = match extract_value("selected_text", cols, vals, span) {
|
);
|
||||||
Ok(selected) => {
|
add_style!(
|
||||||
let selected = selected.into_string("", config);
|
"selected_text",
|
||||||
list_menu.with_selected_text_style(lookup_ansi_color_style(&selected))
|
cols,
|
||||||
}
|
vals,
|
||||||
Err(_) => list_menu,
|
span,
|
||||||
};
|
config,
|
||||||
|
list_menu,
|
||||||
list_menu = match extract_value("description_text", cols, vals, span) {
|
ListMenu::with_selected_text_style
|
||||||
Ok(description) => {
|
);
|
||||||
let description = description.into_string("", config);
|
add_style!(
|
||||||
list_menu.with_description_text_style(lookup_ansi_color_style(&description))
|
"description_text",
|
||||||
}
|
cols,
|
||||||
Err(_) => list_menu,
|
vals,
|
||||||
};
|
span,
|
||||||
|
config,
|
||||||
|
list_menu,
|
||||||
|
ListMenu::with_description_text_style
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let marker = menu.marker.into_string("", config);
|
let marker = menu.marker.into_string("", config);
|
||||||
@ -386,29 +414,33 @@ pub(crate) fn add_description_menu(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Value::Record { cols, vals, span } = &menu.style {
|
if let Value::Record { cols, vals, span } = &menu.style {
|
||||||
description_menu = match extract_value("text", cols, vals, span) {
|
add_style!(
|
||||||
Ok(text) => {
|
"text",
|
||||||
let text = text.into_string("", config);
|
cols,
|
||||||
description_menu.with_text_style(lookup_ansi_color_style(&text))
|
vals,
|
||||||
}
|
span,
|
||||||
Err(_) => description_menu,
|
config,
|
||||||
};
|
description_menu,
|
||||||
|
DescriptionMenu::with_text_style
|
||||||
description_menu = match extract_value("selected_text", cols, vals, span) {
|
);
|
||||||
Ok(selected) => {
|
add_style!(
|
||||||
let selected = selected.into_string("", config);
|
"selected_text",
|
||||||
description_menu.with_selected_text_style(lookup_ansi_color_style(&selected))
|
cols,
|
||||||
}
|
vals,
|
||||||
Err(_) => description_menu,
|
span,
|
||||||
};
|
config,
|
||||||
|
description_menu,
|
||||||
description_menu = match extract_value("description_text", cols, vals, span) {
|
DescriptionMenu::with_selected_text_style
|
||||||
Ok(description) => {
|
);
|
||||||
let description = description.into_string("", config);
|
add_style!(
|
||||||
description_menu.with_description_text_style(lookup_ansi_color_style(&description))
|
"description_text",
|
||||||
}
|
cols,
|
||||||
Err(_) => description_menu,
|
vals,
|
||||||
};
|
span,
|
||||||
|
config,
|
||||||
|
description_menu,
|
||||||
|
DescriptionMenu::with_description_text_style
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let marker = menu.marker.into_string("", config);
|
let marker = menu.marker.into_string("", config);
|
||||||
|
@ -272,7 +272,7 @@ fn create_map(value: &Value, config: &Config) -> Result<HashMap<String, Value>,
|
|||||||
Ok(hm)
|
Ok(hm)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_value_string(
|
pub fn color_value_string(
|
||||||
span: &Span,
|
span: &Span,
|
||||||
inner_cols: &[String],
|
inner_cols: &[String],
|
||||||
inner_vals: &[Value],
|
inner_vals: &[Value],
|
||||||
|
Loading…
Reference in New Issue
Block a user