(address . guix-patches@gnu.org)
I noticed when listing installed or available packages that the output
is often pretty jumbled up because columns in each row have an
inconsistent width.
This series of patches adds a new procedure for printing tabular data
(pretty-print-table) and modifies the code for --list-installed,
--list-available, and --list-generations to utilize it.
-Steve
From 09a6bbb1a8d5d2855cdee06b5937dc3e95b2f401 Mon Sep 17 00:00:00 2001
From: Steve Sprang <scs@stevesprang.com>
Date: Tue, 9 Jan 2018 14:00:11 -0800
Subject: [PATCH 1/3] utils: Add a procedure for pretty printing tabular data.
* guix/utils.scm (pretty-print-table): New procedure.
---
guix/utils.scm | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
Toggle diff (42 lines)
diff --git a/guix/utils.scm b/guix/utils.scm
index 92e45de61..cf1d88d21 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -46,7 +46,9 @@
#:use-module ((ice-9 iconv) #:prefix iconv:)
#:use-module (system foreign)
#:re-export (memoize) ; for backwards compatibility
- #:export (strip-keyword-arguments
+ #:export (pretty-print-table
+
+ strip-keyword-arguments
default-keyword-arguments
substitute-keyword-arguments
ensure-keyword-arguments
@@ -299,6 +301,24 @@ This procedure returns #t on success."
#t))))))
+;;;
+;;; Prettified output.
+;;;
+
+(define (pretty-print-table rows)
+ "Print ROWS in neat columns. All rows should be lists of strings and each
+row should have the same length."
+ (let* ((num-cols (if (null? rows) 0 (length (car rows))))
+ (col-widths (fold (lambda (row maximums)
+ (map max (map string-length row) maximums))
+ ;; Initial max width is 0 for each column.
+ (make-list num-cols 0)
+ rows))
+ (col-fmts (map (cut format #f "~~~da" <>) col-widths))
+ (fmt (string-join col-fmts "~/")))
+ (map (cut format #t "~?~%" fmt <>) rows)))
+
+
;;;
;;; Keyword arguments.
;;;
--
2.15.1