From patchwork Mon Apr 1 11:52:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 10879757 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 78EDE14DE for ; Mon, 1 Apr 2019 11:52:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 65285286E1 for ; Mon, 1 Apr 2019 11:52:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 59C0D286FE; Mon, 1 Apr 2019 11:52:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF058286E1 for ; Mon, 1 Apr 2019 11:52:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726690AbfDALwZ (ORCPT ); Mon, 1 Apr 2019 07:52:25 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:40065 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725882AbfDALwZ (ORCPT ); Mon, 1 Apr 2019 07:52:25 -0400 Received: by mail-wr1-f66.google.com with SMTP id h4so11600682wre.7 for ; Mon, 01 Apr 2019 04:52:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aaky29/FccbXURdWIlAx4/1oV9df5zw19GK3KRzD4Xk=; b=rQ7Dl9b4CakaHB532zPW/Mfth1dQr5EpqykGACv3/owLudq4Lm65fFjpiGyAdl+xzF /cvvQmY2R71Wq+ZzaeLhQbGMgcEtSoZnzOax7He0jOX92GyGKn0tiUHwlX97uaGrq/dT VSSqHtPSAnjTSXvMZQFNmmgusDqjo3DsA+sw5wdBPB0Zk9V9t4JMrvBeOHYIj1giOmG9 V54PRefttYu7k5bttEAjWcvU/VngcBmmN5TbcIp0/XQLaWB5y2yZsTtnNqC8Y24jvsxs YNcZd6P0kG0J72S7UnxjXDHhLZ5AH7bfSoTS3Xj2xXpWcQXALkc3VQdeqNXHpvaPYvzM hDOA== 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:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aaky29/FccbXURdWIlAx4/1oV9df5zw19GK3KRzD4Xk=; b=aiMo46NY1jz0vyH0wILWu1yUHlAbYKeKow2DTotqxWOFJvwQQbd7G26ek9Nc7eI92n dLVQ2Si5v9Bvh6871JCYzQ3Ulug1p65z/H+9wkw75GFTIpqDRwyuH41iV44MR75F5GUO hm7n0sLpRnHS3QvQLse9zxAVKq+26Xv/3Q/2UjsyiLDj9nL0nFTRqo7gIUB/NLl5Wp5p qphThiamvKV4bEQSRJ7JywAYhJMvtLX+Ge4BfYhiIuSK7mkh3mBxmw3g5x9YsI0jiZao KJGDqDTsfmTf9TmTkN82APl86w4+q4rqiqdYDpb8s30SUjoGoCLoouQwiwDq990nQhDt 5//A== X-Gm-Message-State: APjAAAXyfBcYS5JsCL0lBJwP5FmaRyiI+R+4GgQKDO2QOJXoX9UKy0oz FGbU+Z50MRKj6qBhStzq0PE= X-Google-Smtp-Source: APXvYqw6Fbj3SnVClEDAtFzyORjZMfv/jmCSyloYlXf5a887kxvYRStuJrI4AJ0km/n9tPU9jbpe9A== X-Received: by 2002:adf:dcce:: with SMTP id x14mr29846162wrm.315.1554119543244; Mon, 01 Apr 2019 04:52:23 -0700 (PDT) Received: from localhost.localdomain (x4db6660d.dyn.telefonica.de. [77.182.102.13]) by smtp.gmail.com with ESMTPSA id f11sm11250970wrm.30.2019.04.01.04.52.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Apr 2019 04:52:22 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= , Jeff King , Duy Nguyen , Luke Mewburn , git@vger.kernel.org, =?utf-8?q?SZEDER_G?= =?utf-8?q?=C3=A1bor?= Subject: [PATCH v2 1/4] progress: make display_progress() return void Date: Mon, 1 Apr 2019 13:52:14 +0200 Message-Id: <20190401115217.3423-2-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.539.g07239c3a71.dirty In-Reply-To: <20190401115217.3423-1-szeder.dev@gmail.com> References: <20190325103844.26749-1-szeder.dev@gmail.com> <20190401115217.3423-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Ever since the progress infrastructure was introduced in 96a02f8f6d (common progress display support, 2007-04-18), display_progress() has returned an int, telling callers whether it updated the progress bar or not. However, this is: - useless, because over the last dozen years there has never been a single caller that cared about that return value. - not quite true, because it doesn't print a progress bar when running in the background, yet it returns 1; see 85cb8906f0 (progress: no progress in background, 2015-04-13). The related display_throughput() function returned void already upon its introduction in cf84d51c43 (add throughput to progress display, 2007-10-30). Let's make display_progress() return void, too. Signed-off-by: SZEDER Gábor --- progress.c | 15 ++++++++------- progress.h | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/progress.c b/progress.c index 5a99c9fbf0..02a20e7d58 100644 --- a/progress.c +++ b/progress.c @@ -78,12 +78,12 @@ static int is_foreground_fd(int fd) return tpgrp < 0 || tpgrp == getpgid(0); } -static int display(struct progress *progress, uint64_t n, const char *done) +static void display(struct progress *progress, uint64_t n, const char *done) { const char *eol, *tp; if (progress->delay && (!progress_update || --progress->delay)) - return 0; + return; progress->last_value = n; tp = (progress->throughput) ? progress->throughput->display.buf : ""; @@ -100,7 +100,7 @@ static int display(struct progress *progress, uint64_t n, const char *done) fflush(stderr); } progress_update = 0; - return 1; + return; } } else if (progress_update) { if (is_foreground_fd(fileno(stderr)) || done) { @@ -109,10 +109,10 @@ static int display(struct progress *progress, uint64_t n, const char *done) fflush(stderr); } progress_update = 0; - return 1; + return; } - return 0; + return; } static void throughput_string(struct strbuf *buf, uint64_t total, @@ -188,9 +188,10 @@ void display_throughput(struct progress *progress, uint64_t total) display(progress, progress->last_value, NULL); } -int display_progress(struct progress *progress, uint64_t n) +void display_progress(struct progress *progress, uint64_t n) { - return progress ? display(progress, n, NULL) : 0; + if (progress) + display(progress, n, NULL); } static struct progress *start_progress_delay(const char *title, uint64_t total, diff --git a/progress.h b/progress.h index 70a4d4a0d6..59e40cc4fd 100644 --- a/progress.h +++ b/progress.h @@ -4,7 +4,7 @@ struct progress; void display_throughput(struct progress *progress, uint64_t total); -int display_progress(struct progress *progress, uint64_t n); +void display_progress(struct progress *progress, uint64_t n); struct progress *start_progress(const char *title, uint64_t total); struct progress *start_delayed_progress(const char *title, uint64_t total); void stop_progress(struct progress **progress); From patchwork Mon Apr 1 11:52:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 10879763 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DC7CB14DE for ; Mon, 1 Apr 2019 11:52:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C7F76286DE for ; Mon, 1 Apr 2019 11:52:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC40B286F6; Mon, 1 Apr 2019 11:52:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D25F286DE for ; Mon, 1 Apr 2019 11:52:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726827AbfDALw3 (ORCPT ); Mon, 1 Apr 2019 07:52:29 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:55986 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbfDALw0 (ORCPT ); Mon, 1 Apr 2019 07:52:26 -0400 Received: by mail-wm1-f68.google.com with SMTP id o25so10070815wmf.5 for ; Mon, 01 Apr 2019 04:52:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m3Tio5fjCwPDnH5z24dGZajGLAF5zsfpLL1cnXKmJWY=; b=EUNAKkqtGq4bPpccCv/qeysnFfcaYDEY/jt/JsQ0iVZL9wLWI/jzMXajJXJdAFyeBa bcDPSBU+aZjgQ4fLVZsAg0+Qssy8XiXB1lb/XhN0MqdCsXSQWWXHCeFY4z3u3Sgvj/4c RJeT0pKTi3yrnqSmadgaJSpmGghEdMGrnsvn02u1t706qr1rPxdeiKWUUYtvwH8INJv7 BowNM7rE0JdfsBimK32DQ2n+s4wSh4TRM1ZG1/XzZbU6M4DH/tDmFOpIvDXPrFj4FrjE T7nojz3V1FoOj0Svj349tvokBUEkARggXcrxpZ5xwXBIBzkpIQwRNuo6U5p9AWvYEnhZ QhuQ== 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:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=m3Tio5fjCwPDnH5z24dGZajGLAF5zsfpLL1cnXKmJWY=; b=sCDr6JFIpAgjkFXChu/8DwsEfNBqk8nzTrmiaAuZWRWH6E7sT2DGey/4HmWQ6zgWZk cyYsYmYPI9DRtozult/QbOY8YpYWOKXSu5k/BoORtPPsEWrWORiZoJWMmqzkow0zs2Ek bwtaYFFvxK6oUVcPg0u514uu5VNY5RDYGSCibMgkOoP6CoZVGAumRzT3Q2nJ0v9nYYnv uXlRcPZyjNV5Sdg3SlM62xAh9LnT/yXIugzXBCbGLW0kjdvcxYlsV77H6xdaMQ4etIrm 6v2i84GAaZAzjb7uM4mpJ7uADb1cRlllxDaiXImSCjCR64EKil9tgnZAFpWjS/AWJ8IK ueuQ== X-Gm-Message-State: APjAAAVhwIUf4Bh4Rq50V1fdQEZIdRKjkoyRwX/ucfQ6vNq/hE9Jn261 R/EWN+a8rdU2COvZ9LtYa1s7kwV8 X-Google-Smtp-Source: APXvYqzDInJPUcoDbPh88b0haXW+svG6IZwAFrT4fawN7Yg4hMo0c2wVAaTH7Cij8MA5c1RjOtw6Gw== X-Received: by 2002:a1c:c101:: with SMTP id r1mr11630809wmf.123.1554119544400; Mon, 01 Apr 2019 04:52:24 -0700 (PDT) Received: from localhost.localdomain (x4db6660d.dyn.telefonica.de. [77.182.102.13]) by smtp.gmail.com with ESMTPSA id f11sm11250970wrm.30.2019.04.01.04.52.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Apr 2019 04:52:23 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= , Jeff King , Duy Nguyen , Luke Mewburn , git@vger.kernel.org, =?utf-8?q?SZEDER_G?= =?utf-8?q?=C3=A1bor?= Subject: [PATCH v2 2/4] progress: assemble percentage and counters in a strbuf before printing Date: Mon, 1 Apr 2019 13:52:15 +0200 Message-Id: <20190401115217.3423-3-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.539.g07239c3a71.dirty In-Reply-To: <20190401115217.3423-1-szeder.dev@gmail.com> References: <20190325103844.26749-1-szeder.dev@gmail.com> <20190401115217.3423-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The following patches in this series want to handle the progress bar's title and changing parts (i.e. the counter and the optional percentage and throughput combined) differently, and need to know the length of the changing parts of the previously displayed progress bar. To prepare for those changes assemble the changing parts in a separate strbuf kept in 'struct progress' before printing. Signed-off-by: SZEDER Gábor --- progress.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/progress.c b/progress.c index 02a20e7d58..842db14b72 100644 --- a/progress.c +++ b/progress.c @@ -36,6 +36,7 @@ struct progress { unsigned delay; struct throughput *throughput; uint64_t start_ns; + struct strbuf counters_sb; }; static volatile sig_atomic_t progress_update; @@ -80,36 +81,42 @@ static int is_foreground_fd(int fd) static void display(struct progress *progress, uint64_t n, const char *done) { - const char *eol, *tp; + const char *tp; + struct strbuf *counters_sb = &progress->counters_sb; + int show_update = 0; if (progress->delay && (!progress_update || --progress->delay)) return; progress->last_value = n; tp = (progress->throughput) ? progress->throughput->display.buf : ""; - eol = done ? done : " \r"; if (progress->total) { unsigned percent = n * 100 / progress->total; if (percent != progress->last_percent || progress_update) { progress->last_percent = percent; - if (is_foreground_fd(fileno(stderr)) || done) { - fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s", - progress->title, percent, - (uintmax_t)n, (uintmax_t)progress->total, - tp, eol); - fflush(stderr); - } - progress_update = 0; - return; + + strbuf_reset(counters_sb); + strbuf_addf(counters_sb, + "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent, + (uintmax_t)n, (uintmax_t)progress->total, + tp); + show_update = 1; } } else if (progress_update) { + strbuf_reset(counters_sb); + strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp); + show_update = 1; + } + + if (show_update) { if (is_foreground_fd(fileno(stderr)) || done) { - fprintf(stderr, "%s: %"PRIuMAX"%s%s", - progress->title, (uintmax_t)n, tp, eol); + const char *eol = done ? done : " \r"; + + fprintf(stderr, "%s: %s%s", progress->title, + counters_sb->buf, eol); fflush(stderr); } progress_update = 0; - return; } return; @@ -211,6 +218,7 @@ static struct progress *start_progress_delay(const char *title, uint64_t total, progress->delay = delay; progress->throughput = NULL; progress->start_ns = getnanotime(); + strbuf_init(&progress->counters_sb, 0); set_progress_signal(); return progress; } @@ -254,6 +262,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg) free(buf); } clear_progress_signal(); + strbuf_release(&progress->counters_sb); if (progress->throughput) strbuf_release(&progress->throughput->display); free(progress->throughput); From patchwork Mon Apr 1 11:52:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 10879767 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0CDA61575 for ; Mon, 1 Apr 2019 11:52:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EC568286DE for ; Mon, 1 Apr 2019 11:52:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0F16286F6; Mon, 1 Apr 2019 11:52:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 863F8286DE for ; Mon, 1 Apr 2019 11:52:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726789AbfDALw3 (ORCPT ); Mon, 1 Apr 2019 07:52:29 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:42452 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726544AbfDALw1 (ORCPT ); Mon, 1 Apr 2019 07:52:27 -0400 Received: by mail-wr1-f67.google.com with SMTP id g3so11605431wrx.9 for ; Mon, 01 Apr 2019 04:52:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PnDxTY7/ZS55dgLcWzElM8Dh936JJMBgA0ELfkgun00=; b=l4xyXp3LwWm5xqTw96LGcFvrpQK8tCOAlCxr3h21XzPXcIKNmfoOlJ8DP0hS8+IimX m48QiJqJWy4GYtqPpmDZQEtcROkar8uixz+8zsMEQpl9yESV3sXnio9YVxJF1MefZfGQ OjwoLb5TE4U2f1lzGndyYp+721ihAL+GgZEWH0ElcwKv4deVlOHMUx7kywFB6f6CGgap /fn15rCFWxy1ahub8GinRBuau5z58saGsjlM62GRUKwdtmrNX6ZmLkktkeV2aMeSk6S+ 7IWyyizVFmksRi3TmRP8kiC6EzvQBEUeYkisXcWq2tbOM2V8Bs5bsAVYRqAiwQfWragA Wrvw== 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:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PnDxTY7/ZS55dgLcWzElM8Dh936JJMBgA0ELfkgun00=; b=JBCDc7TboDw7K2qGH1TH2sEtVgV8DRKWZM4XbXL8ZMUMPrmR8Ms0x0S1NA2AMIijks Gnrjy7jy5EHZFwBiuEhb+JCMTjaVKbjmI7XtTXFuDM9kCZ3fViRwFQx0E5fyNa0MQHOh IntPY/W5r672F31xCW88PYCerbILEKJHnalf2G46h8NGTCVKpqDjOYRNrhc9YcBUGXe+ 4aDZrgx9bfiFKbetlYRaz2FlsCEAxENAlMHc4hs5gCgM5wMD9vpgK+Ry4TuRC2OvvB8v nEoQD2Yx3FuqbejNjSlqcRQux91nPBEzP+sKmgejDuu722lssSdHuAYILucYKeVyQVu6 hXLw== X-Gm-Message-State: APjAAAVASSoWxt3Ph907ZAMIMmGcelg3u2Fxe6xLNEXrPbqP8eLlzoHq /pQ+F/jqVraUcHPeYb5LK9o= X-Google-Smtp-Source: APXvYqy0mhkSgjAaVEJKjScMjQ0WqbojoaE78Kvwv3FEQe2uj8E/e2hBuS/tedhur496N+SEHo7qfg== X-Received: by 2002:a05:6000:124a:: with SMTP id j10mr42266839wrx.24.1554119545492; Mon, 01 Apr 2019 04:52:25 -0700 (PDT) Received: from localhost.localdomain (x4db6660d.dyn.telefonica.de. [77.182.102.13]) by smtp.gmail.com with ESMTPSA id f11sm11250970wrm.30.2019.04.01.04.52.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Apr 2019 04:52:24 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= , Jeff King , Duy Nguyen , Luke Mewburn , git@vger.kernel.org, =?utf-8?q?SZEDER_G?= =?utf-8?q?=C3=A1bor?= Subject: [PATCH v2 3/4] progress: clear previous progress update dynamically Date: Mon, 1 Apr 2019 13:52:16 +0200 Message-Id: <20190401115217.3423-4-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.539.g07239c3a71.dirty In-Reply-To: <20190401115217.3423-1-szeder.dev@gmail.com> References: <20190325103844.26749-1-szeder.dev@gmail.com> <20190401115217.3423-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When the progress bar includes throughput, its length can shorten as the unit of display changes from KiB to MiB. To cover up remnants of the previous progress bar when such a change of units happens we always print three spaces at the end of the progress bar. Alas, covering only three characters is not quite enough: when both the total and the throughput happen to change units from KiB to MiB in the same update, then the progress bar's length is shortened by four characters (or maybe even more!): Receiving objects: 25% (2901/11603), 772.01 KiB | 733.00 KiB/s Receiving objects: 27% (3133/11603), 1.43 MiB | 1.16 MiB/s s and a stray 's' is left behind. So instead of hard-coding the three characters to cover, let's compare the length of the current progress bar with the previous one, and cover up as many characters as needed. Sure, it would be much simpler to just print more spaces at the end of the progress bar, but this approach is more future-proof, and it won't print extra spaces when none are needed, notably when the progress bar doesn't show throughput and thus never shrinks. Signed-off-by: SZEDER Gábor --- progress.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/progress.c b/progress.c index 842db14b72..3149ecd460 100644 --- a/progress.c +++ b/progress.c @@ -84,6 +84,7 @@ static void display(struct progress *progress, uint64_t n, const char *done) const char *tp; struct strbuf *counters_sb = &progress->counters_sb; int show_update = 0; + int last_count_len = counters_sb->len; if (progress->delay && (!progress_update || --progress->delay)) return; @@ -110,10 +111,11 @@ static void display(struct progress *progress, uint64_t n, const char *done) if (show_update) { if (is_foreground_fd(fileno(stderr)) || done) { - const char *eol = done ? done : " \r"; - - fprintf(stderr, "%s: %s%s", progress->title, - counters_sb->buf, eol); + const char *eol = done ? done : "\r"; + int clear_len = counters_sb->len < last_count_len ? + last_count_len - counters_sb->len : 0; + fprintf(stderr, "%s: %s%-*s", progress->title, + counters_sb->buf, clear_len, eol); fflush(stderr); } progress_update = 0; From patchwork Mon Apr 1 11:52:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 10879765 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D715F14DE for ; Mon, 1 Apr 2019 11:52:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C294F286DE for ; Mon, 1 Apr 2019 11:52:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B730F286F6; Mon, 1 Apr 2019 11:52:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 42EA1286DE for ; Mon, 1 Apr 2019 11:52:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726843AbfDALw3 (ORCPT ); Mon, 1 Apr 2019 07:52:29 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43115 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725882AbfDALw2 (ORCPT ); Mon, 1 Apr 2019 07:52:28 -0400 Received: by mail-wr1-f67.google.com with SMTP id k17so11594802wrx.10 for ; Mon, 01 Apr 2019 04:52:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Pgw0q6Wuvjq06+I2emdoNnWxe7ebvcI62LtKRQ0d+R0=; b=guPyy8MpNsrpVFws++3Gu/Yda1j/qMcDe+jpm7Nexo+7gMlIffmDNu3mprO3vSmh12 BSrH3ZxebKbG6kz62avzUI0KmDPk8Nfg2MqbBBREjg5+SZtcHejHEZYEdxLIL1MHu7Ij z95mS3VfVwafXe46pRorA8EC7rQ+5YuBPSD/62R9dnePdqj569ki4ji1expOnXdyinIZ 98DrUrXWmtiXz8C4VtwqmXeJ7Gylbah7Pq/UHiDSQCpbyErzxryNOzoROiONl5sCRmYk 3p34MIIgQmzFBPCqEPRBWhPNoaCCghcNkSaO2BDGOdv0cYY97m8SaBEageCoiM62BhX6 PR5w== 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:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Pgw0q6Wuvjq06+I2emdoNnWxe7ebvcI62LtKRQ0d+R0=; b=KghydP6Rbgziw+FrZ8sw3q1Fwv9XICrp5Lm2ZBvHXj/avVGcN86GHCaBU6SijMOM+N PR+TSR3iA0g2Z33RDVDFXHDFfeFsfSfMfrb3OFfCwUJ2Xa363PlAW3HYiuUnGlh69lxs pRTr6o6aj02Red8PhkE0e+DcKYEp/wOUbHrfS0C0WbOfUQ3xlHNMSrEWFoAbgfyrjQkt MvixmKU+b+TsL0RPqRie6krEl4bMn6tCEaHHPvEnCPuJkZUgTIWiULCahx34zXXaWeL3 rZbif327x4+h3Fu4YpbbcDZCbiH7DmX+t28ymrUm8p8gopEc3v8oneNJvsxBkuVLQ7sI AE3w== X-Gm-Message-State: APjAAAUZlXeYTk6ShjuW8hDQ8RnlNUcQQTjcm33skyhOhgVKWSDGu13A GJylV1zqzg2NsFh2feAabdQ= X-Google-Smtp-Source: APXvYqxytAn3zAN5A0+NXK2en0ea9QPGh5Halx8WLQsTvEkZfJYE291+MmovcC7X5Kbuun6W2Rs01w== X-Received: by 2002:a5d:6b04:: with SMTP id v4mr2337637wrw.69.1554119546556; Mon, 01 Apr 2019 04:52:26 -0700 (PDT) Received: from localhost.localdomain (x4db6660d.dyn.telefonica.de. [77.182.102.13]) by smtp.gmail.com with ESMTPSA id f11sm11250970wrm.30.2019.04.01.04.52.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Apr 2019 04:52:25 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= , Jeff King , Duy Nguyen , Luke Mewburn , git@vger.kernel.org, =?utf-8?q?SZEDER_G?= =?utf-8?q?=C3=A1bor?= Subject: [PATCH v2 4/4] progress: break too long progress bar lines Date: Mon, 1 Apr 2019 13:52:17 +0200 Message-Id: <20190401115217.3423-5-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.539.g07239c3a71.dirty In-Reply-To: <20190401115217.3423-1-szeder.dev@gmail.com> References: <20190325103844.26749-1-szeder.dev@gmail.com> <20190401115217.3423-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some of the recently added progress indicators have quite long titles, which might be even longer when translated to some languages, and when they are shown while operating on bigger repositories, then the progress bar grows longer than the default 80 column terminal width. When the progress bar exceeds the width of the terminal it gets line-wrapped, and after that the CR at the end doesn't return to the beginning of the progress bar, but to the first column of its last line. Consequently, the first line of the previously shown progress bar is not overwritten by the next, and we end up with a bunch of truncated progress bar lines scrolling past: $ LANG=es_ES.UTF-8 git commit-graph write Encontrando commits para commit graph entre los objetos empaquetados: 2% (1599 Encontrando commits para commit graph entre los objetos empaquetados: 3% (1975 Encontrando commits para commit graph entre los objetos empaquetados: 4% (2633 Encontrando commits para commit graph entre los objetos empaquetados: 5% (3292 [...] Prevent this by breaking progress bars after the title once they exceed the width of the terminal, so the counter and optional percentage and throughput, i.e. all changing parts, are on the last line. Subsequent updates will from then on only refresh the changing parts, but not the title, and it will look like this: $ LANG=es_ES.UTF-8 ~/src/git/git commit-graph write Encontrando commits para commit graph entre los objetos empaquetados: 100% (6584502/6584502), listo. Calculando números de generación de commit graph: 100% (824705/824705), listo. Escribiendo commit graph en 4 pasos: 100% (3298820/3298820), listo. Note that the number of columns in the terminal is cached by term_columns(), so this might not kick in when it should when a terminal window is resized while the operation is running. Furthermore, this change won't help if the terminal is so narrow that the counters don't fit on one line, but I would put this in the "If it hurts, don't do it" box. Signed-off-by: SZEDER Gábor --- progress.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/progress.c b/progress.c index 3149ecd460..e28ccdafd2 100644 --- a/progress.c +++ b/progress.c @@ -8,11 +8,12 @@ * published by the Free Software Foundation. */ -#include "git-compat-util.h" +#include "cache.h" #include "gettext.h" #include "progress.h" #include "strbuf.h" #include "trace.h" +#include "utf8.h" #define TP_IDX_MAX 8 @@ -37,6 +38,8 @@ struct progress { struct throughput *throughput; uint64_t start_ns; struct strbuf counters_sb; + int title_len; + int split; }; static volatile sig_atomic_t progress_update; @@ -114,8 +117,24 @@ static void display(struct progress *progress, uint64_t n, const char *done) const char *eol = done ? done : "\r"; int clear_len = counters_sb->len < last_count_len ? last_count_len - counters_sb->len : 0; - fprintf(stderr, "%s: %s%-*s", progress->title, - counters_sb->buf, clear_len, eol); + int progress_line_len = progress->title_len + + counters_sb->len + 2; + int cols = term_columns(); + + if (progress->split) { + fprintf(stderr, " %s%-*s", counters_sb->buf, + clear_len, eol); + } else if (!done && cols < progress_line_len) { + clear_len = progress->title_len + 1 < cols ? + cols - progress->title_len - 1 : 0; + fprintf(stderr, "%s:%*s\n %s%s", + progress->title, clear_len, "", + counters_sb->buf, eol); + progress->split = 1; + } else { + fprintf(stderr, "%s: %s%-*s", progress->title, + counters_sb->buf, clear_len, eol); + } fflush(stderr); } progress_update = 0; @@ -221,6 +240,8 @@ static struct progress *start_progress_delay(const char *title, uint64_t total, progress->throughput = NULL; progress->start_ns = getnanotime(); strbuf_init(&progress->counters_sb, 0); + progress->title_len = utf8_strwidth(title); + progress->split = 0; set_progress_signal(); return progress; }