~Andrew
From dfc237de7b02cb19b41f11ce61bc37d0ca65e67d Mon Sep 17 00:00:00 2001
From: Andrew Ilijic <ilijic.and...@gmail.com>
Date: Mon, 28 Oct 2019 22:20:38 -0400
Subject: [PATCH] ls: Add `-w` which sets the column width

---
 tests/ls.test   |  3 +++
 toys/posix/ls.c | 13 +++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/tests/ls.test b/tests/ls.test
index 91f69183..ce88b04b 100755
--- a/tests/ls.test
+++ b/tests/ls.test
@@ -31,6 +31,9 @@ testing "with -k" "$IN && ls -k; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" "
 testing "with -m" "$IN && ls -m; $OUT" "dir1, dir2, file1.txt, file2.txt\n" "" ""
 testing "with -F" "$IN && ls -F; $OUT" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" ""
 testing "with -dk *" "$IN && ls -dk *; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
+# TODO(ilijic) Remove `sed` commands in `-w` tests after trailing space patch is applied
+testing "with -w - one column" "$IN && ls -xw 5 | sed 's/^[ \t]*//;s/[ \t]*$//' && $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
+testing "with -w - two columns" "$IN && ls -Cw 32 | sed 's/^[ \t]*//;s/[ \t]*$//' && $OUT" "dir1  file1.txt\ndir2  file2.txt\n" "" ""
 testing "with -Z" "$IN && ls -Z file1.txt | egrep -q '^[^ ]+ file1.txt' || echo fail; $OUT" "" "" ""
 testing "with -lZ" "$IN && ls --full-time -lZ file1.txt | egrep -q '^-[rwx-]+ +[0-9]+ +[^ ]+ +[^ ]+ +[^ ]+ +[0-9]+ [0-9][0-9][0-9][0-9]-[0-9][0-9]-.* file1.txt' || echo fail; $OUT" "" "" ""
 
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index d4c0211a..809e2504 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -12,13 +12,13 @@
  *   Posix says the -l date format should vary based on how recent it is
  *   and we do --time-style=long-iso instead
 
-USE_LS(NEWTOY(ls, "(color):;(full-time)(show-control-chars)ZgoACFHLRSabcdfhikl@mnpqrstux1[-Cxm1][-Cxml][-Cxmo][-Cxmg][-cu][-ftS][-HL][!qb]", TOYFLAG_BIN|TOYFLAG_LOCALE))
+USE_LS(NEWTOY(ls, "(color):;(full-time)(show-control-chars)ZgoACFHLRSabcdfhikl@mnpqrstuw#=80<0x1[-Cxm1][-Cxml][-Cxmo][-Cxmg][-cu][-ftS][-HL][!qb]", TOYFLAG_BIN|TOYFLAG_LOCALE))
 
 config LS
   bool "ls"
   default y
   help
-    usage: ls [-ACFHLRSZacdfhiklmnpqrstux1] [--color[=auto]] [directory...]
+    usage: ls [-ACFHLRSZacdfhiklmnpqrstuwx1] [--color[=auto]] [directory...]
 
     List files.
 
@@ -37,7 +37,8 @@ config LS
     -g  like -l but no owner           -h  human readable sizes
     -l  long (show full details)       -m  comma separated
     -n  like -l but numeric uid/gid    -o  like -l but no group
-    -x  columns (horizontal sort)      -ll long with nanoseconds (--full-time)
+    -w  set column width               -x  columns (horizontal sort)
+    -ll long with nanoseconds (--full-time)
     --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.
@@ -54,6 +55,7 @@ config LS
 // ls -lR starts .: then ./subdir:
 
 GLOBALS(
+  long w;
   long l;
   char *color;
 
@@ -564,7 +566,10 @@ void ls_main(void)
   }
 
   TT.screen_width = 80;
-  terminal_size(&TT.screen_width, NULL);
+  if (FLAG(w)) {
+    // TODO (ilijic): Add test for setting w flag
+    TT.screen_width = TT.w;
+  } else { terminal_size(&TT.screen_width, NULL); }
   if (TT.screen_width<2) TT.screen_width = 2;
   if (FLAG(b)) TT.escmore = " \\";
 
-- 
2.11.0

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to