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

Escape non-printing characters #2154

Merged
merged 2 commits into from Mar 27, 2021
Merged

Escape non-printing characters #2154

merged 2 commits into from Mar 27, 2021

Conversation

xjunior
Copy link
Contributor

@xjunior xjunior commented Sep 6, 2020

Escape non-printing characters to avoid weird behavior when colors are used.

According to https://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt

From lib/readline/display.c:243 in bash source code:

243 /* Current implementation:
244         \001 (^A) start non-visible characters
245         \002 (^B) end non-visible characters
246    all characters except \001 and \002 (following a \001) are copied to
247    the returned string; all characters except those between \001 and
248    \002 are assumed to be `visible'. */

Closes #493

@xjunior
Copy link
Contributor Author

xjunior commented Sep 7, 2020

This is usually not a problem when it's just printing lines, but it becomes an issue if the text color helpers are used to produce a nice prompt. Also, this conforms to the readline spec for scaping non-visible characters.

@kyrylo
Copy link
Member

kyrylo commented Dec 23, 2020

Could you provide a before/after example?

@xjunior
Copy link
Contributor Author

xjunior commented Dec 31, 2020

@kyrylo this solves the issue discussed here: #493

Here's an example of it breaking:

.pryrc

Pry.config.prompt_name = Pry::Helpers::Text.green("MAKE IT LONG")
Screen.Recording.2020-12-31.at.18.29.03.mov

Here is with the fix:
.pryrc

Pry.config.prompt_name = "\001\033[0;32m\002MAKE IT LONG\001\033[0m\002"
Screen.Recording.2020-12-31.at.18.33.10.mov

As you can see, it will start breaking as you navigate through the history and edit the line, especially if you navigate through big lines that span more than one line on your terminal.

@kyrylo
Copy link
Member

kyrylo commented Mar 27, 2021

Thanks for the extra info!

@kyrylo kyrylo merged commit 438ce37 into pry:master Mar 27, 2021
SilverPhoenix99 added a commit to SilverPhoenix99/pry that referenced this pull request Jul 4, 2021
SilverPhoenix99 added a commit to SilverPhoenix99/pry that referenced this pull request Jul 5, 2021
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.

Custom prompt with color
2 participants