Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display a path with multi-byte characters correctly when it is truncated #830

Merged
merged 1 commit into from
Apr 24, 2018

Conversation

bravestarr
Copy link
Contributor

@bravestarr bravestarr commented Apr 22, 2018

When a path is rendered in the Bookmarks area or as the header line of the
tree, it is truncated if there is no enough space for it. But if a path
contains multi-byte characters, it should be truncated by characters, not
bytes, otherwise the path may be truncated between the bytes of a
multi-byte character. To deal with multi-byte characters, use
strdisplaywidth() instead of len() to get the number of display cells, and
use strcharpart() instead of strpart() to truncate a path.

When a path is rendered in the Bookmarks area or as the header line of the
tree, it is truncated if there is no enough space for it.  But if a path
contains multi-byte characters, it should be truncated by characters, not
bytes, otherwise the path may be truncated between the bytes of a
multi-byte character.  To deal with multi-byte characters, use
strdisplaywidth() instead of len() to get the number of display cells, and
use strcharpart() instead of strpart() to truncate a path.
@PhilRunninger
Copy link
Member

@bravestarr , I'm not sure how to test this. I tried creating a file with a mix of characters from both ends of the Latin set, one with all non-ASCII characters, and even one that was all emojis. Can you provide some screenshots or instructions for recreating?

Thanks,

@bravestarr
Copy link
Contributor Author

bravestarr commented Apr 24, 2018

I have reproduced the problem by making a directory as a bookmark of which the path contains Chinese characters. The full path is E:\for-nerdtree\我的书签. I have no idea if you have any Chinese fonts installed, if not, the four Chinese characters are just as in the following screenshots. Each Chinese character is encoded as three bytes (UTF-8), so the four characters are <E6 88 91>, <E7 9A 84>, <E4 B9 A6>, <E7 AD BE>.

When no truncation is needed, all goes well:
all-is-good

Change the width of the window for nerdtree, and double-click the bookmark to force the window to be redrawn. When the leading byte of the first character is stripped off, the path is shown as:
bookmark-1
Note that the remaining two bytes of the first character are rendered as their hex representation.

Make the window even narrower, when the leading two bytes of the second character are stripped off:
bookmark-2

The head line of the tree has the similar problem:
tree-1
tree-2

I also tried a path with Greek letters, the problem remained too. I think the problem should occur when a path contains any multi-byte characters. @PhilRunninger , You said you had tried non-ASCII characters, if the problem didn't appear, I guess you may not change the width of the window to the degree that only the leading bytes of a multi-byte character are stripped off, or you may not double-click the bookmark to force the window to be redrawn.

@PhilRunninger
Copy link
Member

@bravestarr Thanks for the instructions, and the improvement to the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants