Fix output format borken in char --list (#13417)

# Description

Resolve #13395 

When we use the `char --list` command, it also outputs the characters
*bel* and *backspace*, which breaks the table's alignment.
![Screenshot from 2024-07-21
16-10-03](https://github.com/user-attachments/assets/54561cbc-f1a1-4ccd-9561-65dccc939280)
I also found that the behaviour at the beginning of the table is not
correct because of some line change characters, as I mentioned in the
issue discussion.

So, the solution is to create a list containing the characters that do
not need to be output. When the name is in the list, we output the
character as an empty string. Here is the behaviour after fixing.
![Screenshot from 2024-07-21
16-16-08](https://github.com/user-attachments/assets/dd3345a3-6a72-4c90-b331-bc95dc6db66a)
![Screenshot from 2024-07-21
16-16-39](https://github.com/user-attachments/assets/57dc5073-7f8d-40c4-9830-36eba23075e6)
This commit is contained in:
Wenbo Chen 2024-07-21 20:30:14 +09:00 committed by GitHub
parent 01891d637d
commit 9ab706db62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@ use nu_engine::command_prelude::*;
use nu_protocol::Signals;
use once_cell::sync::Lazy;
use std::collections::HashSet;
// Character used to separate directories in a Path Environment variable on windows is ";"
#[cfg(target_family = "windows")]
@ -149,6 +150,24 @@ static CHAR_MAP: Lazy<IndexMap<&'static str, String>> = Lazy::new(|| {
}
});
static NO_OUTPUT_CHARS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
[
// If the character is in the this set, we don't output it to prevent
// the broken of `char --list` command table format and alignment.
"newline",
"enter",
"nl",
"line_feed",
"lf",
"cr",
"crlf",
"bel",
"backspace",
]
.into_iter()
.collect()
});
impl Command for Char {
fn name(&self) -> &str {
"char"
@ -297,6 +316,11 @@ fn generate_character_list(signals: Signals, call_span: Span) -> PipelineData {
CHAR_MAP
.iter()
.map(move |(name, s)| {
let character = if NO_OUTPUT_CHARS.contains(name) {
Value::string("", call_span)
} else {
Value::string(s, call_span)
};
let unicode = Value::string(
s.chars()
.map(|c| format!("{:x}", c as u32))
@ -306,7 +330,7 @@ fn generate_character_list(signals: Signals, call_span: Span) -> PipelineData {
);
let record = record! {
"name" => Value::string(*name, call_span),
"character" => Value::string(s, call_span),
"character" => character,
"unicode" => unicode,
};