1237285Sscottl/* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */ 2237285Sscottl 3237285Sscottl/*- 4237285Sscottl * Copyright (c) 1997-2009 The NetBSD Foundation, Inc. 5237285Sscottl * All rights reserved. 6237285Sscottl * 7237285Sscottl * This code is derived from software contributed to The NetBSD Foundation 8237285Sscottl * by Luke Mewburn. 9237285Sscottl * 10237285Sscottl * Redistribution and use in source and binary forms, with or without 11237285Sscottl * modification, are permitted provided that the following conditions 12237285Sscottl * are met: 13237285Sscottl * 1. Redistributions of source code must retain the above copyright 14237285Sscottl * notice, this list of conditions and the following disclaimer. 15237285Sscottl * 2. Redistributions in binary form must reproduce the above copyright 16237285Sscottl * notice, this list of conditions and the following disclaimer in the 17237285Sscottl * documentation and/or other materials provided with the distribution. 18237285Sscottl * 19237285Sscottl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20237285Sscottl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21237285Sscottl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22237285Sscottl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23237285Sscottl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24237285Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25237285Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26237285Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27237285Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28237285Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29237285Sscottl * POSSIBILITY OF SUCH DAMAGE. 30237285Sscottl */ 31237285Sscottl 32237285Sscottl#include <sys/types.h> 33237285Sscottl#include <sys/param.h> 34237285Sscottl#include <sys/ioctl.h> 35237285Sscottl 36237285Sscottl#include <errno.h> 37237285Sscottl#include <stdio.h> 38237285Sscottl#include <stdlib.h> 39237285Sscottl#include <string.h> 40237285Sscottl#include <termios.h> 41237285Sscottl#include <time.h> 42237285Sscottl#include <unistd.h> 43237285Sscottl 44237285Sscottl#include <sys/cdefs.h> 45237285Sscottl__FBSDID("$FreeBSD$"); 46237285Sscottl 47237285Sscottl#include "progress.h" 48237285Sscottl 49237285Sscottlstatic const char * const suffixes[] = { 50237285Sscottl "", /* 2^0 (byte) */ 51237285Sscottl "KiB", /* 2^10 Kibibyte */ 52237285Sscottl "MiB", /* 2^20 Mebibyte */ 53237285Sscottl "GiB", /* 2^30 Gibibyte */ 54237285Sscottl "TiB", /* 2^40 Tebibyte */ 55237285Sscottl "PiB", /* 2^50 Pebibyte */ 56237285Sscottl "EiB", /* 2^60 Exbibyte */ 57237285Sscottl}; 58237285Sscottl 59237285Sscottl#define NSUFFIXES (sizeof(suffixes) / sizeof(suffixes[0])) 60237285Sscottl#define SECSPERHOUR (60 * 60) 61237285Sscottl#define DEFAULT_TTYWIDTH 80 62237285Sscottl 63237285Sscottl/* initialise progress meter structure */ 64237285Sscottlint 65237285Sscottlprogress_init(progress_t *prog, const char *prefix, uint64_t total) 66237285Sscottl{ 67237285Sscottl struct winsize winsize; 68237285Sscottl int oerrno = errno; 69237285Sscottl 70237285Sscottl (void) memset(prog, 0x0, sizeof(*prog)); 71237285Sscottl prog->size = total; 72237285Sscottl prog->prefix = strdup(prefix); 73237285Sscottl prog->start = time(NULL); 74237285Sscottl if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && 75237285Sscottl winsize.ws_col != 0) { 76237285Sscottl prog->ttywidth = winsize.ws_col; 77237285Sscottl } else { 78237285Sscottl prog->ttywidth = DEFAULT_TTYWIDTH; 79237285Sscottl } 80237285Sscottl errno = oerrno; 81237285Sscottl return 1; 82237285Sscottl} 83237285Sscottl 84237285Sscottl/* update the values in the progress meter */ 85237285Sscottlint 86237285Sscottlprogress_update(progress_t *prog, uint64_t done) 87237285Sscottl{ 88237285Sscottl prog->done = done; 89237285Sscottl prog->percent = (prog->done * 100) / prog->size; 90237285Sscottl prog->now = time(NULL); 91237285Sscottl prog->elapsed = prog->now - prog->start; 92237285Sscottl if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) { 93237285Sscottl prog->eta = 0; 94237285Sscottl } else { 95237285Sscottl prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed; 96237285Sscottl } 97237285Sscottl return 1; 98237285Sscottl} 99237285Sscottl 100237285Sscottl/* update the values in the progress meter */ 101237285Sscottlint 102237285Sscottlprogress_reset_size(progress_t *prog, uint64_t size) 103237285Sscottl{ 104237285Sscottl prog->size = size; 105237285Sscottl return 1; 106237285Sscottl} 107237285Sscottl 108237285Sscottl/* make it look pretty at the end - display done bytes (usually total) */ 109237285Sscottlint 110237285Sscottlprogress_complete(progress_t *prog, uint64_t done) 111237285Sscottl{ 112237285Sscottl progress_update(prog, done); 113237285Sscottl progress_draw(prog); 114237285Sscottl printf("\n"); 115237285Sscottl return 1; 116237285Sscottl} 117237285Sscottl 118237285Sscottl/* draw the progress meter */ 119237285Sscottlint 120237285Sscottlprogress_draw(progress_t *prog) 121237285Sscottl{ 122237285Sscottl#define BAROVERHEAD 45 /* non `*' portion of progress bar */ 123237285Sscottl /* 124237285Sscottl * stars should contain at least 125237285Sscottl * sizeof(buf) - BAROVERHEAD entries 126237285Sscottl */ 127287203Sken#define MIN_BAR_LEN 10 128237285Sscottl static const char stars[] = 129237285Sscottl"*****************************************************************************" 130237285Sscottl"*****************************************************************************" 131237285Sscottl"*****************************************************************************"; 132237285Sscottl unsigned bytesabbrev; 133237285Sscottl unsigned bpsabbrev; 134237285Sscottl int64_t secs; 135237285Sscottl uint64_t bytespersec; 136237285Sscottl uint64_t abbrevsize; 137237285Sscottl int64_t secsleft; 138287203Sken ssize_t barlength; 139237285Sscottl size_t starc; 140237285Sscottl char hours[12]; 141237285Sscottl char buf[256]; 142237285Sscottl int len; 143287203Sken int prefix_len; 144237285Sscottl 145287203Sken prefix_len = strlen(prog->prefix); 146287203Sken barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) - 147287203Sken BAROVERHEAD - prefix_len; 148287203Sken if (barlength < MIN_BAR_LEN) { 149287203Sken int tmp_prefix_len; 150287203Sken /* 151287203Sken * In this case the TTY width is too small or the prefix is 152287203Sken * too large for a progress bar. We'll try decreasing the 153287203Sken * prefix length. 154287203Sken */ 155287203Sken barlength = MIN_BAR_LEN; 156287203Sken tmp_prefix_len = MIN(sizeof(buf) - 1,(unsigned)prog->ttywidth) - 157287203Sken BAROVERHEAD - MIN_BAR_LEN; 158287203Sken if (tmp_prefix_len > 0) 159287203Sken prefix_len = tmp_prefix_len; 160287203Sken else 161287203Sken prefix_len = 0; 162287203Sken } 163237285Sscottl starc = (barlength * prog->percent) / 100; 164237285Sscottl abbrevsize = prog->done; 165237285Sscottl for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) { 166237285Sscottl abbrevsize >>= 10; 167237285Sscottl } 168237285Sscottl if (bytesabbrev == NSUFFIXES) { 169237285Sscottl bytesabbrev--; 170237285Sscottl } 171237285Sscottl bytespersec = 0; 172237285Sscottl if (prog->done > 0) { 173237285Sscottl bytespersec = prog->done; 174237285Sscottl if (prog->elapsed > 0) { 175237285Sscottl bytespersec /= prog->elapsed; 176237285Sscottl } 177237285Sscottl } 178237285Sscottl for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) { 179237285Sscottl bytespersec >>= 10; 180237285Sscottl } 181237285Sscottl if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) { 182237285Sscottl secsleft = 0; 183237285Sscottl } else { 184237285Sscottl secsleft = prog->eta; 185237285Sscottl } 186237285Sscottl if ((secs = secsleft / SECSPERHOUR) > 0) { 187237285Sscottl (void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs); 188237285Sscottl } else { 189237285Sscottl (void) snprintf(hours, sizeof(hours), " "); 190237285Sscottl } 191237285Sscottl secs = secsleft % SECSPERHOUR; 192237285Sscottl len = snprintf(buf, sizeof(buf), 193287203Sken "\r%.*s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA", 194287203Sken prefix_len, (prog->prefix) ? prog->prefix : "", 195237285Sscottl (long long)prog->percent, 196237285Sscottl (int)starc, stars, (int)(barlength - starc), "", 197237285Sscottl (long long)abbrevsize, 198237285Sscottl suffixes[bytesabbrev], 199237285Sscottl (long long)(bytespersec / 1024), 200237285Sscottl (int)((bytespersec % 1024) * 100 / 1024), 201237285Sscottl suffixes[bpsabbrev], 202237285Sscottl hours, 203237285Sscottl (int)secs / 60, (int)secs % 60); 204237285Sscottl return (int)write(STDOUT_FILENO, buf, len); 205237285Sscottl} 206