Skip to content

[bug] character width calculate error with unicode #137

@gsw945

Description

@gsw945

Description

Chinese characters lead to line breaks or print repeatedly, because of the characters' width calculated error.

Code

  1. 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:
    001

  2. 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:
    002

  3. 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:
    003

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions