From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id cEXODUVy8GCofAAAgWs5BA (envelope-from ) for ; Thu, 15 Jul 2021 19:37:09 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id qM+UB0Vy8GCoFQAAbx9fmQ (envelope-from ) for ; Thu, 15 Jul 2021 17:37:09 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id CA558140B1 for ; Thu, 15 Jul 2021 19:37:08 +0200 (CEST) Received: from localhost ([::1]:51548 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m45IJ-0004gV-Nn for larch@yhetil.org; Thu, 15 Jul 2021 13:37:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36652) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m45IE-0004g8-Qa for guix-patches@gnu.org; Thu, 15 Jul 2021 13:37:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38138) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m45IE-00077F-JW for guix-patches@gnu.org; Thu, 15 Jul 2021 13:37:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1m45IE-0004YU-A1 for guix-patches@gnu.org; Thu, 15 Jul 2021 13:37:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#30053] [PATCH 1/3 v2] Improve appearance of tabular output. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 15 Jul 2021 17:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30053 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Cc: 30053@debbugs.gnu.org, Steve Sprang , Roel Janssen Received: via spool by 30053-submit@debbugs.gnu.org id=B30053.162637058517453 (code B ref 30053); Thu, 15 Jul 2021 17:37:02 +0000 Received: (at 30053) by debbugs.gnu.org; 15 Jul 2021 17:36:25 +0000 Received: from localhost ([127.0.0.1]:49684 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m45Hd-0004XQ-2X for submit@debbugs.gnu.org; Thu, 15 Jul 2021 13:36:25 -0400 Received: from mail-qk1-f174.google.com ([209.85.222.174]:46898) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m45Hb-0004XB-IH for 30053@debbugs.gnu.org; Thu, 15 Jul 2021 13:36:24 -0400 Received: by mail-qk1-f174.google.com with SMTP id 201so5942925qkj.13 for <30053@debbugs.gnu.org>; Thu, 15 Jul 2021 10:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=m1p73KC5CZZSlA9ODBB6wgFOwprkqK9VevCaKNXrPUE=; b=e1I0hfgKNORtN2fRjEwzcN44pkdadUTWK0bDyay01Vwd6DYN2PjNCUYE8rOWE6Meqm YpN10/2OX7EHvbzz7yTw5/8KK8B1IdrTE9qbJG1ui2uJ6mibh21ivH+70I1vMnReDCrY RKH8B4NU8iGDopuFl7SrwhN0NSmdUMWGRjOMR1YHAAdXhfQhZYb3Ub20WPOhWlC7f5kR Ns1LMN2Jo7zu79fpm8Bo28cevOjIFE6B8FA1QVE5w0mGSuiuSEwwm7tTGQfkOO0zfwZs ycjIgwpkTIoOZcH5aa7zmOrrwrzn2IPFMCuIr9jz90hU6W/Sj5j83m2CE4IRJaXoZeoL ErKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=m1p73KC5CZZSlA9ODBB6wgFOwprkqK9VevCaKNXrPUE=; b=XFShD3W4H3Es3PJnFgKvRr8amfYXdVxPiXeO2LcnvqYSgX6Qw//MXE1Ba+sT6uzclW Uj33++MPo9qrfwLfVLFqZmpebPwdMuD+fOie6Ly0QP/vYmp1xFYuk4qwZaeGlR/TWGQV QlGvZlo8aF3fqAUkYg0jW3Jv0cVrBzB/Q7WYD5K7uhXdMT1sGTFPMdN9D86+fybHA9uH iTA8NemiY1uKr8TN3c1ISCy1Y6qaVwbJ3t8m4ljwyAvSPW1/HtRoAgKZ8AhICw6rncsb /c8I/OEEsY0Ru0pj5MAViCdp7G9HZpmRGD87cjCky2I4B5LxcZYYkVHpNL224hOdFhSZ /STA== X-Gm-Message-State: AOAM530/uWk4cBR0k7QDN3Uxc8Lqea3+mZb9IGnS5fJg2Bg+RRN/4ZGn fYn/4AjwFOExG3ciKM2kFmE= X-Google-Smtp-Source: ABdhPJzYad61GxIGmCrQPwjKjO9Zppx9aH6dzZF7t4X2X6xSCDIdCWTSFWtcPRYmsmXhQOYe08c/gg== X-Received: by 2002:a05:620a:17a5:: with SMTP id ay37mr5217463qkb.465.1626370577812; Thu, 15 Jul 2021 10:36:17 -0700 (PDT) Received: from raisin ([2607:fad8:4:6:235e:8579:8464:aacc]) by smtp.gmail.com with ESMTPSA id o27sm2785658qkj.98.2021.07.15.10.36.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 10:36:17 -0700 (PDT) From: Maxim Cournoyer References: <878td4nksy.fsf@gnu.org> <87fu7b2old.fsf@gnu.org> <87zi5d6g8f.fsf@gnu.org> <87im1cnnjb.fsf_-_@gmail.com> Date: Thu, 15 Jul 2021 13:36:16 -0400 In-Reply-To: <87im1cnnjb.fsf_-_@gmail.com> (Maxim Cournoyer's message of "Thu, 15 Jul 2021 01:39:36 -0400") Message-ID: <871r7zcwdr.fsf_-_@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1626370629; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:resent-cc:resent-from:resent-sender: resent-message-id:in-reply-to:in-reply-to:references:references: list-id:list-help:list-unsubscribe:list-subscribe:list-post: dkim-signature; bh=Awt8VlAoWmuyaOWi3BvD/67xkJkA8LBTH+gm0REe3ZQ=; b=dagYHTXacyXSwZK9PQllnGOhuS3INQkfd69l8U4DiS6KVkx24Sy262i56RQ45htpmpFbAW 2raSbCgKVqKIwityUesW708U8Yyw2yuulhSY31cUfAvqDqwQWLGcT35cZfKAhKPCET+m5L zfQsMfE3+H3G0v4gn0GV4bmDTMhnl7rWn/ckVGcTqX3YRBR31VY6jGueaSPxHM7Y+QEAwT ZMu6q6g83yha+tB1fG/Xtc4i1Uj/w/XtENKzcBW23Sd0k7qZG/189xIDsRpxB+ClBU1DSv D3KS1o6zaKK1RCOlf9OE+nPHcXv7Kzjoj+mScXjeWQRyvTDtq+p1sSGXzLOXbw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1626370629; a=rsa-sha256; cv=none; b=QX4qczT+7ZWj5ShFUQL8KaxBO2M8IjNjkCCgHMcVpDGjT0E3eJTtesCzuvb0+GQ/r9z7XN cHpISvCT0BB26XMuy37ZAqt4U92SQmsQXS1TXsXRvYlJg9VrzkODsfIM86RmgSYgJ6hGMt +fldnt/xZKtrWVtWt9jYgdwD0xvgPoSBsliCpVhi2ihHnKkKSOSKijHOzowKGBO74QY1A3 d5eU+Vr4UcSvXV6u+Idg7DocBSIMplgWPuZRghNjAkxfhUtHnxcIMmOutLux9hw35TPsYp UzaV+H7a4fOcqBUP6B9pf5NOcy8oE1inSSYKttfjJHwvFIHvfNaryMlaplHVGg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=e1I0hfgK; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: 0.20 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=e1I0hfgK; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: CA558140B1 X-Spam-Score: 0.20 X-Migadu-Scanner: scn0.migadu.com X-TUID: qKhTieZLvN4u --=-=-= Content-Type: text/plain Hi, Here's an improved version of the first patch. It now uses a hard limit on the maximum width of a column, which is a poor man's way of getting rid of the outliers that cause the table to be too wide in some situations (such as in 'guix package -A'). Otherwise the performance remain unchanged (from ~2 to ~7 seconds with 'guix package -A' on a fast machine). --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-utils-Add-a-procedure-for-pretty-printing-tabular-da.patch Content-Transfer-Encoding: quoted-printable >From d8fd6c9a1b8677cd69e50fe4f3e50c60c5fb7e35 Mon Sep 17 00:00:00 2001 From: Steve Sprang Date: Tue, 9 Jan 2018 14:00:11 -0800 Subject: [PATCH] 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 | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/guix/utils.scm b/guix/utils.scm index 05af86fc37..f2506d38b4 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -10,6 +10,8 @@ ;;; Copyright =C2=A9 2020 Efraim Flashner ;;; Copyright =C2=A9 2021 Simon Tournier ;;; Copyright =C2=A9 2021 Chris Marusich +;;; Copyright =C2=A9 2018 Steve Sprang +;;; Copyright =C2=A9 2021 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -123,7 +125,9 @@ canonical-newline-port =20 string-distance - string-closest)) + string-closest + + pretty-print-table)) =20 ;;; @@ -935,6 +939,33 @@ according to THRESHOLD, then #f is returned." #f +inf.0 tests))) =20 + +;;; +;;; 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 ea= ch +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 (cut min <> max-column-width) + (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"))) + (for-each (cut format #t "~?~%" fmt <>) rows))) + ;;; Local Variables: ;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1) ;;; End: --=20 2.32.0 --=-=-= Content-Type: text/plain Thanks! Maxim --=-=-=--