2012-02-27 14 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 तो कर्सर की स्थिति को गलत तरीके से गणना की गई थी और पाठ को गलत तरीके से प्रदर्शित किया गया था (यूपी दबाकर) आह्वान ।

बैश आदमी पेज में इस समस्या का उल्लेख किया और विशेष मार्कर द्वारा तय:

 
    \[  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_IGNORE और RL_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 ** कंसोल? मेरा समाधान django इंटरैक्टिव सत्र में पूरा करने में सक्षम बनाता है, इस उद्देश्य के लिए bpython का उपयोग कैसे करें? – gavenkoa

+1

@gavenkoa [core.managment.commands.shell] पर देख रहे हैं (https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py), मुझे लगता है कि अगर 'ipython 'विफल रहता है,' bpython' का उपयोग किया जाता है। यदि आपने दोनों स्थापित किए हैं, तो आप अभी भी उस फ़ाइल को संपादित कर सकते हैं और 'shells' वर्ग विशेषता को पुन: व्यवस्थित कर सकते हैं ताकि' ipython' से पहले 'bpython' का प्रयास किया जा सके। – jcollado

+0

knowladge साझा करने के लिए धन्यवाद – gavenkoa

संबंधित मुद्दे