From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45021) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eKPLr-0007jm-1k for guix-patches@gnu.org; Thu, 30 Nov 2017 08:58:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eKPLn-00026O-2x for guix-patches@gnu.org; Thu, 30 Nov 2017 08:58:07 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:57236) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eKPLm-00026E-VM for guix-patches@gnu.org; Thu, 30 Nov 2017 08:58:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eKPLm-0000iM-OP for guix-patches@gnu.org; Thu, 30 Nov 2017 08:58:02 -0500 Subject: [bug#29509] [PATCH 3/6] progress: Add 'progress-reporter/bar'. Resent-Message-ID: From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Thu, 30 Nov 2017 14:56:59 +0100 Message-Id: <20171130135702.4321-3-ludo@gnu.org> In-Reply-To: <20171130135702.4321-1-ludo@gnu.org> References: <20171130135702.4321-1-ludo@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 29509@debbugs.gnu.org * guix/progress.scm (progress-reporter/bar): New procedure. --- guix/progress.scm | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/guix/progress.scm b/guix/progress.scm index ba7944214..1ee7ec319 100644 --- a/guix/progress.scm +++ b/guix/progress.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2017 Sou Bunnbu ;;; Copyright © 2015 Steve Sprang +;;; Copyright © 2017 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -32,6 +33,7 @@ progress-reporter/silent progress-reporter/file + progress-reporter/bar byte-count->string current-terminal-columns @@ -212,6 +214,39 @@ ABBREVIATION used to shorten FILE for display." ;; Don't miss the last report. (stop render)))) +(define* (progress-reporter/bar total + #:optional + (prefix "") + (port (current-error-port))) + "Return a reporter that shows a progress bar every time one of the TOTAL +tasks is performed. Write PREFIX at the beginning of the line." + (define done 0) + + (define (report-progress) + (set! done (+ 1 done)) + (unless (> done total) + (let* ((ratio (* 100. (/ done total)))) + (erase-in-line port) + (if (string-null? prefix) + (display (progress-bar ratio (current-terminal-columns)) port) + (let ((width (- (current-terminal-columns) + (string-length prefix) 3))) + (display prefix port) + (display " " port) + (display (progress-bar ratio width) port))) + (force-output port)))) + + (progress-reporter + (start (lambda () + (set! done 0))) + (report report-progress) + (stop (lambda () + (erase-in-line port) + (unless (string-null? prefix) + (display prefix port) + (newline port)) + (force-output port))))) + ;; TODO: replace '(@ (guix build utils) dump-port))'. (define* (dump-port* in out #:key (buffer-size 16384) -- 2.15.0