2012-02-27 19 views
17

Ben interaktif Python oturumu özelleştirmek için standart ipuçlarını kullanın eğer Python taleb sütun hesaplama nasıl düzeltileceği Bak: kullanım renk istemi

 
    $ 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 

Şimdi bağlam duyarlı tamamlama ve renk istemi olsun.

Sorun renk isteminde gelen - Ben tarih-arama-geri imleç pozisyonu yanlış hesaplandı ve metin yanlış gösteriliyordu böylece, acount terminali çıkış sıralarını almak Readline interaktif Python oturumunda (UPbasarak) çağırdığınızda . Bash adam sayfasında

bu sorun özel belirteçler tarafından bahsedilen ve sabit:

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

Nasıl istemi Python için bu sorunu gidermek için?

cevap

25

Ben bilgi taleb ve bulunan açın:

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

olarak söylemek Ben RL_PROMPT_START_IGNORE ve RL_PROMPT_END_IGNORE tanımı içinde readline.h aramak metin ve sonraki bulundu:

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

~ 'ya uygun değişiklikler yaptım ~ /.pystartup:

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

ve şimdi bütün iş para cezası !!!

4

Daha iyi bir python kabuğu deneyimi için, ipython veya bpython'u kullanmanızı öneririz.

+0

+1. bpython harika bir şey! Django **./Manage.py ** konsoluna ne dersiniz? Benim çözüm de django interaktif oturumda, bu amaçla bpython nasıl kullanılır tamamlanmasını sağlar? – gavenkoa

+1

@gavenkoa Bakın [core.managment.commands.shell] (https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py), eğer ipython ise Başarısız, 'bpython' kullanılır. Her ikisini de yüklediyseniz, yine de bu dosyayı düzenleyebilir ve 'shells' class özniteliğini 'bpython'un ipython'dan önce denenmesi için yeniden düzenleyebilirsiniz. – jcollado

+0

Bilgi paylaşımı için teşekkürler – gavenkoa