From db96f9717ac132a6102c281bda32bd3dbb6cd12e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 16 May 2009 18:38:03 +0100 Subject: [PATCH] abstract away the progress printing --- yoweb-scrape | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/yoweb-scrape b/yoweb-scrape index 2790ac7..5b2fe98 100755 --- a/yoweb-scrape +++ b/yoweb-scrape @@ -608,16 +608,25 @@ class ChatLogTracker: def catchup(self, progress=None): while True: more = self._f.readline() - if not more: return + if not more: break self._progress[0] += len(more) - if progress is not None: - progress(*self._progress) + if progress: progress.progress(*self._progress) self._lbuf += more if self._lbuf.endswith('\n'): self.chatline(self._lbuf.rstrip()) self._lbuf = '' + if progress: progress.caughtup() + +class ProgressPrintPercentage: + def __init__(self, f): self._f = f + def progress(self,done,total): + self._f.write("scan chat logs %3d%%\r" % ((done*100) / total)) + self._f.flush() + def caughtup(self): + self._f.write(' \r') + self._f.flush() def do_track_chat_log(args, bu): if len(args) != 1: bu('ship-aid takes only chat log filename') @@ -630,15 +639,7 @@ def do_track_chat_log(args, bu): track = ChatLogTracker(pirate, logfn) myself_pi = PirateInfo(pirate,3600) track = ChatLogTracker(myself_pi, logfn) - - def pprogress(done,total): - sys.stdout.write("scan chat logs %3d%%\r" % - ((done*100) / total)) - sys.stdout.flush() - - track.catchup(pprogress) - sys.stdout.write(' \r') - + track.catchup(ProgressPrintPercentage(sys.stdout)) while True: track.catchup() if track.need_redisplay: -- 2.30.2