我正在使用 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
.
好吧,sunkencity 得到了复选标记,因为我最终使用了他的大部分解决方案,但我必须对其进行如下修改:
# encoding: utf-8
class String
def console_red; colorize(self, "\001\e[1m\e[31m\002"); end
def console_dark_red; colorize(self, "\001\e[31m\002"); end
def console_green; colorize(self, "\001\e[1m\e[32m\002"); end
def console_dark_green; colorize(self, "\001\e[32m\002"); end
def console_yellow; colorize(self, "\001\e[1m\e[33m\002"); end
def console_dark_yellow; colorize(self, "\001\e[33m\002"); end
def console_blue; colorize(self, "\001\e[1m\e[34m\002"); end
def console_dark_blue; colorize(self, "\001\e[34m\002"); end
def console_purple; colorize(self, "\001\e[1m\e[35m\002"); end
def console_def; colorize(self, "\001\e[1m\002"); end
def console_bold; colorize(self, "\001\e[1m\002"); end
def console_blink; colorize(self, "\001\e[5m\002"); end
def colorize(text, color_code) "#{color_code}#{text}\001\e[0m\002" end
end
每个序列都需要包含在 \001..\002 中,以便 Readline 知道忽略非打印字符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)