a size-intelligent progress bar?
cben at techunix.technion.ac.il
Sun Apr 13 19:31:45 CEST 2003
Michael Stenner wrote on 2003-04-09:
> On Wed, Apr 09, 2003 at 06:20:02PM +0100, Tom Chance wrote:
> > Hullo,
> > I'm making a shell-based program that includes a progress bar (like wget's
> > one). At the moment, I've got it printing an equals sign every 2% so I get
> > a progress bar 50 "="s wide, which fits my Konsole perfectly. This isn't
> > very handy for other shell sizes though :)
> > Does anyone have, or can anyone point me in the direction of a way of making
> > a decent progress bar that will automagically detect the width of the shell
> > open, even resizing it mid-"progress"?
> I don't know how portable it is, but the environment variable COLUMNS
> appears to tell me my terminal width and follows resizes. If you
> check that on each update, it should do what you want.
It's there on any normal unix. It is set up correctly in 95% of the
cases and relying on it is OK (many command-line programs do) - if
it's not the user will eventually get annoyed and fix it by 'eval
`resize`' ;-). However it's *not* going to be updated when the
terminal resizes - it's impossible for an environment variable your
program sees to change after your program has started (unless the
program itself changes it). The best way in a shell script would be
to take the output of `tput cols` every time; if you don't like to
launch a process every time, turn to ioctl/curses...
Beni Cherniavsky <cben at tx.technion.ac.il>
Never spend several weeks on a single homework - or you will miss a
nice Linux conference ;-(.
More information about the Python-list