pbapply icon indicating copy to clipboard operation
pbapply copied to clipboard

Have progress bar overwrite consistently in terminal (enhancement)

Open aaronjfisher opened this issue 1 year ago • 0 comments

Thank you for creating this package! I find the setTimerProgressBar function immensely useful.

This is a super minor issue, but there are some situations where running setTimerProgressBar in the terminal produces very messy output with 1 line printed per iteration. This is a similar issue to the one described regarding knitr reports. When I run R in the terminal, setTxtProgressBar always produces a bar that only ever takes up one line, and overwrites itself in an animated way. But setTimerProgressBar sometimes produces a new line of text for every progress bar update. This seems to happen in the terminal when the progress bar is wide enough to require wrapping around the edge of the window. It also doesn't happen in RStudio or the R GUI, just in the terminal.

Is there a way to make the terminal output prettier automatically, without having to resize the window to avoid text explosions?

For example, in my terminal

pbTxt <- txtProgressBar(0, 5)
for (i in 1:5){
  setTxtProgressBar(pbTxt, i)
}

produces

> pbTxt <- txtProgressBar(0, 5)
> for (i in 1:5){
+   setTxtProgressBar(pbTxt, i)
+ }
================================================================================> 

But

pbTime <- timerProgressBar(0, 5)
for (i in 1:5){
  Sys.sleep(.2)
  setTimerProgressBar(pbTime, i)
}

produces the following (scroll to the right to see full output)

for (i in 1:5){
+   setTimerProgressBar(pbTime, i)
+ }
  |================                                                          |================================                                          |================================================                          |================================================================          |================================================================================| 100% elapsed=01s, remaining~00s> 

However! If I reduce the width, the behavior is OK again:

for (i in 1:5){
+   setTimerProgressBar(pbTime, i)
+ }
  |==========| 100% elapsed=01s, remaining~00s 

Again it's a super minor issue. I didn't realize when I was first submitting it that the width made a difference, but for many users it could be helpful. Up until now I've done work around but writing things like

if(i %% 50 == 0) setTimerProgressBar(pbTime, i)

aaronjfisher avatar Aug 08 '22 14:08 aaronjfisher