Skip to content

A helper function to calculate the width should be used in fmt funcs #9

@songzhi

Description

@songzhi

What Rust std::fmt does:

Acording to this doc,if I want to print some content which is around with spaces,I should write like this.println!("Hello {:5}!", "x");

But actually it has bugs when dealing with like CJK chars.I suppose it is because Rust think width as chars().count().So when I want to print a 10-chars -width line,it actually prints longer.Because CJK-like char counts 1 char but displays 2-chars-width.

What I want:

Here's my solution,It's simple but works:

let total_displayed_width = 10;
let chars_count = "你好".chars().count();
let content_displayed_width = UnicodeWidthStr::width("你好");
let width_in_formatter = total_displayed_width - (content_displayed_width - chars_count);
print!("{}{:^width$}{}", '|', "你好", '|', width = width_in_formatter);
// It prints '|   你好   |',perfectly 10-chars-width

Of course we can do it personally,but I think it is better this crate could provide a func like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions