From 5d43617e8ad863ed041309edefd018fdcaa87038 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 30 Jan 2010 16:52:46 -0600 Subject: ls --color: don't emit a final no-op escape sequence * src/ls.c (main): With --color, avoid emitting the final color- resetting escape sequence when it would be a no-op. * tests/ls/color-clear-to-eol: Adjust expected output accordingly. * tests/ls/color-dtype-dir: Likewise. * tests/ls/multihardlink: Likewise. * tests/ls/stat-free-symlinks: Likewise. * tests/misc/ls-misc: Likewise. * NEWS (Changes in behavior): Mention it. C de-Avillez rebased and adapted four of the new sl-dangle* tests in tests/misc/ls-misc. Reported by Jim Avera in http://bugs.launchpad.net/ubuntu/+source/coreutils/+bug/494663 --- NEWS | 5 +++++ src/ls.c | 10 +++++++++- tests/ls/color-clear-to-eol | 2 +- tests/ls/color-dtype-dir | 4 ---- tests/ls/multihardlink | 11 +++++------ tests/ls/stat-free-symlinks | 1 - tests/misc/ls-misc | 17 ++++++++--------- 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index f4c7f97d5..aa37d69e0 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,11 @@ GNU coreutils NEWS -*- outline -*- using the --with-tty-group[=NAME] option, who also compares the group of the TTY device with NAME (or "tty" if no group name is specified). +** Changes in behavior + + ls --color no longer emits the final 3-byte color-resetting escape + sequence when it would be a no-op. + * Noteworthy changes in release 8.4 (2010-01-13) [stable] diff --git a/src/ls.c b/src/ls.c index 9ef7eba21..680679a9a 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1442,7 +1442,15 @@ main (int argc, char **argv) int j; if (used_color) - restore_default_color (); + { + /* Skip the restore when it would be a no-op, i.e., + when left is "\033[" and right is "m". */ + if (!(color_indicator[C_LEFT].len == 2 + && memcmp (color_indicator[C_LEFT].string, "\033[", 2) == 0 + && color_indicator[C_RIGHT].len == 1 + && color_indicator[C_RIGHT].string[0] == 'm')) + restore_default_color (); + } fflush (stdout); /* Restore the default signal handling. */ diff --git a/tests/ls/color-clear-to-eol b/tests/ls/color-clear-to-eol index 96944cb6d..1be68cab6 100755 --- a/tests/ls/color-clear-to-eol +++ b/tests/ls/color-clear-to-eol @@ -29,7 +29,7 @@ touch $long_name || framework_failure e='\33' color_code='0;31;42' c_pre="$e[0m$e[${color_code}m" -c_post="$e[0m$e[K\n$e[m" +c_post="$e[0m$e[K\n" printf "$c_pre$long_name$c_post\n" > exp || framework_failure env TERM=xterm COLUMNS=80 LS_COLORS="*.foo=$color_code" TIME_STYLE=+T \ diff --git a/tests/ls/color-dtype-dir b/tests/ls/color-dtype-dir index 6532d8415..29b872c21 100755 --- a/tests/ls/color-dtype-dir +++ b/tests/ls/color-dtype-dir @@ -36,7 +36,6 @@ chmod o+t sticky || framework_failure ls --color=always > out || fail=1 cat -A out > o1 || fail=1 -echo >> o1 || fail=1 mv o1 out || fail=1 cat <<\EOF > exp || fail=1 @@ -44,7 +43,6 @@ cat <<\EOF > exp || fail=1 ^[[34;42mother-writable^[[0m$ out$ ^[[37;44msticky^[[0m$ -^[[m EOF compare out exp || fail=1 @@ -56,7 +54,6 @@ rm exp LS_COLORS="ow=:" ls --color=always > out || fail=1 cat -A out > o1 || fail=1 -echo >> o1 || fail=1 mv o1 out || fail=1 cat <<\EOF > exp || fail=1 @@ -64,7 +61,6 @@ cat <<\EOF > exp || fail=1 ^[[01;34mother-writable^[[0m$ out$ ^[[37;44msticky^[[0m$ -^[[m EOF compare out exp || fail=1 diff --git a/tests/ls/multihardlink b/tests/ls/multihardlink index 60bb39948..20a94c807 100755 --- a/tests/ls/multihardlink +++ b/tests/ls/multihardlink @@ -30,7 +30,6 @@ code_mh='44;37' code_ex='01;32' code_png='01;35' c0=$(printf '\033[0m') -c_end=$(printf '\033[m') c_mh=$(printf '\033[%sm' $code_mh) c_ex=$(printf '\033[%sm' $code_ex) c_png=$(printf '\033[%sm' $code_png) @@ -44,7 +43,7 @@ compare out out_ok || fail=1 LS_COLORS="mh=$code_mh" ls -U1 --color=always file1 file2 > out || fail=1 printf "$c0${c_mh}file1$c0 ${c_mh}file2$c0 -$c_end" > out_ok || framework_failure +" > out_ok || framework_failure compare out out_ok || fail=1 # hard links and png (hard link coloring takes precedence) @@ -53,7 +52,7 @@ LS_COLORS="mh=$code_mh:*.png=$code_png" ls -U1 --color=always file1 file2.png \ > out || fail=1 printf "$c0${c_mh}file1$c0 ${c_mh}file2.png$c0 -$c_end" > out_ok || framework_failure +" > out_ok || framework_failure compare out out_ok || fail=1 # hard links and exe (exe coloring takes precedence) @@ -63,7 +62,7 @@ LS_COLORS="mh=$code_mh:*.png=$code_png:ex=$code_ex" \ chmod a-x file2.png || framework_failure printf "$c0${c_ex}file1$c0 ${c_ex}file2.png$c0 -$c_end" > out_ok || framework_failure +" > out_ok || framework_failure compare out out_ok || fail=1 # hard links and png (hard link coloring disabled => png coloring enabled) @@ -71,7 +70,7 @@ LS_COLORS="mh=00:*.png=$code_png" ls -U1 --color=always file1 file2.png > out \ || fail=1 printf "file1 $c0${c_png}file2.png$c0 -$c_end" > out_ok || framework_failure +" > out_ok || framework_failure compare out out_ok || fail=1 # hard links and png (hard link coloring not enabled explicitly => png coloring) @@ -79,7 +78,7 @@ LS_COLORS="*.png=$code_png" ls -U1 --color=always file1 file2.png > out \ || fail=1 printf "file1 $c0${c_png}file2.png$c0 -$c_end" > out_ok || framework_failure +" > out_ok || framework_failure compare out out_ok || fail=1 Exit $fail diff --git a/tests/ls/stat-free-symlinks b/tests/ls/stat-free-symlinks index f6cdb9131..6843b97fd 100755 --- a/tests/ls/stat-free-symlinks +++ b/tests/ls/stat-free-symlinks @@ -46,7 +46,6 @@ grep '^stat("x"' err && fail=1 { printf '\033[0m\033[01;35mlink-to-x\033[0m@\n' printf '\033[01;32mx\033[0m*\n' - printf '\033[m' } > exp || fail=1 compare out exp || fail=1 diff --git a/tests/misc/ls-misc b/tests/misc/ls-misc index 27de57c79..e663a5fc8 100755 --- a/tests/misc/ls-misc +++ b/tests/misc/ls-misc @@ -156,10 +156,10 @@ my @Tests = # Test for a bug that was fixed in coreutils-4.5.4. ['sl-F-color', '-F --color=always d', - {OUT => "$e\e[01;36md$e\@\n\e[m"}, + {OUT => "$e\e[01;36md$e\@\n"}, $slink_d, $unlink_d], ['sl-dF-color', '-dF --color=always d', - {OUT => "$e\e[01;36md$e\@\n\e[m"}, + {OUT => "$e\e[01;36md$e\@\n"}, $slink_d, $unlink_d], # A listing with no output should have no color sequences at all. @@ -169,12 +169,12 @@ my @Tests = # Test for a bug fixed after coreutils-6.9. ['sl-target', '--color=always d', - {OUT => "$e\e[01;34mX$e\n\e[m"}, $target, $target2], + {OUT => "$e\e[01;34mX$e\n"}, $target, $target2], # Test for another bug fixed after coreutils-6.9. # This one bites only for a system/file system with d_type support. ['sl-dangle', '--color=always d', - {OUT => "$e\e[40;31;01mX$e\n\e[m"}, + {OUT => "$e\e[40;31;01mX$e\n"}, {PRE => sub { mkdir 'd',0755 or die "d: $!\n"; symlink 'non-existent', 'd/X' or die "d/X: $!\n"; @@ -197,7 +197,7 @@ my @Tests = ], ['sl-dangle3', '-o --color=always l', {OUT_SUBST => 's/.*[0-9][0-9]:[0-9][0-9] //'}, - {OUT => "$e\e[40ml$e -> \e[34mnowhere$e\n\e[m"}, + {OUT => "$e\e[40ml$e -> \e[34mnowhere$e\n"}, {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n"; push_ls_colors('ln=target:or=40:mi=34:') }}, @@ -206,7 +206,7 @@ my @Tests = ], ['sl-dangle4', '-o --color=always l', {OUT_SUBST => 's/.*[0-9][0-9]:[0-9][0-9] //'}, - {OUT => "$e\e[36ml$e -> \e[35mnowhere$e\n\e[m"}, + {OUT => "$e\e[36ml$e -> \e[35mnowhere$e\n"}, {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n"; push_ls_colors('ln=34:mi=35:or=36:') }}, @@ -215,7 +215,7 @@ my @Tests = ], ['sl-dangle5', '-o --color=always l', {OUT_SUBST => 's/.*[0-9][0-9]:[0-9][0-9] //'}, - {OUT => "$e\e[34ml$e -> \e[35mnowhere$e\n\e[m"}, + {OUT => "$e\e[34ml$e -> \e[35mnowhere$e\n"}, {PRE => sub {symlink 'nowhere', 'l' or die "l: $!\n"; push_ls_colors('ln=34:mi=35:') }}, @@ -227,7 +227,7 @@ my @Tests = # To demonstrate it, the file in question (with executable bit set) # must not be a command line argument. ['color-exe1', '--color=always j', - {OUT => "$e\e[01;32md$e\n\e[m"}, + {OUT => "$e\e[01;32md$e\n"}, $exe_in_subdir, $remove_j], # From Stéphane Chazelas. @@ -245,7 +245,6 @@ my @Tests = . "\e[30;43msetgid$e\n" . "\e[37;41msetuid$e\n" . "\e[37;44msticky$e\n" - . "\e[m" }, {POST => sub { -- cgit v1.2.3-70-g09d2