summaryrefslogtreecommitdiff
path: root/guix/utils.scm
diff options
context:
space:
mode:
authorMaxim Cournoyer <maxim.cournoyer@gmail.com>2021-10-01 17:10:49 -0400
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>2021-10-01 17:10:49 -0400
commit2e65e4834a226c570866f2e8976ed7f252b45cd1 (patch)
tree21d625bce8d03627680214df4a6622bf8eb79dc9 /guix/utils.scm
parent9c68ecb24dd1660ce736cdcdea0422a73ec318a2 (diff)
parentf1a3c11407b52004e523ec5de20d326c5661681f (diff)
Merge remote-tracking branch 'origin/master' into staging
With resolved conflicts in: gnu/packages/bittorrent.scm gnu/packages/databases.scm gnu/packages/geo.scm gnu/packages/gnupg.scm gnu/packages/gstreamer.scm gnu/packages/gtk.scm gnu/packages/linux.scm gnu/packages/python-xyz.scm gnu/packages/xorg.scm guix/build/qt-utils.scm
Diffstat (limited to 'guix/utils.scm')
-rw-r--r--guix/utils.scm32
1 files changed, 31 insertions, 1 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index 05af86fc37..2920fa7684 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))
;;;
@@ -935,6 +939,32 @@ 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")))
+ (for-each (cut format #t "~?~%" fmt <>) rows)))
+
;;; Local Variables:
;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1)
;;; End: