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

PATCH: print with fixed no. of columns and across



Something else I've wanted for some time is the ability to specify a
number of columns for printing.  While I'm waiting for a crashed disk
to be restored, here is a patch.

The option is now available with -C.  Also, -a prints across instead of
down first (which is either `column major' or `row major', depending
whether `major' means first or last, which is why I didn't describe it
that way).

Sample usage:
  print -C 2 "${(@k)_comps}" "${(@v)_comps}"
  print -C 2 -a "${(@kv)_comps}"

If the option letters I've picked look like they might clash with
something in another shell, let me know.

I rewrote builtin.c a bit to allow the compiler to optimise constant
strings, and builtins.yo to put print's options in alphabetical order.

Specifying the column padding or using variable-width padding (like the
compressed format of completion listings) are possible enhancements.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.90
diff -u -r1.90 builtin.c
--- Src/builtin.c	31 Oct 2002 18:32:06 -0000	1.90
+++ Src/builtin.c	15 Nov 2002 12:43:57 -0000
@@ -100,7 +100,7 @@
 #endif
 
     BUILTIN("popd", 0, bin_cd, 0, 1, BIN_POPD, NULL, NULL),
-    BUILTIN("print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "RDPbnrsf:lzNu:pioOcm-", NULL),
+    BUILTIN("print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "abcC:Df:ilmnNoOpPrRsu:z-", NULL),
     BUILTIN("printf", 0, bin_print, 1, -1, BIN_PRINTF, NULL, NULL),
     BUILTIN("pushd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_PUSHD, "sPL", NULL),
     BUILTIN("pushln", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
@@ -3172,7 +3172,8 @@
 	    } else {
 		fd = (int)zstrtol(argptr, &eptr, 10);
 		if (*eptr) {
-		    zwarnnam(name, "number expected after -u: %s", argptr, 0);
+		    zwarnnam(name, "number expected after -%c: %s", argptr,
+			     'u');
 		    return 1;
 		}
 	    }
@@ -3209,30 +3210,71 @@
 	    len[n] = strlen(args[n]);
 
     /* -c -- output in columns */
-    if (OPT_ISSET(ops,'c')) {
+    if (OPT_ISSET(ops,'c') || OPT_ISSET(ops,'C')) {
 	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;
-	nc = (columns + 1) / sc;
-	if (!nc)
-	    nc = 1;
+	if (OPT_ISSET(ops,'C')) {
+	    char *eptr, *argptr = OPT_ARG(ops,'C');
+	    nc = (int)zstrtol(argptr, &eptr, 10);
+	    if (*eptr) {
+		zwarnnam(name, "number expected after -%c: %s", argptr, 'C');
+		return 1;
+	    }
+	    if (nc <= 0) {
+		zwarnnam(name, "invalid number of columns: %s", argptr, 0);
+		return 1;
+	    }
+	}
+	else
+	{
+	    nc = (columns + 1) / sc;
+	    if (!nc)
+		nc = 1;
+	}
 	nr = (n + nc - 1) / nc;
 
+	if (OPT_ISSET(ops,'a'))	/* print across, i.e. columns first */
+	    ap = args;
 	for (i = 0; i < nr; i++) {
-	    ap = args + i;
-	    do {
-		l = strlen(*ap);
-		fprintf(fout, "%s", *ap);
-		for (t = nr; t && *ap; t--, ap++);
-		if(*ap)
-		    for (; l < sc; l++)
-			fputc(' ', fout);
-	    } while (*ap);
+	    if (OPT_ISSET(ops,'a'))
+	    {
+		int ic;
+		for (ic = 0; ic < nc && *ap; ic++, ap++)
+		{
+		    l = strlen(*ap);
+		    fprintf(fout, "%s", *ap);
+		    if (*ap)
+			for (; l < sc; l++)
+			    fputc(' ', fout);
+		}
+	    }
+	    else
+	    {
+		ap = args + i;
+		do {
+		    l = strlen(*ap);
+		    fprintf(fout, "%s", *ap);
+		    for (t = nr; t && *ap; t--, ap++);
+		    if(*ap)
+			for (; l < sc; l++)
+			    fputc(' ', fout);
+		} while (*ap);
+	    }
 	    fputc(OPT_ISSET(ops,'N') ? '\0' : '\n', fout);
 	}
 	/* Testing EBADF special-cases >&- redirections */
@@ -4104,7 +4146,7 @@
 	} else {
 	    readfd = (int)zstrtol(argptr, &eptr, 10);
 	    if (*eptr) {
-		zwarnnam(name, "number expected after -u: %s", argptr, 0);
+		zwarnnam(name, "number expected after -%c: %s", argptr, 'u');
 		return 1;
 	    }
 	}
Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.53
diff -u -r1.53 builtins.yo
--- Doc/Zsh/builtins.yo	16 Sep 2002 18:10:54 -0000	1.53
+++ Doc/Zsh/builtins.yo	15 Nov 2002 12:43:57 -0000
@@ -655,7 +655,8 @@
 `tt(-)' in this context are swapped.
 )
 findex(print)
