Description
Chinese characters lead to line breaks or print repeatedly, because of the characters' width calculated error.
Code
use FormatCustomText:
import progressbar
import time
total = 12
current = 0
format_custom_text = progressbar .FormatCustomText (
'%(progress)6s: ' ,
dict (
progress = '进度'
),
)
bar = progressbar .ProgressBar (
widgets = [
format_custom_text ,
progressbar .Bar (),
' ' ,
progressbar .Counter (),
' / {0}' .format (total )
],
max_value = total ,
redirect_stdout = True
)
bar .start ()
bar .update (current )
while current < total :
current += 1
bar .update (current )
time .sleep (0.5 )
bar .finish ()
output:
use raw text:
import progressbar
import time
total = 12
current = 0
tip = '进度: '
bar = progressbar .ProgressBar (
widgets = [
tip ,
progressbar .Bar (),
' ' ,
progressbar .Counter (),
' / {0}' .format (total )
],
max_value = total ,
redirect_stdout = True
)
bar .start ()
bar .update (current )
while current < total :
current += 1
bar .update (current )
time .sleep (0.5 )
bar .finish ()
output:
fill length of str with 6(a chinese character's width is 2) by using \0
import progressbar
import time
total = 12
current = 0
tip = '进\0 度\0 : '
bar = progressbar .ProgressBar (
widgets = [
tip ,
progressbar .Bar (),
' ' ,
progressbar .Counter (),
' / {0}' .format (total )
],
max_value = total ,
redirect_stdout = True
)
bar .start ()
bar .update (current )
while current < total :
current += 1
bar .update (current )
time .sleep (0.5 )
bar .finish ()
output:
ps: 3 is the effect i expected.
Versions
Python version: 3.5.2
Python distribution/environment: CPython
Operating System: Ubuntu 16.04 LTS
Package version: 3.33.2
maybe helpful
wcwidth Python library that measures the width of unicode strings rendered to a terminal
Reactions are currently unavailable
Description
Chinese characters lead to line breaks or print repeatedly, because of the characters' width calculated error.
Code
use
FormatCustomText:output:

use raw text:
output:

fill length of str with 6(a chinese character's width is 2) by using
\0output:

ps: 3 is the effect i expected.
Versions
maybe helpful
wcwidth Python library that measures the width of unicode strings rendered to a terminal