彩色输出打破了 readline 的换行
我正在使用 Ruby 中的 readline 对一些输出进行着色,但我没有运气让换行正常工作。例如:
"\e[01;32mThis prompt is green and bold\e[00m > "
期望的结果是:
This prompt is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
我实际得到的是:
aaaaaaaaaaa is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
如果我删除颜色代码,换行工作正常。我知道在 bash 中,如果颜色代码被错误终止,就会发生这种情况,但我已经尝试了我能想到的一切,包括一些不同的 gem,并且行为是相同的。它也会发生在具有不同 Readline 版本的多个系统上。这个特定项目使用的是 rb-readline,而不是 C readline。
I'm working with colorizing some output using readline in Ruby, but I am not having any luck getting line wrapping to work properly. For example:
"\e[01;32mThis prompt is green and bold\e[00m > "
The desired result would be:
This prompt is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
What I actually get is:
aaaaaaaaaaa is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
If I remove the color codes, line wrapping works correctly. I know with bash, this can happen if the color codes are incorrectly terminated, but I have tried everything I can think of, including a few different gems, and the behavior is the same. It also occurs on multiple systems with different versions of Readline. This particular project is using rb-readline
as opposed to C readline
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,sunkencity 得到了复选标记,因为我最终使用了他的大部分解决方案,但我必须对其进行如下修改:
每个序列都需要包含在 \001..\002 中,以便 Readline 知道忽略非打印字符。
Ok, sunkencity gets the check mark because I ended up using most of his solution, but I had to modify it as follows:
Each sequence needs to be wrapped in \001..\002 so that Readline knows to ignore non printing characters.
当我需要为控制台的字符串着色时,我总是抛出这个字符串扩展。您的代码中的问题似乎是终止符,应该只有一个零“\e[0m”。
I always throw this string extension in when I need to colorize strings for console. The problem in your code seems to be the terminator, there should be just one zero "\e[0m".
这个问题不是 Ruby 特有的 - 它也出现在 bash 中。如果您放入 bash shell,
您将看到与上面相同的结果。但如果你投入,
你就会得到你想要的结果。
This problem is not ruby-specific - it occurs in bash too. If you put in a bash shell
you will see the same result as above. But if you put in
you will get the result you wanted.