Skip to content

Support multiline inputs in non TTY contexts#518

Merged
gwenn merged 11 commits intokkawakam:masterfrom
basile-henry:multiline-non-tty
Jun 13, 2021
Merged

Support multiline inputs in non TTY contexts#518
gwenn merged 11 commits intokkawakam:masterfrom
basile-henry:multiline-non-tty

Conversation

@basile-henry
Copy link
Copy Markdown
Contributor

This PR add supports for multiline inputs (via the Validator API) for the case where stdin isn't a TTY (or is an unsupported terminal).

I have added a new example input_multiline to make quickly showcase how this works.

This lets us do:

echo -e "(\n\n)" | cargo run --example input_multiline

And have readline correctly read this as a single multiline input instead of 3 separate inputs (if it was called in a loop).
Even in the single line case, there was a difference between the TTY and the non-TTY versions of readline: the non-TTY version would keep the trailing newline character.

My main motivation for this change is to be able to easily test a REPL made using rustyline by piping my inputs in without having to mock the TTY part of it. It also make rustyline more consistent this way.

@basile-henry
Copy link
Copy Markdown
Contributor Author

I have also added support to handle backspace characters in non TTY mode.

This makes the following work:

echo -e "([)\n\b\b\n])" | cargo run --example input_multiline 2> /dev/null
Input: ([
])

@gwenn
Copy link
Copy Markdown
Collaborator

gwenn commented May 8, 2021

Sorry, but there should be no ANSI escape sequence (colors) in the input stream.
See #215 (comment)

@basile-henry
Copy link
Copy Markdown
Contributor Author

there should be no ANSI escape sequence (colors) in the input stream

I don't really understand what you mean. I don't think I have added anything ANSI dependent in the input stream. Are you referring to '\b'? I believe that is a ASCII control character. And by the looks of it python-prompt-toolkit also supports it.

Or are you referring to the output stream, and I need to filter out ANSI sequences in the messages from the validator?

@gwenn
Copy link
Copy Markdown
Collaborator

gwenn commented May 8, 2021

there should be no ANSI escape sequence (colors) in the input stream

I don't really understand what you mean. I don't think I have added anything ANSI dependent in the input stream. Are you referring to '\b'? I believe that is a ASCII control character. And by the looks of it python-prompt-toolkit also supports it.

Or are you referring to the output stream, and I need to filter out ANSI sequences in the messages from the validator?

Sorry I was completely wrong !

@basile-henry
Copy link
Copy Markdown
Contributor Author

Since I have fixed an edge case in the way the Invalid case worked the quick example is now this:

echo -e "([)\n\b\n])" | cargo run --example input_multiline 2> /dev/null
Input: ([
])

Notice how I don't need to backspace the \n anymore, because the input is invalid the newline isn't entered, so \b char applies to ) instead.

@gwenn gwenn merged commit 13a0b71 into kkawakam:master Jun 13, 2021
@gwenn
Copy link
Copy Markdown
Collaborator

gwenn commented Jun 13, 2021

Thanks and sorry for the long delay.

@basile-henry
Copy link
Copy Markdown
Contributor Author

No worries, thanks for reviewing and taking the feature in! 😄

@basile-henry basile-henry deleted the multiline-non-tty branch June 13, 2021 10:57
@gwenn
Copy link
Copy Markdown
Collaborator

gwenn commented Aug 17, 2021

Version 9.0.0 released.

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