have precommand firing working

This commit is contained in:
Anthony Scopatz 2016-11-20 18:09:13 -05:00
parent d0ac0c2efc
commit c971dd9838
5 changed files with 46 additions and 44 deletions

View file

@ -55,8 +55,6 @@ else:
_sys.modules['xonsh.proc'] = __amalgam__
xontribs = __amalgam__
_sys.modules['xonsh.xontribs'] = __amalgam__
base_shell = __amalgam__
_sys.modules['xonsh.base_shell'] = __amalgam__
dirstack = __amalgam__
_sys.modules['xonsh.dirstack'] = __amalgam__
history = __amalgam__
@ -67,8 +65,6 @@ else:
_sys.modules['xonsh.xonfig'] = __amalgam__
environ = __amalgam__
_sys.modules['xonsh.environ'] = __amalgam__
readline_shell = __amalgam__
_sys.modules['xonsh.readline_shell'] = __amalgam__
tracer = __amalgam__
_sys.modules['xonsh.tracer'] = __amalgam__
replay = __amalgam__
@ -83,8 +79,12 @@ else:
_sys.modules['xonsh.imphooks'] = __amalgam__
shell = __amalgam__
_sys.modules['xonsh.shell'] = __amalgam__
base_shell = __amalgam__
_sys.modules['xonsh.base_shell'] = __amalgam__
main = __amalgam__
_sys.modules['xonsh.main'] = __amalgam__
readline_shell = __amalgam__
_sys.modules['xonsh.readline_shell'] = __amalgam__
del __amalgam__
except ImportError:
pass

View file

@ -16,6 +16,7 @@ from xonsh.codecache import (should_use_cache, code_cache_name,
from xonsh.completer import Completer
from xonsh.prompt.base import multiline_prompt, PromptFormatter
from xonsh.events import events
from xonsh.shell import fire_precommand
if ON_WINDOWS:
import ctypes
@ -324,14 +325,12 @@ class BaseShell(object):
info['out'] = last_out
else:
info['out'] = tee_out + '\n' + last_out
events.on_postcommand.fire(
info['inp'],
info['rtn'],
info.get('out', None),
info['ts']
)
)
hist.append(info)
hist.last_cmd_rtn = hist.last_cmd_out = None
@ -352,38 +351,10 @@ class BaseShell(object):
self.buffer.append(line)
if self.need_more_lines:
return None, None
src = self.obtain_source()
src = ''.join(self.buffer)
src = fire_precommand(src)
return self.compile(src)
def obtain_source(self):
"""Gets the source code from the current buffer and any precommand
event handlers that may be active.
"""
i = 0
limit = sys.getrecursionlimit()
lst = ''
src = raw = ''.join(self.buffer)
while src != lst:
lst = src
srcs = events.on_precommand.fire(src)
for s in srcs:
if s != lst:
src = s
break
i += 1
if i == limit:
print_exception('Modifcations to source input took more than '
'the recurssion limit number of interations to '
'converge.')
if builtins.__xonsh_env__.get('XONSH_DEBUG') and src != raw:
sys.stderr.writelines(difflib.unified_diff(
raw.splitlines(keepends=True),
src.splitlines(keepends=True),
fromfile='before precommand event',
tofile='after precommand event',
))
return src
def compile(self, src):
"""Compiles source code and returns the (possibly modified) source and
a valid code object.

View file

@ -6,8 +6,10 @@ from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import (Condition, IsMultiline, HasSelection,
EmacsInsertMode, ViInsertMode)
from prompt_toolkit.keys import Keys
from xonsh.aliases import xonsh_exit
from xonsh.tools import ON_WINDOWS, check_for_partial_string
from xonsh.shell import fire_precommand
env = builtins.__xonsh_env__
DEDENT_TOKENS = frozenset(['raise', 'return', 'pass', 'break', 'continue'])
@ -70,6 +72,7 @@ def _is_blank(l):
def can_compile(src):
"""Returns whether the code can be compiled, i.e. it is valid xonsh."""
src = src if src.endswith('\n') else src + '\n'
src = fire_precommand(src, show_diff=False)
src = src.lstrip()
try:
builtins.__xonsh_execer__.compile(src, mode='single', glbs=None,

View file

@ -29,13 +29,11 @@ class PromptToolkitShell(BaseShell):
self.prompter = Prompter()
self.history = PromptToolkitHistory()
self.pt_completer = PromptToolkitCompleter(self.completer, self.ctx)
key_bindings_manager_args = {
'enable_auto_suggest_bindings': True,
'enable_search': True,
'enable_abort_and_exit_bindings': True,
}
'enable_auto_suggest_bindings': True,
'enable_search': True,
'enable_abort_and_exit_bindings': True,
}
self.key_bindings_manager = KeyBindingManager(**key_bindings_manager_args)
load_xonsh_bindings(self.key_bindings_manager)
@ -91,7 +89,7 @@ class PromptToolkitShell(BaseShell):
line = self.prompter.prompt(**prompt_args)
return line
def push(self, line):
def _push(self, line):
"""Pushes a line onto the buffer and compiles the code in a way that
enables multiline input.
"""

View file

@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
"""The xonsh shell"""
import os
import sys
import random
import difflib
import builtins
import warnings
@ -27,6 +29,34 @@ Fires just after a command is executed.
""")
def fire_precommand(src, show_diff=True):
"""Returns the results of firing the precommand handles."""
i = 0
limit = sys.getrecursionlimit()
lst = ''
raw = src
while src != lst:
lst = src
srcs = events.on_precommand.fire(src)
for s in srcs:
if s != lst:
src = s
break
i += 1
if i == limit:
print_exception('Modifcations to source input took more than '
'the recurssion limit number of interations to '
'converge.')
if show_diff and builtins.__xonsh_env__.get('XONSH_DEBUG') and src != raw:
sys.stderr.writelines(difflib.unified_diff(
raw.splitlines(keepends=True),
src.splitlines(keepends=True),
fromfile='before precommand event',
tofile='after precommand event',
))
return src
class Shell(object):
"""Main xonsh shell.