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					 */
127237285Sscottl	static const char	stars[] =
128237285Sscottl"*****************************************************************************"
129237285Sscottl"*****************************************************************************"
130237285Sscottl"*****************************************************************************";
131237285Sscottl	unsigned		bytesabbrev;
132237285Sscottl	unsigned		bpsabbrev;
133237285Sscottl	int64_t			secs;
134237285Sscottl	uint64_t		bytespersec;
135237285Sscottl	uint64_t		abbrevsize;
136237285Sscottl	int64_t			secsleft;
137237285Sscottl	size_t			barlength;
138237285Sscottl	size_t			starc;
139237285Sscottl	char			hours[12];
140237285Sscottl	char			buf[256];
141237285Sscottl	int			len;
142237285Sscottl
143237285Sscottl	barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) - BAROVERHEAD - strlen(prog->prefix);
144237285Sscottl	starc = (barlength * prog->percent) / 100;
145237285Sscottl	abbrevsize = prog->done;
146237285Sscottl	for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) {
147237285Sscottl		abbrevsize >>= 10;
148237285Sscottl	}
149237285Sscottl	if (bytesabbrev == NSUFFIXES) {
150237285Sscottl		bytesabbrev--;
151237285Sscottl	}
152237285Sscottl	bytespersec = 0;
153237285Sscottl	if (prog->done > 0) {
154237285Sscottl		bytespersec = prog->done;
155237285Sscottl		if (prog->elapsed > 0) {
156237285Sscottl			bytespersec /= prog->elapsed;
157237285Sscottl		}
158237285Sscottl	}
159237285Sscottl	for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) {
160237285Sscottl		bytespersec >>= 10;
161237285Sscottl	}
162237285Sscottl	if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) {
163237285Sscottl		secsleft = 0;
164237285Sscottl	} else {
165237285Sscottl		secsleft = prog->eta;
166237285Sscottl	}
167237285Sscottl	if ((secs = secsleft / SECSPERHOUR) > 0) {
168237285Sscottl		(void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs);
169237285Sscottl	} else {
170237285Sscottl		(void) snprintf(hours, sizeof(hours), "   ");
171237285Sscottl	}
172237285Sscottl	secs = secsleft % SECSPERHOUR;
173237285Sscottl	len = snprintf(buf, sizeof(buf),
174237285Sscottl		"\r%s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA",
175237285Sscottl		(prog->prefix) ? prog->prefix : "",
176237285Sscottl		(long long)prog->percent,
177237285Sscottl		(int)starc, stars, (int)(barlength - starc), "",
178237285Sscottl		(long long)abbrevsize,
179237285Sscottl		suffixes[bytesabbrev],
180237285Sscottl		(long long)(bytespersec / 1024),
181237285Sscottl		(int)((bytespersec % 1024) * 100 / 1024),
182237285Sscottl		suffixes[bpsabbrev],
183237285Sscottl		hours,
184237285Sscottl		(int)secs / 60, (int)secs % 60);
185237285Sscottl	return (int)write(STDOUT_FILENO, buf, len);
186237285Sscottl}
187