On Thu, Aug 23, 2012 at 5:58 PM, Thomas Kluyver <<a href="mailto:takowl@gmail.com">takowl@gmail.com</a>> wrote:<br>> Hi Skipper,<br>><br>> On 23 August 2012 19:13, Skipper Seabold <<a href="mailto:jsseabold@gmail.com">jsseabold@gmail.com</a>> wrote:<br>
>> I'm having some trouble working through the code. Is there anyway to inject<br>>> user-defined (callable) substitutions into the prompts. E.g., something like<br>><br>> I don't have time to test at the moment, but I think you need to add<br>
> it using the IPython.core.prompts.LazyEvaluate wrapper, something like<br>> this:<br>><br>> @LazyEvaluate<br>> def am_at_home():<br>> import os<br>> if os.path.expanduser('~') == os.path.abspath(os.path.curdir):<br>
> return "(home)"<br>> return ""<br>><br>> get_ipython().prompt_manager.lazy_evaluate_fields['amathome'] = am_at_home<br>> get_ipython().prompt_manager.in_template = '[\#]: {amathome}'<br>
><br>> Note the new-style formatting syntax, rather than \amathome.<br>><br>> This example is written for a startup script -<br>> PromptManager.lazy_evaluate_fields is not currently configurable (and<br>> adding values to lists or dicts in configuration is awkward anyway.<br>
><br>> Hope that helps,<br>> Thomas<br><br>Thanks Thomas,<br><br>Cool. Works like a charm, my ipython config now to give my git branch in the ipython terminal<br><br>c.TerminalIPythonApp.exec_lines = [<br> """<br>
from IPython.core.prompts import LazyEvaluate<br><br>@LazyEvaluate<br>def parse_git_branch():<br> import subprocess<br> cmd = 'ref=$(git symbolic-ref HEAD 2> /dev/null); echo \"(\"${ref#refs/heads/}\")\"'<br>
branch = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]<br> if not branch.strip().strip("()"):<br> return ""<br> else:<br> return branch""",<br>
"""get_ipython().prompt_manager.lazy_evaluate_fields["parse_git_branch"] = parse_git_branch""",<br> r"get_ipython().prompt_manager.in_template = '[\Y0/] {parse_git_branch}\n[\#]: '"<br>
]<br><br><br><br>