forked from extern/nushell
add a display of what the colors look like in ansi --list
(#4866)
* add a display of what the colors look like in `ansi --list` * change 'color' to 'preview' - add the ability to turn it off via config with use_ansi_coloring
This commit is contained in:
parent
7773c4cd4d
commit
4f05e9f4a6
@ -309,9 +309,10 @@ Format: #
|
|||||||
let list: bool = call.has_flag("list");
|
let list: bool = call.has_flag("list");
|
||||||
let escape: bool = call.has_flag("escape");
|
let escape: bool = call.has_flag("escape");
|
||||||
let osc: bool = call.has_flag("osc");
|
let osc: bool = call.has_flag("osc");
|
||||||
|
let use_ansi_coloring = stack.get_config()?.use_ansi_coloring;
|
||||||
|
|
||||||
if list {
|
if list {
|
||||||
return generate_ansi_code_list(engine_state, call.head);
|
return generate_ansi_code_list(engine_state, call.head, use_ansi_coloring);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The code can now be one of the ansi abbreviations like green_bold
|
// The code can now be one of the ansi abbreviations like green_bold
|
||||||
@ -416,16 +417,37 @@ pub fn str_to_ansi(s: &str) -> Option<String> {
|
|||||||
fn generate_ansi_code_list(
|
fn generate_ansi_code_list(
|
||||||
engine_state: &nu_protocol::engine::EngineState,
|
engine_state: &nu_protocol::engine::EngineState,
|
||||||
call_span: Span,
|
call_span: Span,
|
||||||
|
use_ansi_coloring: bool,
|
||||||
) -> Result<nu_protocol::PipelineData, ShellError> {
|
) -> Result<nu_protocol::PipelineData, ShellError> {
|
||||||
return Ok(CODE_LIST
|
return Ok(CODE_LIST
|
||||||
.iter()
|
.iter()
|
||||||
.map(move |ansi_code| {
|
.enumerate()
|
||||||
let cols = vec!["name".into(), "short name".into(), "code".into()];
|
.map(move |(i, ansi_code)| {
|
||||||
|
let cols = if use_ansi_coloring {
|
||||||
|
vec![
|
||||||
|
"name".into(),
|
||||||
|
"preview".into(),
|
||||||
|
"short name".into(),
|
||||||
|
"code".into(),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
vec!["name".into(), "short name".into(), "code".into()]
|
||||||
|
};
|
||||||
let name: Value = Value::string(String::from(ansi_code.long_name), call_span);
|
let name: Value = Value::string(String::from(ansi_code.long_name), call_span);
|
||||||
let short_name = Value::string(ansi_code.short_name.unwrap_or(""), call_span);
|
let short_name = Value::string(ansi_code.short_name.unwrap_or(""), call_span);
|
||||||
|
// The first 96 items in the ansi array are colors
|
||||||
|
let preview = if i < 96 {
|
||||||
|
Value::string(format!("{}NUSHELL\u{1b}[0m", &ansi_code.code), call_span)
|
||||||
|
} else {
|
||||||
|
Value::string("\u{1b}[0m", call_span)
|
||||||
|
};
|
||||||
let code_string = String::from(&ansi_code.code.replace('\u{1b}', ""));
|
let code_string = String::from(&ansi_code.code.replace('\u{1b}', ""));
|
||||||
let code = Value::string(code_string, call_span);
|
let code = Value::string(code_string, call_span);
|
||||||
let vals = vec![name, short_name, code];
|
let vals = if use_ansi_coloring {
|
||||||
|
vec![name, preview, short_name, code]
|
||||||
|
} else {
|
||||||
|
vec![name, short_name, code]
|
||||||
|
};
|
||||||
Value::Record {
|
Value::Record {
|
||||||
cols,
|
cols,
|
||||||
vals,
|
vals,
|
||||||
|
Loading…
Reference in New Issue
Block a user