Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[bug] character width calculate error with unicode #137

Copy link
Copy link

Description

@gsw945
Issue body actions

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

Reactions are currently unavailable

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.