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

Looping History with Unicode Items #7

Closed
mitnk opened this issue Apr 3, 2017 · 6 comments
Closed

Looping History with Unicode Items #7

mitnk opened this issue Apr 3, 2017 · 6 comments

Comments

@mitnk
Copy link

mitnk commented Apr 3, 2017

When adding history and fetch them back with Ctrl+P, Unicode item would result wrong buffer rendering.

demo $ echo hi
hi
demo $ ls 世界
ls: 世界: No such file or directory
demo $ echo yoo
yoo

Then, let's fetch items back with Ctrl+P:

demo $ echo yoo       # <-- first `Ctrl+P` got "echo yoo", good!
demo $ ls 世界      # <-- second, we got "ls 世界", good!
demo $ lsecho hi    # <-- now a third `Ctrl+P` fetched something weird

Though, when you hit ENTER now, you will get output "hi", which means this is only a rendering issue. It is actually "echo hi" in buffer, not "lsecho hi".

@murarth
Copy link
Owner

murarth commented Apr 3, 2017

Yes, there appears to be a mistake in how multibyte characters are counted. I'm looking into it now.

@murarth
Copy link
Owner

murarth commented Apr 3, 2017

Nope. I was mistaken. Multibyte characters are handled properly as far as moving the internal cursor around.

The issue is actually caused by fullwidth characters and how they are treated by terminals. Solving this problem will be a bit more interesting. Fortunately, there already exists a crate for computing the width of a string, so the most confusing part is already done.

@mitnk
Copy link
Author

mitnk commented Apr 3, 2017

Linefeed seems know the total chars (i.e. 5 for "ls 世界") in buffer. But when moving cursor, it seems cursor moves over bytes (two moves on surface of char "界") instead of chars (or boundary), compare to bash.

@murarth
Copy link
Owner

murarth commented Apr 3, 2017

That was my first thought, as well, but it turned out to be wrong. Other multibyte characters such as "ü" and "•" were being treated correctly.

What's actually happening is that the terminal application considers characters such as "世" (called fullwidth characters) to be occupying two columns. linefeed was erroneously assuming that any character could occupy only one column and so, when it would signal to the terminal to move from the end of the line to the beginning of the line "ls 世界", it would signal to the terminal to move five columns left. Instead, linefeed must signal to the terminal to move seven columns left.

I've corrected by this checking for fullwidth characters (using the simple and wonderful unicode-width crate) and giving such characters two columns, which implicitly corrects terminal cursor movement operations.

I've just committed this change. Please let me know if it solves the problem or if you encounter any other issues.

@mitnk
Copy link
Author

mitnk commented Apr 3, 2017

Just checked with master, it solved the issue! Awesome! Thanks a lot!

@murarth murarth closed this as completed Apr 3, 2017
@murarth
Copy link
Owner

murarth commented Apr 3, 2017

You're welcome and thanks for filing the issue. Also, I've just published the fix on crates.io as version 0.2.3.

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

No branches or pull requests

2 participants