2012-02-27 3 views
17

내가 대화 형 파이썬 세션을 사용자 정의하는 표준 팁을 사용하는 경우 파이썬의 readline의 열 계산을 해결하는 방법을 봐 : 색상을 사용 프롬프트

 
    $ 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 

이 지금은 상황에 맞는 완성 및 색상 프롬프트를 얻을.

문제는 컬러 프롬프트에서 온 - 나는 역사 검색 뒤로 커서 위치가 잘못 계산 및 텍스트가 잘못 표시 그래서, ACOUNT 터미널 이스케이프 시퀀스에 걸릴 readline에 대화 형 파이썬 세션에서 (UP을 을 눌러)를 호출 할 때 . 배쉬 man 페이지에서

이 문제는 특수 마커 언급 고정 :

 
    \[  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 

어떻게하라는 메시지가 파이썬이 문제를 해결하려면?

답변

25

나는 정보의 readline 발견 엽니 다

 
-- 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. 

으로 말을 내가 RL_PROMPT_START_IGNORERL_PROMPT_END_IGNORE 정의 readline.h 검색 텍스트와 다음 발견

 
/* Definitions available for use by readline clients. */ 
#define RL_PROMPT_START_IGNORE '\001' 
#define RL_PROMPT_END_IGNORE '\002' 

그래서 적절한 변경 내용을 내 ~ /.pystartup :

 
    green='\001\033[32m\002' 
    red='\001\033[31m\002' 
    reset='\001\033[0m\002' 

이제 모두 잘됩니다!

4

더 나은 파이썬 셸 경험을 위해 ipython 또는 bpython을 사용하는 것이 좋습니다.

+0

+1. bpython은 좋은 것입니다! 어때요 django **./manage.py ** console? 내 솔루션은 또한 장고 대화 형 세션에서 완성을 가능케하는데,이 목적을 위해 bpython을 사용하는 방법은 무엇입니까? – gavenkoa

+1

@gavenkoa [core.managment.commands.shell] (https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py)에서 보았을 때,'ipython '실패하면'bpython'이 사용됩니다. 둘 다 설치했다면, 여전히'ipython' 전에'bpython'을 시도하도록'shells' 클래스 속성을 재정렬하고 파일을 편집 할 수 있습니다. – jcollado

+0

knowladge 공유 주셔서 감사합니다. – gavenkoa

관련 문제