彩色的readline提示会破坏控制权-a [英] Colorized readline prompt breaks control-a

查看:90
本文介绍了彩色的readline提示会破坏控制权-a的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序将打印彩色的阅读行提示.它破坏了控制-a(光标最终移到了比应有的位置靠右的位置):

The following program prints a colorized readline prompt. It breaks control-a (the cursor ends up much farther to the right than it should be):

#include <readline/readline.h>
#include <readline/history.h>

#define CYELLOW "\001\e[0;31m\002"
#define RESET   "\001\e[0m\002"

int main(int argc, char **argv)
{
    readline(CYELLOW "prompt> " RESET);
    return 0;
}

control-a在我不带彩色提示的情况下调用readline()时起作用:

control-a works when I call readline() without a colorized prompt:

readline("prompt> ");

我正在使用Mac OS X 10.9.4.这是otool的输出:

I'm using Mac OS X 10.9.4. Here's the output of otool:

/usr/lib/libreadline.dylib:
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1

似乎有一些关于stackoverflow的问题:

There are a couple of stackoverflow questions that seem to touch on this:

如果使用颜色提示,请查看如何在Python阅读行中修复列计算

他们两个都提到颜色标记必须用\ 001和\ 002括起来.我遵循了这些指示,但控制-a仍然坏了.

Both of them mention that color markers need to be bracketed by \001 and \002. I followed those instructions but control-a is still broken.

推荐答案

Mac OS X使用NetBSD editline库,该库包括readline的部分兼容性实现.

Mac OS X uses the NetBSD editline library, which includes a partial compatibility implementation of readline.

似乎试图添加readline的功能,允许用户在提示符下使用字符RL_PROMPT_START_IGNORERL_PROMPT_END_IGNORE(在'\1'和'\2' >).不幸的是,由于打字错误,该功能从未在rl_initialize中启用.

It appears that an attempt was made to add readline's feature of allowing the user to bracket terminal control sequences in the prompt using the characters RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE (defined as '\1' and '\2' respectively in readline.h). Unfortunately, due to a small typo, the feature is never enabled in rl_initialize.

当我阅读代码(我尚未测试过)时,解决方法是将readline.c的第327行[请参见注释1]从以下位置更改:

As I read the code (I haven't tested this), the fix would be to change line 327 of readline.c [see note 1] from:

el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE);

收件人:

el_set(e, EL_PROMPT_ESC, _get_prompt, RL_PROMPT_START_IGNORE);

这将需要您找到libedit的源代码,进行微不足道的更改,重新配置和重新编译它,然后安装它.我没有Mac,因此无法指导您完成整个过程. (有关可能有用的源存储库的链接,请参见注释1.)

That would require you to find the source code for libedit, make that trivial change, reconfigure and recompile it, and then install it. I don't have a Mac so I can't guide you through that process. (See Note 1 for links to source repositories which might be useful.)

另一种解决方案是在您的项目中使用GNU readline库.它显然在MacPorts中可用,因此,如果使用它,它应该很容易安装.

Another solution would be to use the GNU readline library in your project. It's apparently available in MacPorts, so if you use that, it should be easy enough to install.

  1. 我从 OS X源存储库中获得了该行号.在
  1. I got that line number from this OS X source repository. In the head revision of the NetBSD distribution, it's at line 337. But it should be easy enough to find by searching for EL_PROMPT. Just make sure that the symbol EL_PROMPT_ESC is defined in histedit.h; if it isn't, the version of the editline library you've found is probably too old.

这篇关于彩色的readline提示会破坏控制权-a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