2012-02-27 8 views
17

私はPythonの対話セッションをカスタマイズするための標準的なヒントを使用した場合のPythonの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 

は、今私は文脈依存完了と色プロンプトを取得します。カラープロンプトから来る

問題 - 私はPythonの対話セッションのReadlineに履歴検索・後方(UPを押して)を呼び出し、カーソル位置が誤って計算し、テキストが誤って表示されていたので、ACOUNT端末エスケープシーケンスに取ります。プロンプトPythonのために、この問題を解決する方法

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

:Bashのmanページで

特殊なマーカーで言及し、固定この問題?

答えて

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

もっと良いpythonシェルの経験のために、ipythonまたはbpythonのいずれかを使用することをお勧めします。

+0

+1。 bpythonは素晴らしいことです!どうすればdjango **/manage.py ** console?私のソリューションは、この目的のためにbpython使用する方法、Djangoのインタラクティブセッションで補完を有効にしますか? – gavenkoa

+1

@gavenkoa [core.managment.commands.shell](https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py)を見ると、 'ipython '失敗すると、' bpython'が使われます。両方をインストールしている場合でも、そのファイルを編集して 'shells'クラスの属性を並べ替えることができるので、' bpython'が 'ipython'の前に試みられます。共有knowladgeため – jcollado

+0

感謝 – gavenkoa

関連する問題