Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: `print -C' column spacing



This improves the output from print -C by ignoring the width of the
final column.  Otherwise, you could end up with silly spacing.  For
example,

typeset -A testass
testass=(m /a/very/very/long/name/which/should/not/have/any/effect
s /another/long/name/which/still/shouldn\'t\ do\ anything.)
print -aC 2 ${(kv)testass}

pushed the long value names to the right unnecessarily.  This means you
can get better spacing if you select the number of columns
yourself rather than relying on print -c; this could be fixed, but it
would have to be iterative.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.91
diff -u -r1.91 builtin.c
--- Src/builtin.c	20 Nov 2002 12:37:45 -0000	1.91
+++ Src/builtin.c	11 Dec 2002 14:28:08 -0000
@@ -3214,20 +3214,6 @@
 	int l, nc, nr, sc, n, t, i;
 	char **ap;
 
-	/*
-	 * n: loop counter
-	 * ap: array iterator
-	 * l: maximum length seen
-	 */
-	for (n = l = 0, ap = args; *ap; ap++, n++)
-	    if (l < (t = strlen(*ap)))
-		l = t;
-
-	/*
-	 * sc: column width
-	 * nc: number of columns (at least one)
-	 */
-	sc = l + 2;
 	if (OPT_ISSET(ops,'C')) {
 	    char *eptr, *argptr = OPT_ARG(ops,'C');
 	    nc = (int)zstrtol(argptr, &eptr, 10);
@@ -3239,14 +3225,56 @@
 		zwarnnam(name, "invalid number of columns: %s", argptr, 0);
 		return 1;
 	    }
+	    /*
+	     * n: number of elements
+	     * nc: number of columns
+	     * nr: number of rows
+	     */
+	    n = arrlen(args);
+	    nr = (n + nc - 1) / nc;
+
+	    /*
+	     * i: loop counter
+	     * ap: array iterator
+	     * l: maximum length seen
+	     *
+	     * Ignore lengths in last column since they don't affect
+	     * the separation.
+	     */
+	    for (i = l = 0, ap = args; *ap; ap++, i++) {
+		if (OPT_ISSET(ops, 'a')) {
+		    if ((i % nc) == nc - 1)
+			continue;
+		} else {
+		    if (i >= nr * (nc - 1))
+			break;
+		}
+		if (l < (t = strlen(*ap)))
+		    l = t;
+	    }
+	    sc = l + 2;
 	}
 	else
 	{
+	    /*
+	     * n: loop counter
+	     * ap: array iterator
+	     * l: maximum length seen
+	     */
+	    for (n = l = 0, ap = args; *ap; ap++, n++)
+		if (l < (t = strlen(*ap)))
+		    l = t;
+
+	    /*
+	     * sc: column width
+	     * nc: number of columns (at least one)
+	     */
+	    sc = l + 2;
 	    nc = (columns + 1) / sc;
 	    if (!nc)
 		nc = 1;
+	    nr = (n + nc - 1) / nc;
 	}
-	nr = (n + nc - 1) / nc;
 
 	if (OPT_ISSET(ops,'a'))	/* print across, i.e. columns first */
 	    ap = args;

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author