From 9ab706db627df18b19eee727c39d6473892de7dd Mon Sep 17 00:00:00 2001 From: Wenbo Chen Date: Sun, 21 Jul 2024 20:30:14 +0900 Subject: [PATCH] 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) --- crates/nu-command/src/strings/char_.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/strings/char_.rs b/crates/nu-command/src/strings/char_.rs index 7737210254..aa987438d2 100644 --- a/crates/nu-command/src/strings/char_.rs +++ b/crates/nu-command/src/strings/char_.rs @@ -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> = Lazy::new(|| { } }); +static NO_OUTPUT_CHARS: Lazy> = 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, };