2012-02-27 31 views
17

tôi sử dụng các mẹo tiêu chuẩn để tùy biến phiên Python tương tác:Nhìn cách sửa chữa tính cột bằng Python readline nếu nhắc sử dụng màu sắc

 
    $ 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 

Bây giờ tôi có được hoàn bối cảnh nhạy cảm và kịp thời màu sắc.

Vấn đề đến từ dấu nhắc màu - khi tôi gọi lịch sử tìm kiếm-backward (bằng cách nhấn UP) trong phiên Python tương tác Readline mất trong chuỗi thoát terminal acount, vì vậy vị trí con trỏ đã được tính toán sai và văn bản được hiển thị sai .

Trong trang người đàn ông Bash vấn đề này được đề cập và cố định bởi các dấu hiệu đặc biệt:

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

Làm thế nào để khắc phục vấn đề này trong Python nhắc?

Trả lời

25

tôi mở thông tin readline và thấy:

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

Như nói văn bản tôi tìm kiếm RL_PROMPT_START_IGNORERL_PROMPT_END_IGNORE định nghĩa trong readline.h và thấy tiếp theo:

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

Vì vậy, tôi đặt các thay đổi thích hợp cho ~ /.pystartup:

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

và bây giờ tất cả đều hoạt động tốt !!!

4

Để có trải nghiệm vỏ trăn tốt hơn, tôi khuyên bạn nên sử dụng hoặc ipython hoặc bpython.

+0

+1. bpython là điều tuyệt vời! Làm thế nào về django **./Manage.py ** console? Giải pháp của tôi cũng cho phép hoàn thành trong phiên tương tác django, làm thế nào để sử dụng bpython cho mục đích này? – gavenkoa

+1

@gavenkoa Nhìn vào [core.managment.commands.shell] (https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py), tôi thấy rằng nếu 'ipython 'không thành công,' bpython' được sử dụng. Nếu bạn đã cài đặt xong, bạn vẫn có thể chỉnh sửa tập tin đó và sắp xếp lại thuộc tính lớp 'shells' để' bpython' được thử trước 'ipython'. – jcollado

+0

Cảm ơn bạn đã chia sẻ knowladge – gavenkoa

Các vấn đề liên quan