Hello! This is mainly a curiousity question, I may apply it to something in the near future though just as an added twist... :D

So essentially I want to figure out how a progress indicator is created in the shell.
For example with wget or something you may see a progress bar like this:

[======================>     ] 96%

or something like that.
I know how to acheive the bar itself by using escape characters such as \b (backspace, overwrite characters), \r (carriage return, go to beginning of line and update characters) and such.

What I want to figure out is how to make it not some fake thing, how is it accurate like that?

Recently I finished writing a libarchive binding to D (that was a pain xD) and am working on OO D API/wrapper around the C bindings. Soon I'm going to create a mini archiver/compressor tool just as an example of using my binding, I think it would be cool to make it look a bit nicer by using a progress bar/indicator in the shell. How would I create a semi-accurate progress indicator/bar with compressing files?

Hopefully that example makes sense. Thanks for any help, ideas, or suggestions!
Thanks.

What do you mean Ancient? I wasn't asking about the looks I was asking about an accurate/working progress indicator.

To make a working progress bar you have to split up the work into small units so that the screen can be updated periodically. Depending on what work is being done that may or may not be possible. For example if writing or copying a very large file once the operarating system is called to do the work the os has complete control over the program and it will be impossible to update the progress bar. I know of a couple MS-Windows api functions which accept a pointer to a callback function for that purpose. Don't know about *nix api. Even then, it's on one or two api functions out of thousands that are like that.

oic...
Thanks, I will definitely look through the C API again. I can recall translating C function pointers to D function pointers while working on the bindingso there may be some in there.
Thanks again.

The C API don't help you, if by C api you mean standard C language. You need to study operating system specific API such as win32 api for MS-Windows.

I meant the library I'm working with. Could have callbacks at checkpoints or something.

Conceptually, accuracy is mapping the amount complete of a whole to some textual representation given your screen dimensions. For things that dont have an end pre se (such as a download of undetermined size) you may want to just have an animation that goes back and forth on the screen repeatedly until the task is complete.

If you are interested in a Linux solution I have created something like this that does simple text, color, or a GUI when available. You can find it on github. This version only handles fixed-size representations, however. You'd have to extend to include the arbitrary time progress bar.

This article has been dead for over six months. Start a new discussion instead.