zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk (Zsh hackers list)
Subject: PATCH: `print -C' column spacing
Date: Wed, 11 Dec 2002 14:33:39 +0000	[thread overview]
Message-ID: <11923.1039617219@csr.com> (raw)

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@csr.com>                  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.
**********************************************************************


             reply	other threads:[~2002-12-11 14:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-11 14:33 Peter Stephenson [this message]
2002-12-11 17:07 ` DervishD
2002-12-11 17:35   ` Peter Stephenson
2002-12-12 12:00     ` DervishD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11923.1039617219@csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).