summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWenbo Chen <wenbo.chen86@gmail.com>2024-07-21 20:30:14 +0900
committerGitHub <noreply@github.com>2024-07-21 06:30:14 -0500
commit9ab706db627df18b19eee727c39d6473892de7dd (patch)
tree8285858f2a2fb2db3a2d01c5927ea392455e5594
parent01891d637d7b28cff1985bc3211dc4f544996c64 (diff)
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)
-rw-r--r--crates/nu-command/src/strings/char_.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/crates/nu-command/src/strings/char_.rs b/crates/nu-command/src/strings/char_.rs
index 773721025..aa987438d 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<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,
};