-item(tt(print) [ tt(-bnrslzpNDPoOicm) ] [ tt(-u)var(n) ] [ tt(-f) var(format) ] [ tt(-R) [ tt(-en) ]] [ var(arg) ... ])(
+xitem(tt(print) [ tt(-abcDilmnNoOpPrsz) ] [ tt(-u)var(n) ] [ tt(-f) var(format) ] [ tt(-C) var(cols) ])
+item(  [ tt(-R) [ tt(-en) ]] [ var(arg) ... ])(
 With the `tt(-f)' option the arguments are printed as described by tt(printf).
 With no flags or with the flag `tt(-)', the arguments are printed on
 the standard output as described by tt(echo), with the following differences:
@@ -667,14 +668,9 @@
 sequence, `tt(\)' escapes the following character and is not printed.
 
 startitem()
-item(tt(-r))(
-Ignore the escape conventions of tt(echo).
-)
-item(tt(-R))(
-Emulate the BSD tt(echo) command, which does not process escape sequences
-unless the tt(-e) flag is given.  The tt(-n) flag suppresses the trailing
-newline.  Only the tt(-e) and tt(-n) flags are recognized after
-tt(-R); all other arguments and options are printed.
+item(tt(-a))(
+Print arguments with the column incrementing first.  Only useful with the
+tt(-c) and tt(-C) options.
 )
 item(tt(-b))(
 Recognize all the escape sequences defined for the tt(bindkey) command,
@@ -683,20 +679,33 @@
 ifnzman(noderef(Zle Builtins))\
 .
 )
+item(tt(-c))(
+Print the arguments in columns.  Unless tt(-a) is also given, arguments are
+printed with the row incrementing first.
+)
+item(tt(-C) var(cols))(
+Print the arguments in var(cols) columns.  Unless tt(-a) is also given,
+arguments are printed with the row incrementing first.
+)
+item(tt(-D))(
+Treat the arguments as directory names, replacing prefixes with tt(~)
+expressions, as appropriate.
+)
+item(tt(-i))(
+If given together with tt(-o) or tt(-O), sorting is performed
+case-independently.
+)
+item(tt(-l))(
+Print the arguments separated by newlines instead of spaces.
+)
 item(tt(-m))(
 Take the first argument as a pattern (should be quoted), and remove
 it from the argument list together with subsequent arguments that
 do not match this pattern.
 )
-item(tt(-s))(
-Place the results in the history list instead of on the standard output.
-)
 item(tt(-n))(
 Do not add a newline to the output.
 )
-item(tt(-l))(
-Print the arguments separated by newlines instead of spaces.
-)
 item(tt(-N))(
 Print the arguments separated and terminated by nulls.
 )
@@ -706,31 +715,32 @@
 item(tt(-O))(
 Print the arguments sorted in descending order.
 )
-item(tt(-i))(
-If given together with tt(-o) or tt(-O), sorting is performed
-case-independently.
-)
-item(tt(-c))(
-Print the arguments in columns.
-)
-item(tt(-u)var(n))(
-Print the arguments to file descriptor var(n).
-)
 item(tt(-p))(
 Print the arguments to the input of the coprocess.
 )
-item(tt(-z))(
-Push the arguments onto the editing buffer stack, separated by spaces.
-)
-item(tt(-D))(
-Treat the arguments as directory names, replacing prefixes with tt(~)
-expressions, as appropriate.
-)
 item(tt(-P))(
 Perform prompt expansion (see
 ifzman(zmanref(zshmisc))\
 ifnzman(noderef(Prompt Expansion))\
 ).
+)
+item(tt(-r))(
+Ignore the escape conventions of tt(echo).
+)
+item(tt(-R))(
+Emulate the BSD tt(echo) command, which does not process escape sequences
+unless the tt(-e) flag is given.  The tt(-n) flag suppresses the trailing
+newline.  Only the tt(-e) and tt(-n) flags are recognized after
+tt(-R); all other arguments and options are printed.
+)
+item(tt(-s))(
+Place the results in the history list instead of on the standard output.
+)
+item(tt(-u)var(n))(
+Print the arguments to file descriptor var(n).
+)
+item(tt(-z))(
+Push the arguments onto the editing buffer stack, separated by spaces.
 )
 enditem()
 

-- 
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