ansi -l includes previews of attributes (e.g., bold, dimmed, blink, etc.) (#14196)

# Description

A few simple changes:

* Extends the range of previews to include the attributes - Bold,
italic, underline, etc.
* Also resets the colors before *every* preview. Previously we weren't
doing this, so the "string" theme color was bleeding into a few previews
(mostly, if not all, `bg` ones). Now the "default foreground" color is
used for any preview without an explicit foreground color.
* Moves the preview code into the `if use_ansi_coloring` block as a
stupid-nitpick optimization. There's no reason to populate the previews
when they are explicitly not shown with `use_ansi_coloring: false`.
* Moves `reset` to the bottom of the attribute list so that it isn't
previewed. This is a bit of a nitpick as well since internally we send
the same code for both a `reset` and `attr_normal` (which is correct),
but semantically a `reset` doesn't seem like a "previewable" thing,
whereas "normal" text can be demonstrated with a preview.

# User-Facing Changes

`ansi -l` now shows additional previews

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

N/A
This commit is contained in:
Douglas 2024-10-29 10:14:54 -04:00 committed by GitHub
parent 03015ed33f
commit 74bd0e32cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -425,8 +425,6 @@ static CODE_LIST: Lazy<Vec<AnsiCode>> = Lazy::new(|| { vec![
AnsiCode { short_name: Some("grey89"), long_name: "xterm_grey89", code: Color::Fixed(254).prefix().to_string()}, AnsiCode { short_name: Some("grey89"), long_name: "xterm_grey89", code: Color::Fixed(254).prefix().to_string()},
AnsiCode { short_name: Some("grey93"), long_name: "xterm_grey93", code: Color::Fixed(255).prefix().to_string()}, AnsiCode { short_name: Some("grey93"), long_name: "xterm_grey93", code: Color::Fixed(255).prefix().to_string()},
AnsiCode{ short_name: None, long_name: "reset", code: "\x1b[0m".to_owned()},
// Attributes // Attributes
AnsiCode { short_name: Some("n"), long_name: "attr_normal", code: Color::Green.suffix().to_string()}, AnsiCode { short_name: Some("n"), long_name: "attr_normal", code: Color::Green.suffix().to_string()},
AnsiCode { short_name: Some("bo"), long_name: "attr_bold", code: Style::new().bold().prefix().to_string()}, AnsiCode { short_name: Some("bo"), long_name: "attr_bold", code: Style::new().bold().prefix().to_string()},
@ -437,6 +435,8 @@ static CODE_LIST: Lazy<Vec<AnsiCode>> = Lazy::new(|| { vec![
AnsiCode { short_name: Some("h"), long_name: "attr_hidden", code: Style::new().hidden().prefix().to_string()}, AnsiCode { short_name: Some("h"), long_name: "attr_hidden", code: Style::new().hidden().prefix().to_string()},
AnsiCode { short_name: Some("s"), long_name: "attr_strike", code: Style::new().strikethrough().prefix().to_string()}, AnsiCode { short_name: Some("s"), long_name: "attr_strike", code: Style::new().strikethrough().prefix().to_string()},
AnsiCode{ short_name: None, long_name: "reset", code: "\x1b[0m".to_owned()},
// Reference for ansi codes https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 // Reference for ansi codes https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
// Another good reference http://ascii-table.com/ansi-escape-sequences.php // Another good reference http://ascii-table.com/ansi-escape-sequences.php
@ -842,15 +842,19 @@ fn generate_ansi_code_list(
.map(move |(i, ansi_code)| { .map(move |(i, ansi_code)| {
let name = Value::string(ansi_code.long_name, call_span); let name = Value::string(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 102 items in the ansi array are colors
let preview = if i < 389 {
Value::string(format!("{}NUSHELL\u{1b}[0m", &ansi_code.code), call_span)
} else {
Value::string("\u{1b}[0m", call_span)
};
let code = Value::string(ansi_code.code.replace('\u{1b}', "\\e"), call_span); let code = Value::string(ansi_code.code.replace('\u{1b}', "\\e"), call_span);
let record = if use_ansi_coloring { let record = if use_ansi_coloring {
// The first 397 items in the ansi array are previewable
let preview = if i < 397 {
Value::string(
format!("\u{1b}[0m{}NUSHELL\u{1b}[0m", &ansi_code.code),
call_span,
)
} else {
Value::string("\u{1b}[0m", call_span)
};
record! { record! {
"name" => name, "name" => name,
"preview" => preview, "preview" => preview,