changeset 1037:af1780148f7c

Implement ls --color=auto, suggested by Rich Felker.
author Rob Landley <rob@landley.net>
date Sun, 01 Sep 2013 08:00:41 -0500
parents a25239480e7a
children 5b6027634d01
files scripts/make.sh toys/posix/ls.c
diffstat 2 files changed, 12 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/make.sh	Sun Sep 01 07:50:32 2013 -0500
+++ b/scripts/make.sh	Sun Sep 01 08:00:41 2013 -0500
@@ -68,7 +68,7 @@
   sed -n -e "s/.*TOY($FLX"',[ \t]*"\([^"]*\)"[ \t]*,.*)/\1/' \
          -e 't keep;d;:keep' -e 's/^[<>=][0-9]//' -e 's/[?&^]//' \
          -e 't keep' -e 's/[><=][0-9][0-9]*//g' -e 's/+.//g' \
-         -e 's/\[[^]]*\]//g' -e 's/[-?^:&#|@* ]//g' "$@" -e 'p'
+         -e 's/\[[^]]*\]//g' -e 's/[-?^:&#|@* ;]//g' "$@" -e 'p'
 }
 
 # Extract global structure definitions and flag definitions from toys/*/*.c
--- a/toys/posix/ls.c	Sun Sep 01 07:50:32 2013 -0500
+++ b/toys/posix/ls.c	Sun Sep 01 08:00:41 2013 -0500
@@ -5,7 +5,7 @@
  *
  * See http://opengroup.org/onlinepubs/9699919799/utilities/ls.html
 
-USE_LS(NEWTOY(ls, USE_LS_COLOR("(color)")"goACFHLRSacdfiklmnpqrstux1[-1Cglmnox][-cu][-ftS][-HL]", TOYFLAG_BIN))
+USE_LS(NEWTOY(ls, USE_LS_COLOR("(color):;")"goACFHLRSacdfiklmnpqrstux1[-1Cglmnox][-cu][-ftS][-HL]", TOYFLAG_BIN))
 
 config LS
   bool "ls"
@@ -37,10 +37,11 @@
   default y
   depends on LS
   help
-    usage: ls --color
+    usage: ls --color[=auto]
 
     --color  device=yellow  symlink=turquoise/red  dir=blue  socket=purple
              files: exe=green  suid=red  suidfile=redback  stickydir=greenback
+             =auto means detect if output is a tty.
 */
 
 #define FOR_ls
@@ -51,6 +52,8 @@
 // ls -lR starts .: then ./subdir:
 
 GLOBALS(
+  char *color;
+
   struct dirtree *files;
 
   unsigned screen_width;
@@ -381,7 +384,7 @@
       xprintf(" %s ", thyme);
     }
 
-    if ((flags & FLAG_color) && TT.screen_width) {
+    if (flags & FLAG_color) {
       color = color_from_mode(st->st_mode);
       if (color) printf("\033[%d;%dm", color>>8, color&255);
     }
@@ -394,7 +397,7 @@
 
     if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode)) {
       printf(" -> ");
-      if ((flags & FLAG_color) && TT.screen_width) {
+      if (flags & FLAG_color) {
         struct stat st2;
 
         if (fstatat(dirfd, sort[next]->symlink, &st2, 0)) color = 256+31;
@@ -442,7 +445,10 @@
   if (TT.screen_width<2) TT.screen_width = 2;
 
   // Do we have an implied -1
-  if (!isatty(1) || (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)))
+  if (!isatty(1)) {
+    toys.optflags |= FLAG_1;
+    if (TT.color) toys.optflags ^= FLAG_color;
+  } else if (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g))
     toys.optflags |= FLAG_1;
   else if (!(toys.optflags&(FLAG_1|FLAG_x|FLAG_m))) toys.optflags |= FLAG_C;
   // The optflags parsing infrastructure should really do this for us,