2012-02-27 4 views
17

io uso punte standard, per personalizzare la sessione interattiva di Python:Guardate come risolvere il problema di calcolo di colonna in Python readline se pronta all'uso di colore

 
    $ 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 

Ora ho il completamento sensibile al contesto e tempestiva colore.

problema viene dal prompt di colore - quando invoco storia-search-backward (premendo UP) nella sessione interattiva di Python Readline prendere in terminal acount sequenze di escape, quindi la posizione del cursore è stato erroneamente calcolato e il testo è stato visualizzato erroneamente .

Nella pagina man bash questo problema menzionato e fissati con speciali marcatori:

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

Come risolvere questo problema per Python chiederà?

risposta

25

apro informazioni readline e trovato:

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

Come dicono testo Cerco RL_PROMPT_START_IGNORE e RL_PROMPT_END_IGNORE definizione readline.h e trovato vicino:

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

Quindi applico le modifiche appropriate al mio ~ /.pystartup:

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

e ora tutto funzionano benissimo !!!

4

Per una migliore esperienza della shell Python, ti consigliamo di utilizzare ipython o bpython.

+0

+1. bpython è una grande cosa! Che ne dici di django **./Manage.py ** console? La mia soluzione consente anche il completamento della sessione interattiva di django, come usare bpython per questo scopo? – gavenkoa

+1

@gavenkoa Guardando [core.managment.commands.shell] (https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py), vedo che se 'ipython 'fallisce,' bpython' viene usato. Se hai entrambi installato, puoi comunque modificare quel file e riordinare l'attributo di classe 'shells' in modo che venga tentato' bpython' prima di 'ipython'. – jcollado

+0

Grazie per aver condiviso knowladge – gavenkoa

Problemi correlati