[[!tag ttystatus python programming]]
Oops, I did it again. Even though there exist some already, I made my own.
This time, I wasn't happy with any of the Python libraries I could find for providing progress reporting for command line applications. I admit I didn't look very far, since this was a simple thing to write.
Anyway, http://liw.fi/ttystatus/ has the gory stuff. A mutilated part of the example program, to whet your appetite:
ts = ttystatus.TerminalStatus(period=0.1)
ts.add(ttystatus.Literal('Finding symlinks: '))
ts.add(ttystatus.Counter('symlink'))
ts.add(ttystatus.Literal(' found; now at '))
ts.add(ttystatus.Index('pathname', 'pathnames'))
ts.add(ttystatus.Literal(' ('))
ts.add(ttystatus.PercentDone('done', 'total', decimals=2))
ts.add(ttystatus.Literal(' done) '))
ts.add(ttystatus.RemainingTime('done', 'total'))
ts.add(ttystatus.Literal(' '))
ts.add(ttystatus.ProgressBar('done', 'total'))
ts['pathnames'] = pathnames
ts['done'] = 0
ts['total'] = len(pathnames)
for pathname in pathnames:
ts['pathname'] = pathname
if os.path.islink(pathname):
ts['symlink'] = pathname
ts.notify('Symlink! %s' % pathname)
ts['done'] += 1
ts.finish()
I am going to be using this in obnam, obviously. Actually, obnam already had some code like this, but I wanted to use it in other programs as well, so I extracted it, abstracted it, and make it more easily re-usable elsewhere.