/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Aaron Bentley
  • Date: 2008-02-24 16:42:13 UTC
  • mfrom: (3234 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3235.
  • Revision ID: aaron@aaronbentley.com-20080224164213-eza1lzru5bwuwmmj
Merge with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
 
19
 
"""Simple text-mode progress indicator.
20
 
 
21
 
To display an indicator, create a ProgressBar object.  Call it,
22
 
passing Progress objects indicating the current state.  When done,
23
 
call clear().
24
 
 
25
 
Progress is suppressed when output is not sent to a terminal, so as
26
 
not to clutter log files.
 
19
"""Progress indicators.
 
20
 
 
21
The usual way to use this is via bzrlib.ui.ui_factory.nested_progress_bar which
 
22
will maintain a ProgressBarStack for you.
 
23
 
 
24
For direct use, the factory ProgressBar will return an auto-detected progress
 
25
bar that should match your terminal type. You can manually create a
 
26
ProgressBarStack too if you need multiple levels of cooperating progress bars.
 
27
Note that bzrlib's internal functions use the ui module, so if you are using
 
28
bzrlib it really is best to use bzrlib.ui.ui_factory.
27
29
"""
28
30
 
29
 
# TODO: should be a global option e.g. --silent that disables progress
30
 
# indicators, preferably without needing to adjust all code that
31
 
# potentially calls them.
32
 
 
33
 
# TODO: If not on a tty perhaps just print '......' for the benefit of IDEs, etc
34
 
 
35
31
# TODO: Optionally show elapsed time instead/as well as ETA; nicer
36
32
# when the rate is unpredictable
37
33
 
298
294
        self.child_fraction = 0
299
295
        self._have_output = False
300
296
    
301
 
 
302
297
    def throttle(self, old_msg):
303
298
        """Return True if the bar was updated too recently"""
304
299
        # time.time consistently takes 40/4000 ms = 0.01 ms.
320
315
        return False
321
316
        
322
317
    def tick(self):
323
 
        self.update(self.last_msg, self.last_cnt, self.last_total, 
 
318
        self.update(self.last_msg, self.last_cnt, self.last_total,
324
319
                    self.child_fraction)
325
320
 
326
321
    def child_update(self, message, current, total):
336
331
            self.last_msg = ''
337
332
        self.tick()
338
333
 
339
 
    def update(self, msg, current_cnt=None, total_cnt=None, 
 
334
    def update(self, msg, current_cnt=None, total_cnt=None,
340
335
               child_fraction=0):
341
336
        """Update and redraw progress bar."""
342
337
        if msg is None:
438
433
        self._have_output = True
439
434
        #self.to_file.flush()
440
435
            
441
 
    def clear(self):        
 
436
    def clear(self):
442
437
        if self._have_output:
443
438
            self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
444
439
        self._have_output = False
491
486
    def note(self, *args, **kwargs):
492
487
        self.parent.note(*args, **kwargs)
493
488
 
494
 
 
 
489
 
 
490
class InstrumentedProgress(TTYProgressBar):
 
491
    """TTYProgress variant that tracks outcomes"""
 
492
 
 
493
    def __init__(self, *args, **kwargs):
 
494
        self.always_throttled = True
 
495
        self.never_throttle = False
 
496
        TTYProgressBar.__init__(self, *args, **kwargs)
 
497
 
 
498
    def throttle(self, old_message):
 
499
        if self.never_throttle:
 
500
            result =  False
 
501
        else:
 
502
            result = TTYProgressBar.throttle(self, old_message)
 
503
        if result is False:
 
504
            self.always_throttled = False
 
505
 
 
506
 
495
507
def str_tdelta(delt):
496
508
    if delt is None:
497
509
        return "-:--:--"
551
563
            self.cur_phase += 1
552
564
        assert self.cur_phase < self.total
553
565
        self.pb.update(self.message, self.cur_phase, self.total)
554
 
 
555
 
 
556
 
def run_tests():
557
 
    import doctest
558
 
    result = doctest.testmod()
559
 
    if result[1] > 0:
560
 
        if result[0] == 0:
561
 
            print "All tests passed"
562
 
    else:
563
 
        print "No tests to run"
564
 
 
565
 
 
566
 
def demo():
567
 
    sleep = time.sleep
568
 
    
569
 
    print 'dumb-terminal test:'
570
 
    pb = DotsProgressBar()
571
 
    for i in range(100):
572
 
        pb.update('Leoparden', i, 99)
573
 
        sleep(0.1)
574
 
    sleep(1.5)
575
 
    pb.clear()
576
 
    sleep(1.5)
577
 
    
578
 
    print 'smart-terminal test:'
579
 
    pb = ProgressBar(show_pct=True, show_bar=True, show_spinner=False)
580
 
    for i in range(100):
581
 
        pb.update('Elephanten', i, 99)
582
 
        sleep(0.1)
583
 
    sleep(2)
584
 
    pb.clear()
585
 
    sleep(1)
586
 
 
587
 
    print 'done!'
588
 
 
589
 
if __name__ == "__main__":
590
 
    demo()