>From faf27c47211281628dc5e216b316583da503dcd1 Mon Sep 17 00:00:00 2001 From: Steve Sprang Date: Tue, 9 Jan 2018 14:00:11 -0800 Subject: [PATCH 1/4] utils: Add a procedure for pretty printing tabular data. * guix/utils.scm (pretty-print-table): New procedure. Co-authored-by: Maxim Cournoyer Signed-off-by: Maxim Cournoyer --- guix/utils.scm | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/guix/utils.scm b/guix/utils.scm index 05af86fc37..d43ff8d719 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -10,6 +10,8 @@ ;;; Copyright © 2020 Efraim Flashner ;;; Copyright © 2021 Simon Tournier ;;; Copyright © 2021 Chris Marusich +;;; Copyright © 2018 Steve Sprang +;;; Copyright © 2021 Maxim Cournoyer ;;; ;;; 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,27 @@ according to THRESHOLD, then #f is returned." #f +inf.0 tests))) + +;;; +;;; 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. The columns are separated by a tab +character, and aligned using spaces." + (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" <>) column-widths)) + (fmt (string-append (string-join column-formats "\t") "\t~a"))) + (map (cut format #t "~?~%" fmt <>) rows))) + ;;; Local Variables: ;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1) ;;; End: -- 2.32.0