all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
To: ludo@gnu.org (Ludovic Courtès)
Cc: 30053@debbugs.gnu.org, Steve Sprang <steve.sprang@gmail.com>,
	Roel Janssen <roel@gnu.org>
Subject: [bug#30053] [PATCH 1/3 v2] Improve appearance of tabular output.
Date: Thu, 15 Jul 2021 13:36:16 -0400	[thread overview]
Message-ID: <871r7zcwdr.fsf_-_@gmail.com> (raw)
In-Reply-To: <87im1cnnjb.fsf_-_@gmail.com> (Maxim Cournoyer's message of "Thu,  15 Jul 2021 01:39:36 -0400")

[-- Attachment #1: Type: text/plain, Size: 367 bytes --]

Hi,

Here's an improved version of the first patch.  It now uses a hard limit
on the maximum width of a column, which is a poor man's way of getting
rid of the outliers that cause the table to be too wide in some
situations (such as in 'guix package -A').

Otherwise the performance remain unchanged (from ~2 to ~7 seconds with
'guix package -A' on a fast machine).


[-- Attachment #2: 0001-utils-Add-a-procedure-for-pretty-printing-tabular-da.patch --]
[-- Type: text/x-patch, Size: 2872 bytes --]

From d8fd6c9a1b8677cd69e50fe4f3e50c60c5fb7e35 Mon Sep 17 00:00:00 2001
From: Steve Sprang <scs@stevesprang.com>
Date: Tue, 9 Jan 2018 14:00:11 -0800
Subject: [PATCH] utils: Add a procedure for pretty printing tabular data.

* guix/utils.scm (pretty-print-table): New procedure.

Co-authored-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
---
 guix/utils.scm | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/guix/utils.scm b/guix/utils.scm
index 05af86fc37..f2506d38b4 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -10,6 +10,8 @@
 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
 ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 ;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
+;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
+;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -123,7 +125,9 @@
             canonical-newline-port
 
             string-distance
-            string-closest))
+            string-closest
+
+            pretty-print-table))
 
 \f
 ;;;
@@ -935,6 +939,33 @@ according to THRESHOLD, then #f is returned."
            #f +inf.0
            tests)))
 
+\f
+;;;
+;;; Prettified output.
+;;;
+
+(define* (pretty-print-table rows #:key (max-column-width 20))
+  "Print ROWS in neat columns.  All rows should be lists of strings and each
+row should have the same length.  The columns are separated by a tab
+character, and aligned using spaces.  The maximum width of each column is
+bound by MAX-COLUMN-WIDTH."
+  (let* ((number-of-columns-to-pad (if (null? rows)
+                                       0
+                                       (1- (length (first rows)))))
+         ;; Ignore the last column as it is left aligned and doesn't need
+         ;; padding; this prevents printing extraneous trailing spaces.
+         (column-widths (fold (lambda (row maximums)
+                                (map (cut min <> max-column-width)
+                                     (map max
+                                          (map string-length row)
+                                          maximums)))
+                              ;; Initial max width is 0 for each column.
+                              (make-list number-of-columns-to-pad 0)
+                              (map (cut drop-right <> 1) rows)))
+         (column-formats (map (cut format #f "~~~da" <>) column-widths))
+         (fmt (string-append (string-join column-formats "\t") "\t~a")))
+    (for-each (cut format #t "~?~%" fmt <>) rows)))
+
 ;;; Local Variables:
 ;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1)
 ;;; End:
-- 
2.32.0


[-- Attachment #3: Type: text/plain, Size: 16 bytes --]


Thanks!

Maxim

  reply	other threads:[~2021-07-15 17:37 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-09 22:34 [bug#30053] [PATCH 1/3] Improve appearance of tabular output Steve Sprang
2018-01-09 22:37 ` [bug#30053] [PATCH 2/3] " Steve Sprang
2018-01-09 22:37 ` [bug#30053] [PATCH 3/3] " Steve Sprang
2018-01-11 21:32 ` [bug#30053] [PATCH 1/3] " Ludovic Courtès
2018-01-11 23:32   ` Steve Sprang
2018-01-12 13:28     ` Roel Janssen
2018-01-13 19:59       ` Steve Sprang
2018-01-16 14:16         ` Ludovic Courtès
2018-01-16 23:56           ` Steve Sprang
2021-07-15  5:39           ` Maxim Cournoyer
2021-07-15 17:36             ` Maxim Cournoyer [this message]
2021-07-15 20:15               ` bug#30053: " Maxim Cournoyer
2021-07-21 16:56               ` [bug#30053] " Ludovic Courtès
2021-07-21 21:43                 ` Maxim Cournoyer
2021-07-15 22:05             ` Sarah Morgensen via Guix-patches via
2021-07-16  1:25               ` Maxim Cournoyer
2018-01-12 14:56   ` Danny Milosavljevic
2018-01-12 15:26     ` Leo Famulari
2018-01-12 15:50       ` bug#30087: "guix package -A" hangs with attached package set Danny Milosavljevic
2020-12-03  0:10         ` zimoun
2020-12-22 16:12           ` zimoun
2021-01-11 12:29             ` zimoun
2018-01-13 13:47       ` [bug#30053] [PATCH 1/3] Improve appearance of tabular output Ludovic Courtès

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=871r7zcwdr.fsf_-_@gmail.com \
    --to=maxim.cournoyer@gmail.com \
    --cc=30053@debbugs.gnu.org \
    --cc=ludo@gnu.org \
    --cc=roel@gnu.org \
    --cc=steve.sprang@gmail.com \
    /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 external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.