如果使用颜色提示,如何修复 Python readline 中的列计算 [英] How to fix column calculation in Python readline if using color prompt

查看:17
本文介绍了如果使用颜色提示,如何修复 Python readline 中的列计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用标准提示来自定义交互式 Python 会话:

<前>$ cat ~/.bashrc导出 PYTHONSTARTUP=~/.pystartup$ cat ~/.pystartup导入操作系统导入系统进口出口导入阅读线导入完成器historyPath = os.path.expanduser("~/.pyhistory")def save_history(historyPath=historyPath):导入阅读线readline.write_history_file(historyPath)如果 os.path.exists(historyPath):readline.read_history_file(historyPath)term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce']如果 os.environ.get('TERM') 在 term_with_colors 中:绿色='33[32m'红色='33[31m'重置='33[0m'sys.ps1 = 红色 + '>>> ' + 重置sys.ps2 = 绿色 + '...' + 重置del term_with_colorsatexit.register(save_history)del os、sys、atexit、readline、rlcompleter、save_history、historyPath

现在我得到了上下文相关的完成和颜色提示.

问题来自颜色提示 - 当我在交互式 Python 会话中调用 history-search-backward(通过按 UP)时,Readline 会考虑终端转义序列,所以光标位置计算错误,文字显示错误.

在 Bash 手册页中,这个问题被特殊标记提到并修复:

<前>[ 开始一系列非打印字符,可用于嵌入终端控制序列进入提示] 结束非打印字符序列

如何解决 Python 提示的这个问题?

解决方案

我打开 info readline 并发现:

<前>-- 函数:int rl_expand_prompt (char *prompt)在 PROMPT 中展开任何特殊字符序列并设置本地 Readline 提示重新显示变量.这个功能是由‘readline()’调用.它也可以被称为扩展如果 `rl_on_new_line_with_prompt()' 函数或使用了`rl_already_prompted' 变量.它返回的数量最后一行的可见字符(可能是多行)迅速的.应用程序可能表明提示包含显示时不占用物理屏幕空间的字符用特殊标记将一系列这样的字符括起来RL_PROMPT_START_IGNORE"和RL_PROMPT_END_IGNORE"(在‘readline.h’.这可用于嵌入特定于终端的转义提示中的序列.

如文本所示,我在 readline.h 中搜索了 RL_PROMPT_START_IGNORERL_PROMPT_END_IGNORE 定义并找到了下一个:

<前>/* 可供 readline 客户端使用的定义.*/#define RL_PROMPT_START_IGNORE '01'#define RL_PROMPT_END_IGNORE '02'

所以我对我的~/.pystartup进行了适当的更改:

<前>绿色='0133[32m02'红色='0133[31m02'重置='0133[0m02'

现在一切正常!!!

I use standard tips for customizing interactive Python session:

$ cat ~/.bashrc
export PYTHONSTARTUP=~/.pystartup

$ cat ~/.pystartup
import os
import sys
import atexit
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce']
if os.environ.get('TERM') in term_with_colors:
    green='33[32m'
    red='33[31m'
    reset='33[0m'
    sys.ps1 = red + '>>> ' + reset
    sys.ps2 = green + '... ' + reset
del term_with_colors

atexit.register(save_history)
del os, sys, atexit, readline, rlcompleter, save_history, historyPath

Now I get context sensitive completion and color prompt.

The problem comes from color prompt - when I invoke history-search-backward (by pressing UP) in an interactive Python session, Readline takes in account terminal escape sequences, so the cursor position is wrongly calculated and the text is wrongly displayed.

In Bash man page this problem is mentioned and fixed by special markers:

    [     begin a sequence of non-printing characters,
           which could be used to embed a
           terminal control sequence into the prompt
    ]     end a sequence of non-printing characters

How to fix this issue for Python prompt?

解决方案

I open info readline and found:

 -- Function: int rl_expand_prompt (char *prompt)
     Expand any special character sequences in PROMPT and set up the
     local Readline prompt redisplay variables.  This function is
     called by `readline()'.  It may also be called to expand the
     primary prompt if the `rl_on_new_line_with_prompt()' function or
     `rl_already_prompted' variable is used.  It returns the number of
     visible characters on the last line of the (possibly multi-line)
     prompt.  Applications may indicate that the prompt contains
     characters that take up no physical screen space when displayed by
     bracketing a sequence of such characters with the special markers
     `RL_PROMPT_START_IGNORE' and `RL_PROMPT_END_IGNORE' (declared in
     `readline.h'.  This may be used to embed terminal-specific escape
     sequences in prompts.

As text suggested I searched for RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE definition in readline.h and found next:

/* Definitions available for use by readline clients. */
#define RL_PROMPT_START_IGNORE  '01'
#define RL_PROMPT_END_IGNORE    '02'

So I put appropriate changes to my ~/.pystartup:

    green='0133[32m02'
    red='0133[31m02'
    reset='0133[0m02'

and now all work fine!!!

这篇关于如果使用颜色提示,如何修复 Python readline 中的列计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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