ls: allow --classify to be ignored for non tty output

Have the `ls` `--classify` option take an optional argument for when to
classify ("always", "auto", "never"), just like the optional argument
for `--color`.  When the optional argument is not specified, default to
"always" for backwards compatibility.

* src/ls.c (usage): Update help text.
(decode_switches): Support an optional argument for --classify.
* tests/ls/classify.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the new feature.
This commit is contained in:
Jason Kim authored and Pádraig Brady committed 2020-04-27 13:48:32 +01:00
1 parent f38bd362d9
commit 9c8a385aa4
5 files changed
+103 -5

No files matched your search

+5
View File
@@ -19,6 +19,11 @@ GNU coreutils NEWS -*- outline -*-
directory merely because it was removed. This reverts a change
that was made in release 8.32.
** New Features
ls --classify now supports the "always", "auto", or "never" flags,
to support only outputting classifier characters if connected to a tty.
* Noteworthy changes in release 8.32 (2020-03-05) [stable]
+16 -1
View File
@@ -8004,7 +8004,7 @@ and on a @code{dirent.d_type}-capable file system, @command{ls}
will perform only one @code{stat} call per command line argument.
@item -F
@itemx --classify
@itemx --classify [=@var{when}]
@itemx --indicator-style=classify
@opindex -F
@opindex --classify
@@ -8016,6 +8016,21 @@ for regular files that are executable, append @samp{*}. The file type
indicators are @samp{/} for directories, @samp{@@} for symbolic links,
@samp{|} for FIFOs, @samp{=} for sockets, @samp{>} for doors,
and nothing for regular files.
@var{when} may be omitted, or one of:
@itemize @bullet
@item none
@vindex none @r{classify option}
- Do not classify. This is the default.
@item auto
@vindex auto @r{classify option}
@cindex terminal, using classify iff
- Only classify if standard output is a terminal.
@item always
@vindex always @r{classify option}
- Always classify.
@end itemize
Specifying @option{--classify} and no @var{when} is equivalent to
@option{--classify=always}.
@c The following sentence is the same as the one for -d.
Do not follow symbolic links listed on the
command line unless the @option{--dereference-command-line} (@option{-H}),
+18 -4
View File
@@ -859,7 +859,7 @@ static struct option const long_options[] =
{"width", required_argument, NULL, 'w'},
{"almost-all", no_argument, NULL, 'A'},
{"ignore-backups", no_argument, NULL, 'B'},
{"classify", no_argument, NULL, 'F'},
{"classify", optional_argument, NULL, 'F'},
{"file-type", no_argument, NULL, FILE_TYPE_INDICATOR_OPTION},
{"si", no_argument, NULL, SI_OPTION},
{"dereference-command-line", no_argument, NULL, 'H'},
@@ -2093,8 +2093,20 @@ decode_switches (int argc, char **argv)
break;
case 'F':
indicator_style = classify;
break;
{
int i;
if (optarg)
i = XARGMATCH ("--classify", optarg, when_args, when_types);
else
/* Using --classify with no argument is equivalent to using
--classify=always. */
i = when_always;
if (i == when_always
|| (i == when_if_tty && isatty (STDOUT_FILENO)))
indicator_style = classify;
break;
}
case 'G': /* inhibit display of group info */
print_group = false;
@@ -5353,7 +5365,9 @@ Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n\
"), stdout);
fputs (_("\
-f do not sort, enable -aU, disable -ls --color\n\
-F, --classify append indicator (one of */=>@|) to entries\n\
-F, --classify[=WHEN] append indicator (one of */=>@|) to entries;\n\
WHEN can be 'always' (default if omitted),\n\
'auto', or 'never'\n\
--file-type likewise, except do not append '*'\n\
--format=WORD across -x, commas -m, horizontal -x, long -l,\n\
single-column -1, verbose -l, vertical -C\n\
+1
View File
@@ -593,6 +593,7 @@ all_tests = \
tests/ls/abmon-align.sh \
tests/ls/birthtime.sh \
tests/ls/block-size.sh \
tests/ls/classify.sh \
tests/ls/color-clear-to-eol.sh \
tests/ls/color-dtype-dir.sh \
tests/ls/color-norm.sh \
+63
View File
@@ -0,0 +1,63 @@
#!/bin/sh
# Test --classify processing
# Copyright (C) 2020 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ ls
mkdir testdir || framework_failure_
cd testdir || framework_failure_
mkdir dir || framework_failure_
touch regular executable || framework_failure_
chmod a+x executable || framework_failure_
ln -s regular slink-reg || framework_failure_
ln -s dir slink-dir || framework_failure_
ln -s nowhere slink-dangle || framework_failure_
mknod block b 20 20 2> /dev/null && block="block
"
mknod char c 10 10 2> /dev/null && char="char
"
mkfifo_or_skip_ fifo
cd ..
cat <<EOF > exp
$block${char}dir/
executable*
fifo|
regular
slink-dangle@
slink-dir@
slink-reg@
EOF
sed 's/[*/@|]//' exp > exp2 || framework_failure_
ls --classify testdir > out || fail=1
ls --classify=always testdir > out2 || fail=1
ls --classify=auto testdir > out3 || fail=1
ls --classify=never testdir > out4 || fail=1
compare exp out || fail=1
compare exp out2 || fail=1
compare exp2 out3 || fail=1
compare exp2 out4 || fail=1
returns_ 1 ls --classify=invalid || fail=1
Exit $fail