roelvandijk / terminal-progress-bar

Other
24 stars 9 forks source link

How can I display a progress bar on stderr? #3

Closed peti closed 11 years ago

peti commented 11 years ago

I'd would like to add a progress bar to my program, but it needs the standard output channel to write resuts which are processed in a pipe, i.e. I need the progress bar to be displayed on stderr. Is that possible with your package?

roelvandijk commented 11 years ago

Take a look at the implementation of progressBar:

putChar '\r'
putStr $ mkProgressBar mkPreLabel mkPostLabel width todo done

If you replace putChar and putStr with hPutChar and hPutStr you can easily write a progress bar to any file handle, such as stderr.

I'll see if I can add this functionality to the terminal-progress-bar package in an elegant way.

peti commented 11 years ago

Well, hacking the source code of the package is something I'd rather avoid, because if I do that then I can no longer re-use the package from Hackage as-is, but have to publish my own version instead.

In my humble opinion, it would be easiest to split the API into two parts:

  1. functions that generate the string that represents a properly formatted progress bar, and
  2. functions that display that string, i.e. write it to stdout/stderr.

If the code that does (1) were accessible to me, then I could easily write the progress bar to stderr, or display it on a web page, or do whatever I'd like to do without having to patch the source code of your library.

peti commented 11 years ago

Duh, I just realize that mkProgessBar does exactly what I need! Sorry for the noise.