如果使用颜色提示,如何在Python readline中修复列计算 [英] How to fix column calculation in Python readline if using color prompt
问题描述
我使用标准技巧自定义交互式Python会话:
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='\033[32m'
red='\033[31m'
reset='\033[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.
问题来自颜色提示-当我在交互式Python会话中调用 history-search-backward (通过按 UP )时,Readline考虑了终端转义序列,因此光标位置计算错误,文本显示错误.
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.
在Bash手册页中,通过特殊标记提到并解决了此问题:
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
如何为Python提示符解决此问题?
How to fix this issue for Python prompt?
推荐答案
我打开信息阅读行,发现:
-- 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.
我说的是文字,我在 readline.h 中搜索 RL_PROMPT_START_IGNORE 和 RL_PROMPT_END_IGNORE 定义,然后找到下一个:
As say text I search 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 '\001'
#define RL_PROMPT_END_IGNORE '\002'
因此,我对〜/.pystartup 进行了适当的更改:
So I put appropriate changes to my ~/.pystartup:
green='\001\033[32m\002'
red='\001\033[31m\002'
reset='\001\033[0m\002'
现在一切正常!
这篇关于如果使用颜色提示,如何在Python readline中修复列计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!