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

Fix input prompt repeating value when value is longer than terminal width. Closes #214 #239

Closed
wants to merge 1 commit into from
Closed

Fix input prompt repeating value when value is longer than terminal width. Closes #214 #239

wants to merge 1 commit into from

Conversation

jviotti
Copy link

@jviotti jviotti commented Apr 27, 2015

The problem happens because inquirer cleans only the current line on
each key stroke, without paying attention to the actual length of the
value, which might be longer than one line.

This PR was only tested on OS X. Help from people with other OSes would be appreciated.

* @return {Number} - The string length
*/

var REGEX_SYMBOLS = /([\0-\u02FF\u0370-\u1DBF\u1E00-\u20CF\u2100-\uD7FF\uDC00-\uFE1F\uFE30-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF])([\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]+)/g;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line gives a long line warning from the linter. What can we do about it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most linters can be configured to ignore lines with regular expressions in, for that exact reason. You can use the constructor function, but then you're compromising code quality, which isn't great.

@callumacrae
Copy link

Is there any way you can add a test for this?

@jviotti
Copy link
Author

jviotti commented Apr 27, 2015

@callumacrae Let me see what can I do.

tty.lines = function() {
var value = this.rl.line || this.rl.history[0];
var lineLength = utils.getUnicodeStringLength( this.rl._prompt + value );
var result = lineLength / cliWidth();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cliWidth can return 0 which is definitively not what we want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SBoudrias In which cases can it return 0?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case: https://github.com/knownasilya/cli-width/blob/master/index.js#L17

Pretty sure all the time on Windows.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I guess my fix will not work in Windows in that case. Let me borrow a PC and give it a try there. Maybe there is an alternative way to get the terminal width in Windows.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get free Windows VM to test on https://www.modern.ie/en-us/virtualization-tools#downloads

Pretty sure you can install git and checkout inquirer with them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SBoudrias I've tested in Windows 8.1 and cliWidth returns the correct width on that platform. I had to implement some windows-specific workarounds for my patch to work fine, but I can confirm it works as expected on cmd.exe.

There still exist the possibility of cliWidth() returning 0, maybe in older Windows versions? I propose checking the return value of cliWidth, and if it is zero, don't make use of my patch, thus resulting in the actual behaviour. What do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I would vote for 😃

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a potential workaround.

Another is to manually handle line returns so we stay consistent. But that might just bring new kinds of issues.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@callumacrae @SBoudrias Workaround implemented. tty.lines now returns 1 if cliWidth is 0, which, preserves the original behaviour.

@jviotti
Copy link
Author

jviotti commented Apr 28, 2015

Works great in Ubuntu as well.

…idth. Closes #214

The problem happens because inquirer cleans only the current line on
each key stroke, without paying attention to the actual length of the
value, which might be longer than one line.
@jviotti
Copy link
Author

jviotti commented Apr 29, 2015

I was able to write some unit tests that check that the corresponding number of lines are cleaned, and that my patch is reverted if cliWidth() === 0.

done();
}.bind(this));

this.rl.line = "hello";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, I have to assign this.rl.line directly, as emitting a line event doesn't assign the actual line to neither this.rl.line or this.rl.history, where tty expects them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct as we use a mocked readline. So there's no usual Readline behavior during testing.

@jviotti
Copy link
Author

jviotti commented May 11, 2015

@SBoudrias Is there anything else needed for this PR to be merged?

@SBoudrias
Copy link
Owner

Hey, I'd have a couple comments on the code changes, but I'll hold onto them for now because this fix is very specific to the input prompt and I have a solution in mind that would fix this issue for every prompt type. I'm experimenting with it ATM and just need some more time to make sure it'll work fine.

@SBoudrias
Copy link
Owner

I'm going forward with the rendering process redesign. I'll close this PR as I don't intend to merge it.

@LeftoverChineseFood
Copy link

seriously, please, please merge this PR. We're still having problems with it 6 years later.

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.

4 participants