]> CyberLeo.Net >> Repos - CDN/shlib.git/blob - lib/sh/progress.sh
sh/stopwatch: Add 'time' verb to stopwatch, to retrieve just the times
[CDN/shlib.git] / lib / sh / progress.sh
1 # Progress library script
2 : <<"END_OF_COPYRIGHT"
3
4 Copyright (c) 2000-2012, CyberLeo
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10     * Redistributions of the source code must retain the above copyright
11       notice, this list of conditions, and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions, and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15     * Neither the name of the organization nor the names of its contributors
16       may be used to endorse or promote products derived from this software
17       without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILTY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 END_OF_COPYRIGHT
31
32 if [ -z "${__progress_sh_loaded}" ]
33 then
34   __progress_sh_loaded=yes
35   
36   # Implement a simple progress indicator
37   # ( i:current i:total i:width -- s:
38   progress() {
39     local time_last
40
41     cur="${1}"
42     all="${2}"
43     width="${3:-78}"
44
45     time="$(date +%s)"
46
47     prcnt="$(printf "%u / %u * 100\n" "${cur}" "${all}" | bc -l 2>/dev/null)"
48     elapsed="$(printf "%u - %u\n" "${time}" "${time_start}" | bc -l 2>/dev/null)"
49     remain="$(printf "( %u - %u ) / ( %u / %u )\n" "${all}" "${cur}" "${cur}" "${elapsed}" | bc -l 2>/dev/null)"
50
51     time_last="${time}"
52
53     printf "%0.2f%% E:%us R:%0.1fs" "${prcnt:-0}" "${elapsed:-0}" "${remain:-0}"
54   }
55 fi