0

I am using progressbar2 with FormatCustomText. The text is parameterized (eg "%s") and the width of the string to display varies.

Let's say on the first iteration, the substituted text is "AA." On the second iteration, the text is "B."

progressbar displays BA on the second iteration instead of B.

Is there a way to clear everything to the right of the cursor to the end of the line after B? Normally, this is done with control-K (chr(11)).

Unfortunately, adding chr(11) to the end of the string given to FormatCustomText causes a newline character to be output for each iteration.

Any other ideas?

Terris
  • 887
  • 1
  • 10
  • 15
  • 1
    What if you pad your substitute value with whitespace before it gets substituted? – N Chauhan Aug 15 '18 at 23:34
  • Thanks for your reply! Unfortunately, the length is based on strings in a database that can get rather long. Padding excessive whitespace can cause the terminal to wrap and the cursor to go to the next line which defeats the niceness of ProgressBar. – Terris Aug 16 '18 at 14:31

2 Answers2

0

Print "\033[K" at the end

progressbar.ProgressBar(widgets=[format_custom_text, "\033[K"])

Ideally the text to substitute should have a maximum length based on a percentage of the width of the terminal. I found a way to get the width but there is probably a better way! (How to get Linux console window width in Python)

This works in python3+:

import shutil shutil.get_terminal_size((80, 20)) # Provide default values

Terris
  • 887
  • 1
  • 10
  • 15
0

There is a better answer on github

Terris
  • 887
  • 1
  • 10
  • 15