summaryrefslogtreecommitdiff
path: root/guix/utils.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2021-07-18 16:05:21 +0200
committerLudovic Courtès <ludo@gnu.org>2021-07-18 19:50:01 +0200
commit0e47fcced442d8e7c1b05184fdc1c14f10ed04ec (patch)
tree4ae844bc0ec3c670f8697bdc24362c122fa718ad /guix/utils.scm
parente4b70bc55a538569465bcedee19d1f2607308e65 (diff)
parent8b1bde7bb3936a64244824500ffe60f123704437 (diff)
downloadguix-0e47fcced442d8e7c1b05184fdc1c14f10ed04ec.tar.gz
guix-0e47fcced442d8e7c1b05184fdc1c14f10ed04ec.tar.xz
Merge branch 'master' into core-updates
Diffstat (limited to 'guix/utils.scm')
-rw-r--r--guix/utils.scm33
1 files changed, 32 insertions, 1 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index b75710eb0d..c5a3a52f9d 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -12,6 +12,8 @@
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
;;; Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
+;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
+;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -135,7 +137,9 @@
canonical-newline-port
string-distance
- string-closest))
+ string-closest
+
+ pretty-print-table))
;;;
@@ -1062,6 +1066,33 @@ according to THRESHOLD, then #f is returned."
#f +inf.0
tests)))
+
+;;;
+;;; 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 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" <>)
+ (map (cut min <> max-column-width)
+ column-widths)))
+ (fmt (string-append (string-join column-formats "\t") "\t~a")))
+ (setvbuf (current-output-port) 'block) ;for better performance
+ (for-each (cut format #t "~?~%" fmt <>) rows)))
+
;;; Local Variables:
;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1)
;;; End: