From patchwork Fri Apr 12 19:45:12 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: 10899069 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 0EDF3139A for ; Fri, 12 Apr 2019 19:45:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E636E28E99 for ; Fri, 12 Apr 2019 19:45:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DA64E28E9B; Fri, 12 Apr 2019 19:45:26 +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 7404C28E9F for ; Fri, 12 Apr 2019 19:45:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726886AbfDLTpZ (ORCPT ); Fri, 12 Apr 2019 15:45:25 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:37931 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726833AbfDLTpY (ORCPT ); Fri, 12 Apr 2019 15:45:24 -0400 Received: by mail-wr1-f65.google.com with SMTP id k11so13395921wro.5 for ; Fri, 12 Apr 2019 12:45: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=JQdtYMQw2ijGNwYIodyEzRfbTyeUhP+cu9lR9r/otxM=; b=YPysQ39woNpIQH9XytJAcdVQNycQva3tH7OD4ZmY1vjJUPY4HP90Qeoozon3K5zrq3 Kbw3k7DuRzhUKJjVWl5McbXYOwXuEDAsbR27REUzuXfa6LFuLkV2Jduy+I6k7xvzttQS wbTexBeM9rFNooNT3oyRAiomu9orOQ8m2Zfg2ddWSM4ZXLSsRYAnZC4RZVuA92b9CBV1 msp+sIQn0SwULa/f8n7DRUN7WFU0dh00hRDxH+u0hLDL3ULlS8rt9X/zuVoKQxNP84FZ thSg8Eu5LG1QAg37IldxjzNcN9D+JNGw+YsJWSIknMvBvlDtOBwYQNPhKpJ4Q2gQV7tS Qggg== 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=JQdtYMQw2ijGNwYIodyEzRfbTyeUhP+cu9lR9r/otxM=; b=n+/KRDFb6E7QasoowGDkB0/7F+cmW0bW+qCKjscFZDpHmBH9x/1CpdYpdB8Ym15UBr /mnOeM+pGW2kNYdOZUt2HOoFdKqrpoWt4mEP6x5nJmZ5AnV8UHrgK79hgjBzp3496TED KyRHRFS7zZkA86R2fObaGgieDIF+IxRMSk7mF4AzTsyrYCEOnz+vY4KiS+udgIbe8M/U 8ugbcpO40MdujCFv2R2QjQ/oJ54tLLG1wNsusfaUiYqWGJi3f4CGhXuNOxXEcFiCd2Mb T+lT+JcuIyXHsRb7ma4ju7bYCRBWAE3C6hY4BeYH72vFrblijqZW9/C2FYr7MGPrF6lf 53rA== X-Gm-Message-State: APjAAAXkiyjnvkmq6PA3BeeSn4UKoIvrXIJYBlfSWeV5eIsJlijQyTyx P3hebseMokk1EmzK9SlYT40= X-Google-Smtp-Source: APXvYqxSSOnLi/DzRI0ZafaL3XJmzeOjLu3zU7UCFkuVhMRbDZgv+/YtpR8tjydZhLnUBmfuxytI1g== X-Received: by 2002:adf:8bc5:: with SMTP id w5mr34913597wra.226.1555098322927; Fri, 12 Apr 2019 12:45:22 -0700 (PDT) Received: from localhost.localdomain (x4dbe3614.dyn.telefonica.de. [77.190.54.20]) by smtp.gmail.com with ESMTPSA id x5sm33503081wru.12.2019.04.12.12.45.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Apr 2019 12:45: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 v4 1/4] progress: make display_progress() return void Date: Fri, 12 Apr 2019 21:45:12 +0200 Message-Id: <20190412194515.6244-2-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.746.gd74f1657d3 In-Reply-To: <20190412194515.6244-1-szeder.dev@gmail.com> References: <20190405004539.31467-1-szeder.dev@gmail.com> <20190412194515.6244-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. While doing so several return statements in display() become unnecessary, remove them. Signed-off-by: SZEDER Gábor --- progress.c | 13 +++++-------- progress.h | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/progress.c b/progress.c index 5a99c9fbf0..9010032446 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,6 @@ static int display(struct progress *progress, uint64_t n, const char *done) fflush(stderr); } progress_update = 0; - return 1; } } else if (progress_update) { if (is_foreground_fd(fileno(stderr)) || done) { @@ -109,10 +108,7 @@ static int display(struct progress *progress, uint64_t n, const char *done) fflush(stderr); } progress_update = 0; - return 1; } - - return 0; } static void throughput_string(struct strbuf *buf, uint64_t total, @@ -188,9 +184,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 Fri Apr 12 19:45:13 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: 10899071 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 423481800 for ; Fri, 12 Apr 2019 19:45:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2565F28E99 for ; Fri, 12 Apr 2019 19:45:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1979E28E9D; Fri, 12 Apr 2019 19:45:29 +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 A9B2C28E99 for ; Fri, 12 Apr 2019 19:45:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726907AbfDLTp1 (ORCPT ); Fri, 12 Apr 2019 15:45:27 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:41198 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726829AbfDLTpZ (ORCPT ); Fri, 12 Apr 2019 15:45:25 -0400 Received: by mail-wr1-f66.google.com with SMTP id r4so13331668wrq.8 for ; Fri, 12 Apr 2019 12:45:24 -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=qtGi14H31MVdAWUDvYP8E9KP5n7HnfreKENwnOcmBxw=; b=QBjDJGx9GFmTGLkKhbSd56HDoshPb8bWRtiT0xpX5WpYEKUEAG+2qMr1MJrqGi8PQg yfE6xS3Qi++JjmZKblVaT9OnltcPNxk5w3Y2KDhVzH99/ciZHUOZ30uuC76hmUo1QIiP Pc+2XSdjv5vxY8qGRqrCAYRK5KC+PfCRjNzUd58TPWe+CeRgejOn0GJFicAonzfeImWZ Fo1KsKEnF17I+KGO+hqNSjIDli42TvS6V4LoksTq0QvjavFnBnvmsPICMhnJrS5xqqC7 QbXK2YEcrinpmEDmMdPtdeCZ8ACemu+/5AEjfnf+CuHbcAHEkdTHUrT236KzOrr4PqBK 17mQ== 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=qtGi14H31MVdAWUDvYP8E9KP5n7HnfreKENwnOcmBxw=; b=PpvJthEO11gw6Ph8UJhAPSPJmSd+1aM8R/kTU7IRo5MRlCgfl9PQHUtAWC2OFlAtLG lypIk31Xqm0HZio9WHoxACt0Xku8oEbiaKzlapMFkayC/ZxORHGodZ6uCHmZiCxcdrI6 KujtsCCrSVLd7kdRp8RIrm8fUQXd5KtN2oucw0c2DVa3yqwYPadV99zLLM0dL1PgUTXH 83Db8gIRM6IdSdMyU+6HtUKu8GDhEvdYE9ToB2wuxJt6zasOx2mnFjt4TWp0PGkd7y7m 0dLI3LXUpevTIIdeDvjINQ3CRRxtraX4KimAPzasb0jj/OKtsHL9E6VQ6iK0STWJZUXB mqeg== X-Gm-Message-State: APjAAAWQANjEM8j6yjrgfFbBuTM0aeTi5hLwthQKmrNfIZJB62WZ0jVg o7i8zWVI83wz36MHBtLk6uk= X-Google-Smtp-Source: APXvYqyroW3C9DQf5ES8iE2oEshMtl2g5srAHsSjORM/7HytVTcxF2H+G01GUf7PVXuDtZFqoIWJFg== X-Received: by 2002:adf:dd82:: with SMTP id x2mr26957557wrl.214.1555098324032; Fri, 12 Apr 2019 12:45:24 -0700 (PDT) Received: from localhost.localdomain (x4dbe3614.dyn.telefonica.de. [77.190.54.20]) by smtp.gmail.com with ESMTPSA id x5sm33503081wru.12.2019.04.12.12.45.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Apr 2019 12:45: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 v4 2/4] progress: assemble percentage and counters in a strbuf before printing Date: Fri, 12 Apr 2019 21:45:13 +0200 Message-Id: <20190412194515.6244-3-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.746.gd74f1657d3 In-Reply-To: <20190412194515.6244-1-szeder.dev@gmail.com> References: <20190405004539.31467-1-szeder.dev@gmail.com> <20190412194515.6244-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 | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/progress.c b/progress.c index 9010032446..564845a36b 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,31 +81,39 @@ 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; + + 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; @@ -207,6 +216,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; } @@ -250,6 +260,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 Fri Apr 12 19:45: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: 10899073 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 D9FA9139A for ; Fri, 12 Apr 2019 19:45:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFA5328E99 for ; Fri, 12 Apr 2019 19:45:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B3C2228E9D; Fri, 12 Apr 2019 19:45: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 583C928E99 for ; Fri, 12 Apr 2019 19:45:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727015AbfDLTp2 (ORCPT ); Fri, 12 Apr 2019 15:45:28 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43642 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726833AbfDLTp1 (ORCPT ); Fri, 12 Apr 2019 15:45:27 -0400 Received: by mail-wr1-f67.google.com with SMTP id k17so8704313wrx.10 for ; Fri, 12 Apr 2019 12:45: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=ErmQJiREhoV0DLwYLbn+aMtYAjsEEmpykhC3s4WEebU=; b=r8Hk8AtndilkNi32e5MlyvhPClVwYvLctZhBloqtk14fM7DPCm+qg8b40xr6HF9yug a5dyOpP4frGGwYAnswpkLyzbVxquPDTdXR1LX733z6cBbrOTbq4tSUSQE637W8b3crpb C/OX5tRHwpcnafvhEDJRmmLcc2CFzYRH1oR3I5oGbJU3nA1vAsAHwg4mzqPsKuX7rtLA +WjA9us8sD1xBW5Qa23Tq5YgF4QX/WiW86uiFzLNZ7O+Uw9eHy5Cm3nSA1HImaJLeUpF CzPVCZYKcDGts085yHwbGI2nYhIBZyjZk99DnyuMNeCV1Bf69KK66WFTH4RwldxTBHKW o6/w== 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=ErmQJiREhoV0DLwYLbn+aMtYAjsEEmpykhC3s4WEebU=; b=qwKmo9kP0c+bpnLcybupdvG6200y6G/DyJkPPcX0sakuW3J1yi/7ToLoLNub2TCgDE j2sXJVUQFsxqJR2pYk1E21AIBDGAMyR6sBQNMwrMMDJ0OCKX1uboMzHFNuQtXgvNIIDv xjIQVLX+W1mrmxGQvh69ur+JTHLodSCQIzjHXYmkKuNRy54mdVt50kujq0SPc960qin0 7R1J2HPHN+3y5itDEVB4igwhgrVWjj7+ewDAbJotNs2OY2ArZ6j5OgCbm+/NXo4nz2Yq paZHFqev/87cdTY5dA01+wZ/xlpiYVHOVzCJF6O54+wWBKYB5/on/lBk6dhVgjlHnnMy I2vg== X-Gm-Message-State: APjAAAUXKmC5Y2wxUhIGZbHbIExaxBVBdlvxUjgTvYyQaaUQcbSiaHWJ ubDl7Cjb8NXQtZxMP+16jhc= X-Google-Smtp-Source: APXvYqwz4NwAtKfWWLdlHh4pRPGA+PWLBSLyVq3sVaaBIoFONm403HpTTknouTJoxwa1noP2P03YPg== X-Received: by 2002:adf:ea45:: with SMTP id j5mr28794207wrn.89.1555098325134; Fri, 12 Apr 2019 12:45:25 -0700 (PDT) Received: from localhost.localdomain (x4dbe3614.dyn.telefonica.de. [77.190.54.20]) by smtp.gmail.com with ESMTPSA id x5sm33503081wru.12.2019.04.12.12.45.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Apr 2019 12:45: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 v4 3/4] progress: clear previous progress update dynamically Date: Fri, 12 Apr 2019 21:45:14 +0200 Message-Id: <20190412194515.6244-4-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.746.gd74f1657d3 In-Reply-To: <20190412194515.6244-1-szeder.dev@gmail.com> References: <20190405004539.31467-1-szeder.dev@gmail.com> <20190412194515.6244-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 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/progress.c b/progress.c index 564845a36b..949a2a576d 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,12 @@ 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"; + size_t clear_len = counters_sb->len < last_count_len ? + last_count_len - counters_sb->len + 1 : + 0; + fprintf(stderr, "%s: %s%*s", progress->title, + counters_sb->buf, (int) clear_len, eol); fflush(stderr); } progress_update = 0; From patchwork Fri Apr 12 19:45: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: 10899075 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 343CB139A for ; Fri, 12 Apr 2019 19:45:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AD4928E99 for ; Fri, 12 Apr 2019 19:45:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E08D28E9D; Fri, 12 Apr 2019 19:45: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 9039128E99 for ; Fri, 12 Apr 2019 19:45:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727020AbfDLTpb (ORCPT ); Fri, 12 Apr 2019 15:45:31 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:35585 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726890AbfDLTp2 (ORCPT ); Fri, 12 Apr 2019 15:45:28 -0400 Received: by mail-wm1-f68.google.com with SMTP id y197so518252wmd.0 for ; Fri, 12 Apr 2019 12:45: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=2zrRzh0ukW6xdQB0tx7LizrEPsvtXEb1ciGM3vAoos0=; b=ZGUIsAeHXyTlbAIKAHNScsyKW+g7I/h1X0OLLf/QP9hwAaEYlvePDrU1ek7mFQ4RZR 0pjdceTEySb6yrLtGuWCEWY5yhA+htrZmev1H+Z4lRBmk31kO5wO6HR1ioGnS1r81cIj IYxU3gL2E+KIqsYaEp1v6ornGOyzSbRrZ9+PtmGfi3EauxTL8xzQEHx+Cwb+CzvRvsO9 c98fbmdKo7q2dRVuuMW7alh850uXtWhQxIThLLVtf7ztoU70e2CwwThj8x9ZbiwWP1Bz DKjVOvySRk9ootqDgz7cMHJwDnqQvOxdMilQracxujgbMySFXQ5be/nZgMzawVEp4gYT enpw== 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=2zrRzh0ukW6xdQB0tx7LizrEPsvtXEb1ciGM3vAoos0=; b=KDiNOOsURVNdrCKGc2j3/SW8XczCqmJ33h1spWd1fn0zaSwoELxgS5VrLwIdBGgc96 OJLbUfHp/OD7F1JsVx9KjFMi0mVOltskyeYxsQEiGuWkgVQlePn6+sPtNhTVmCUuL5Wg V9OwzPpv3YPIvZ+lXLCE8aGB67mhzdHbvOSuAeY9KaVIUAU6SAh0cUQrV5MbRhDGMdSO 2TnP4Cr4B/zLi54N8mbb81im95qnLxytXvmppG13/axFktTtWcJJl1zY+k6436d6fawf ZYWYNYICufgHFu5h6IJCEeu4x6xLcvNEWKI7u9s9DUzaXCGsGte6Ny8E4uwLlGfotr3r 09wg== X-Gm-Message-State: APjAAAW9C0Ksb/ZR4CGQXqcv3AlHTPmlKe8sTMR+2xq5qKSP4GOqbp/2 /2bxk71wkGRt4G0Uk4rEK7Bxjkmh X-Google-Smtp-Source: APXvYqw7cWtoDO7H6++YpcaduCdyuTjbnsopMjOFvwo8Ll9LbKp7BsTwT2NIE7TocAVHXvaf2irR3A== X-Received: by 2002:a1c:ca06:: with SMTP id a6mr12820862wmg.14.1555098326200; Fri, 12 Apr 2019 12:45:26 -0700 (PDT) Received: from localhost.localdomain (x4dbe3614.dyn.telefonica.de. [77.190.54.20]) by smtp.gmail.com with ESMTPSA id x5sm33503081wru.12.2019.04.12.12.45.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 12 Apr 2019 12:45: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 v4 4/4] progress: break too long progress bar lines Date: Fri, 12 Apr 2019 21:45:15 +0200 Message-Id: <20190412194515.6244-5-szeder.dev@gmail.com> X-Mailer: git-send-email 2.21.0.746.gd74f1657d3 In-Reply-To: <20190412194515.6244-1-szeder.dev@gmail.com> References: <20190405004539.31467-1-szeder.dev@gmail.com> <20190412194515.6244-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 949a2a576d..2d8022a622 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; @@ -115,8 +118,24 @@ static void display(struct progress *progress, uint64_t n, const char *done) size_t clear_len = counters_sb->len < last_count_len ? last_count_len - counters_sb->len + 1 : 0; - fprintf(stderr, "%s: %s%*s", progress->title, - counters_sb->buf, (int) clear_len, eol); + size_t progress_line_len = progress->title_len + + counters_sb->len + 2; + int cols = term_columns(); + + if (progress->split) { + fprintf(stderr, " %s%*s", counters_sb->buf, + (int) clear_len, eol); + } else if (!done && cols < progress_line_len) { + clear_len = progress->title_len + 1 < cols ? + cols - progress->title_len : 0; + fprintf(stderr, "%s:%*s\n %s%s", + progress->title, (int) clear_len, "", + counters_sb->buf, eol); + progress->split = 1; + } else { + fprintf(stderr, "%s: %s%*s", progress->title, + counters_sb->buf, (int) clear_len, eol); + } fflush(stderr); } progress_update = 0; @@ -220,6 +239,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; }