Skip to content

Logging

bjmfactory edited this page Nov 18, 2014 · 1 revision

While using the progress bar, you may wish to log some output for the user. If you attempt to do this using a standard puts statement, you'll find that the text will overwrite the bar. For example if you were to puts "hello" after progress has already begun, you may get something like this:

helloess: |=======                                                             |
Progress: |========                                                            |

This happens because the ruby-progressbar has to keep redrawing itself every time you change the progress. It's a limitation of terminal output. Using puts messes that up because puts adds a newline which moves the cursor to the next line, then when ruby-progressbar updates, it does so on the following line.

To circumvent this, use #log instead.

progressbar = ProgressBar.create
progressbar.progress = 20
progressbar.log 'hello'
hello
Progress: |=============                                                       |

#log will automatically clear the bar, print your desired text and then redraw the bar on the following line. Notice that we did not get a bar above the logged output. If you consistently use #log, you should only ever see one bar on the screen at any time.