Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: set default quoting style to literal when not TTY #5553

Merged
merged 5 commits into from Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/uu/ls/src/ls.rs
Expand Up @@ -620,7 +620,9 @@ fn extract_quoting_style(options: &clap::ArgMatches, show_control: bool) -> Quot
QuotingStyle::C {
quotes: quoting_style::Quotes::Double,
}
} else if options.get_flag(options::DIRED) {
} else if options.get_flag(options::DIRED) || !std::io::stdout().is_terminal() {
// By default, `ls` uses Literal quoting when
// writing to a non-terminal file descriptor
QuotingStyle::Literal { show_control }
} else {
// TODO: use environment variable if available
Expand Down
15 changes: 11 additions & 4 deletions tests/by-util/test_ls.rs
Expand Up @@ -2453,13 +2453,16 @@ fn test_ls_quoting_style() {
{
at.touch("one\ntwo");
at.touch("one\\two");
// Default is shell-escape
// Default is literal, when stdout is not a TTY.
// Otherwise, it is shell-escape
scene
.ucmd()
.arg("--hide-control-chars")
.arg("one\ntwo")
.succeeds()
.stdout_only("'one'$'\\n''two'\n");
.stdout_only("one?two\n");
// TODO: TTY-expected output, find a way to check this as well
// .stdout_only("'one'$'\\n''two'\n");

for (arg, correct) in [
("--quoting-style=literal", "one?two"),
Expand Down Expand Up @@ -2546,7 +2549,9 @@ fn test_ls_quoting_style() {
.ucmd()
.arg("one two")
.succeeds()
.stdout_only("'one two'\n");
.stdout_only("one two\n");
// TODO: TTY-expected output
// .stdout_only("'one two'\n");

for (arg, correct) in [
("--quoting-style=literal", "one two"),
Expand Down Expand Up @@ -2609,7 +2614,9 @@ fn test_ls_quoting_and_color() {
.arg("--color")
.arg("one two")
.succeeds()
.stdout_only("'one two'\n");
.stdout_only("one two\n");
// TODO: TTY-expected output
// .stdout_only("'one two'\n");
}

#[test]
Expand Down