util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lennard Hofmann <lennard.hofmann@web.de>
To: util-linux@vger.kernel.org
Cc: Lennard Hofmann <lennard.hofmann@web.de>
Subject: [PATCH 1/4] column: Optionally keep empty lines in cols/rows mode
Date: Sun, 20 Sep 2020 14:55:18 +0200	[thread overview]
Message-ID: <20200920125520.28204-1-lennard.hofmann@web.de> (raw)

Signed-off-by: Lennard Hofmann <lennard.hofmann@web.de>
---
It surprised me that the -L option only works in table mode because I like
column(1) for columnating long terminal output on wide screens. I am not aware
of a simple way to achieve this using other Unix utilities: pr(1) truncates
columns and requires you to calculate the number of columns and the output
width manually.

The following patches remove the unnecessary restriction of the -L option. The
patch below simply moves the existing logic to a new function `add_entry()` that
gets called with an empty wcs if the -L option is present.

I am very new to C and mailing lists so I appreciate any feedback.

text-utils/column.c
| 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/text-utils/column.c b/text-utils/column.c
index 238dbab41..bc7851472 100644
--- a/text-utils/column.c
+++ b/text-utils/column.c
@@ -487,6 +487,21 @@ static int add_emptyline_to_table(struct column_control *ctl)
 	return 0;
 }

+static void add_entry(struct column_control *ctl, size_t *maxents, wchar_t *wcs)
+{
+	if (ctl->nents <= *maxents) {
+		*maxents += 1000;
+		ctl->ents = xrealloc(ctl->ents, *maxents * sizeof(wchar_t *));
+	}
+	ctl->ents[ctl->nents] = wcs;
+}
+
+static void add_empty_entry(struct column_control *ctl, size_t *maxents)
+{
+	add_entry(ctl, maxents, mbs_to_wcs(""));
+	ctl->nents++;
+}
+
 static int read_input(struct column_control *ctl, FILE *fp)
 {
 	char *buf = NULL;
@@ -512,8 +527,12 @@ static int read_input(struct column_control *ctl, FILE *fp)
 				*p = '\0';
 		}
 		if (!str || !*str) {
-			if (ctl->mode == COLUMN_MODE_TABLE && ctl->tab_empty_lines)
-				add_emptyline_to_table(ctl);
+			if (ctl->tab_empty_lines) {
+				if (ctl->mode == COLUMN_MODE_TABLE)
+					add_emptyline_to_table(ctl);
+				else
+					add_empty_entry(ctl, &maxents);
+			}
 			continue;
 		}

@@ -539,12 +558,7 @@ static int read_input(struct column_control *ctl, FILE *fp)

 		case COLUMN_MODE_FILLCOLS:
 		case COLUMN_MODE_FILLROWS:
-			if (ctl->nents <= maxents) {
-				maxents += 1000;
-				ctl->ents = xrealloc(ctl->ents,
-						maxents * sizeof(wchar_t *));
-			}
-			ctl->ents[ctl->nents] = wcs;
+			add_entry(ctl, &maxents, wcs);
 			len = width(ctl->ents[ctl->nents]);
 			if (ctl->maxlength < len)
 				ctl->maxlength = len;
--
2.28.0


             reply	other threads:[~2020-09-20 13:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-20 12:55 Lennard Hofmann [this message]
2020-09-21 13:07 ` [PATCH 1/4] column: Optionally keep empty lines in cols/rows mode Karel Zak
2020-09-21 16:00   ` Lennard Hofmann

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=20200920125520.28204-1-lennard.hofmann@web.de \
    --to=lennard.hofmann@web.de \
    --cc=util-linux@vger.kernel.org \
    /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.
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).