[hackers] [sbase] Implement nl -n format || sin

From: <git_AT_suckless.org>
Date: Fri, 20 Feb 2015 15:14:21 +0100 (CET)

commit 21c43e0710f64e056be730d2f32db4842c60b1b8
Author: sin <sin_AT_2f30.org>
Date: Fri Feb 20 14:12:03 2015 +0000

    Implement nl -n format

diff --git a/README b/README
index 33d74dc..f60d19d 100644
--- a/README
+++ b/README
_AT_@ -48,7 +48,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =* mktemp non-posix none
 =* mv yes none (-i)
 =* nice yes none
-= nl no -d, -f, -h, -l, -n, -p
+= nl no -d, -f, -h, -l, -p
 =* nohup yes none
 #* paste yes none
 =* printenv non-posix none
diff --git a/nl.1 b/nl.1
index 21f4ee9..e4f14c1 100644
--- a/nl.1
+++ b/nl.1
_AT_@ -8,6 +8,7 @@
 .Nm
 .Op Fl b Ar type
 .Op Fl i Ar incr
+.Op Fl n Ar format
 .Op Fl s Ar sep
 .Op Fl v Ar startnum
 .Op Fl w Ar width
_AT_@ -40,6 +41,23 @@ a regular expression as defined in
 .El
 .It Fl i Ar incr
 Defines the increment between numbered lines.
+.It Fl n Ar format
+Specify the line number output format.
+The
+.Ar format
+can be any of the following:
+.Bl -tag -width pstringXX
+.It ln
+Left justified.
+.It rn
+Right justified.
+.It rz
+Right justified with leading zeroes.
+.El
+.Pp
+The default
+.Ar format
+is rn.
 .It Fl s Ar sep
 Defines the string used to separate line numbers and lines. By default this is
 a tab.
diff --git a/nl.c b/nl.c
index 5de4844..7323859 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -8,7 +8,12 @@
 #include "text.h"
 #include "util.h"
 
+#define FORMAT_LN "%-*ld%s%s"
+#define FORMAT_RN "%*ld%s%s"
+#define FORMAT_RZ "%0*ld%s%s"
+
 static char mode = 't';
+static const char *format = FORMAT_RN;
 static const char *sep = "\t";
 static int width = 6;
 static size_t startnum = 1;
_AT_@ -25,7 +30,7 @@ nl(const char *name, FILE *fp)
                 if ((mode == 'a')
                     || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
                     || (mode == 't' && buf[0] != '\n')) {
- printf("%*ld%s%s", width, startnum, sep, buf);
+ printf(format, width, startnum, sep, buf);
                         startnum += incr;
                 } else {
                         printf(" %s", buf);
_AT_@ -39,7 +44,7 @@ nl(const char *name, FILE *fp)
 static void
 usage(void)
 {
- eprintf("usage: %s [-b type] [-i incr] [-s sep] [-v startnum] [-w width] [file]\n", argv0);
+ eprintf("usage: %s [-b type] [-i incr] [-n format] [-s sep] [-v startnum] [-w width] [file]\n", argv0);
 }
 
 int
_AT_@ -60,6 +65,17 @@ main(int argc, char *argv[])
         case 'i':
                 incr = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
                 break;
+ case 'n':
+ format = EARGF(usage());
+ if (!strcmp(format, "ln"))
+ format = FORMAT_LN;
+ else if (!strcmp(format, "rn"))
+ format = FORMAT_RN;
+ else if (!strcmp(format, "rz"))
+ format = FORMAT_RZ;
+ else
+ eprintf("%s: bad format\n", format);
+ break;
         case 's':
                 sep = EARGF(usage());
                 break;
Received on Fri Feb 20 2015 - 15:14:21 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 20 2015 - 15:24:08 CET