From 09a6bbb1a8d5d2855cdee06b5937dc3e95b2f401 Mon Sep 17 00:00:00 2001 From: Steve Sprang 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(-) 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