1 # This library is free software; you can redistribute it and/or
2 # modify it under the terms of the GNU Lesser General Public
3 # License as published by the Free Software Foundation; either
4 # version 2.1 of the License, or (at your option) any later version.
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the
13 # Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330,
15 # Boston, MA 02111-1307 USA
17 # this is basically a copy of python-urlgrabber's TextMeter class,
18 # with support added for dynamical sizing according to screen size.
19 # it uses getScreenWidth() scrapped from smart.
23 from urlgrabber.progress import BaseMeter, format_time, format_number
27 import termios, struct, fcntl
28 s = struct.pack('HHHH', 0, 0, 0, 0)
30 x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
33 return struct.unpack('HHHH', x)[1]
36 class TextMeter(BaseMeter):
37 def __init__(self, fo=sys.stderr, hide_finished=False):
38 BaseMeter.__init__(self)
40 self.hide_finished = hide_finished
42 width = int(os.environ['COLUMNS'])
43 except (KeyError, ValueError):
44 width = getScreenWidth()
47 #self.unsized_templ = '\r%-60.60s %5sB %s '
48 self.unsized_templ = '\r%%-%s.%ss %%5sB %%s ' % (width *4/3, width*3/5)
49 #self.sized_templ = '\r%-45.45s %3i%% |%-15.15s| %5sB %8s '
50 self.bar_length = width/5
51 self.sized_templ = '\r%%-%s.%ss %%3i%%%% |%%-%s.%ss| %%5sB %%8s ' %(width*4/10, width*4/10, self.bar_length, self.bar_length)
54 def _do_start(self, *args, **kwargs):
55 BaseMeter._do_start(self, *args, **kwargs)
58 def _do_update(self, amount_read, now=None):
59 etime = self.re.elapsed_time()
60 fetime = format_time(etime)
61 fread = format_number(amount_read)
63 if self.text is not None:
68 out = self.unsized_templ % \
71 rtime = self.re.remaining_time()
72 frtime = format_time(rtime)
73 frac = self.re.fraction_read()
74 bar = '='*int(self.bar_length * frac)
76 out = self.sized_templ % \
77 (text, frac*100, bar, fread, frtime) + 'ETA '
82 def _do_end(self, amount_read, now=None):
83 total_time = format_time(self.re.elapsed_time())
84 total_size = format_number(amount_read)
85 if self.text is not None:
90 out = self.unsized_templ % \
91 (text, total_size, total_time)
93 bar = '=' * self.bar_length
94 out = self.sized_templ % \
95 (text, 100, bar, total_size, total_time) + ' '
96 if self.hide_finished:
97 self.fo.write('\r'+ ' '*len(out) + '\r')
99 self.fo.write(out + '\n')