Hi Rob, These two patches should work. The code in "...Remove-trailing..." is different from what we discussed. > - if (curcol < 255) printf("%*c", curcol, ' '); > + // Use dtlen and ul to not print spaces when we are at the end > + if (curcol < 255 && ((dtlen - ul - 1) > 0)) printf("%*c", curcol, ' '); Using `dtlen` and `ul` only deleted the last trailing space. If `-C` or `-x` generates multiline output, every line except the last one would have two trailing spaces. GNU `ls` does not have any trailing spaces. I tried for a while to find a way to detect when we are on the last filename of the line, and could not find one. I ended up putting the padding code with the newline code. I had to add the two other variables to hold on to the values of the previous iteration.
~Andrew
From b6d8bfe762edadfb683505612e380c6dfbe0d469 Mon Sep 17 00:00:00 2001 From: Andrew Ilijic <ilijic.and...@gmail.com> Date: Mon, 28 Oct 2019 17:07:35 -0400 Subject: [PATCH] ls: Remove trailing whitespace so that tests pass When in modes `-C` and `-x` we need to remove the trailing whitespace on each line. This is the behavior of other `ls` commands. Other `ls` commands will print the last filename and then print a newline. Prior to this patch we would print the last filename, followed by two spaces, and then print a newline. Previously, we would get to the end of the loop and print the padding. I couldn't figure out a way to determine when the program had reached the end of a line. So I piggybacked off of the newline code. --- toys/posix/ls.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/toys/posix/ls.c b/toys/posix/ls.c index d4c0211a..4d1c3365 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -401,9 +401,10 @@ static void listfiles(int dirfd, struct dirtree *indir) // Loop through again to produce output. width = 0; + unsigned curcol = 0; for (ul = 0; ul<dtlen; ul++) { int ii, zap; - unsigned curcol, color = 0; + unsigned lastlen = *len, lastcol = curcol, color = 0; unsigned long next = next_column(ul, dtlen, columns, &curcol); struct stat *st = &(sort[next]->st); mode_t mode = st->st_mode; @@ -424,6 +425,10 @@ static void listfiles(int dirfd, struct dirtree *indir) if (mm) xputc(','); if (flags & (FLAG_C|FLAG_x)) { if (!curcol) xputc('\n'); + else { // Pad columns + lastcol = colsizes[lastcol]-lastlen-totpad; + printf("%*c", lastcol, ' '); + } } else if ((flags & FLAG_1) || width+1+*len > TT.screen_width) { xputc('\n'); width = 0; @@ -519,12 +524,6 @@ static void listfiles(int dirfd, struct dirtree *indir) } if (et) xputc(et); - - // Pad columns - if (flags & (FLAG_C|FLAG_x)) { - curcol = colsizes[curcol]-(*len)-totpad; - if (curcol < 255) printf("%*c", curcol, ' '); - } } if (width) xputc('\n'); -- 2.11.0
From 8b780c88c926e6f2b6ab7a0de9be804bd1db8684 Mon Sep 17 00:00:00 2001 From: Andrew Ilijic <ilijic.and...@gmail.com> Date: Mon, 28 Oct 2019 11:21:16 -0400 Subject: [PATCH] ls: Add tests for `-C` and `-x` options These tests ensure we follow the behavior of other `ls` commands, in the basic case. --- tests/ls.test | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ls.test b/tests/ls.test index 91f69183..ab67bbe9 100755 --- a/tests/ls.test +++ b/tests/ls.test @@ -18,6 +18,8 @@ IN="cd lstest" OUT="cd .. " testing "no argument" "$IN && ls; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" "" +testing "with -C: test column spacing equals 2" "$IN && ls -C; $OUT" "dir1 dir2 file1.txt file2.txt\n" "" "" +testing "with -x: test column spacing equals 2" "$IN && ls -x; $OUT" "dir1 dir2 file1.txt file2.txt\n" "" "" testing "with wild char" "$IN && ls file*; $OUT" "file1.txt\nfile2.txt\n" "" "" testing "with wild char - long listing" "$IN && ls -1 file*; $OUT" "file1.txt\nfile2.txt\n" "" "" testing "with -p" "$IN && ls -p; $OUT" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" "" -- 2.11.0
_______________________________________________ Toybox mailing list Toybox@lists.landley.net http://lists.landley.net/listinfo.cgi/toybox-landley.net