Skip to content

Commit

Permalink
fix: allow multiple command pastes in console
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed May 14, 2020
1 parent 0ee2d52 commit 4fb38fb
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions brownie/_cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from prompt_toolkit.auto_suggest import AutoSuggest, Suggestion
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.history import FileHistory
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.keys import Keys
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles.pygments import style_from_pygments_cls
from pygments import highlight
Expand Down Expand Up @@ -134,14 +136,21 @@ def __init__(self, project=None, extra_locals=None):
kwargs["auto_suggest"] = TestAutoSuggest(locals_dict)
if console_settings["completions"]:
kwargs["completer"] = ConsoleCompleter(locals_dict)

# add binding for multi-line pastes
key_bindings = KeyBindings()
key_bindings.add(Keys.BracketedPaste)(self.paste_event)
self.compile_mode = "single"

self.prompt_session = PromptSession(
history=SanitizedFileHistory(history_file, locals_dict),
input=self.prompt_input,
key_bindings=key_bindings,
**kwargs,
)

if console_settings["auto_suggest"]:
# remove the builting binding for auto-suggest acceptance
# remove the builtin binding for auto-suggest acceptance
key_bindings = self.prompt_session.app.key_bindings
accept_binding = key_bindings.get_bindings_for_keys(("right",))[0]
key_bindings._bindings2.remove(accept_binding.handler)
Expand Down Expand Up @@ -175,6 +184,15 @@ def _console_write(self, obj):
def raw_input(self, prompt=""):
return self.prompt_session.prompt(prompt)

def paste_event(self, event):
data = event.data
data = data.replace("\r\n", "\n")
data = data.replace("\r", "\n")

if "\n" in data:
self.compile_mode = "exec"
event.current_buffer.insert_text(data)

def showsyntaxerror(self, filename):
tb = color.format_tb(sys.exc_info()[1])
self.write(tb + "\n")
Expand All @@ -184,8 +202,11 @@ def showtraceback(self):
self.write(tb + "\n")

def runsource(self, source, filename="<input>", symbol="single"):
mode = self.compile_mode
self.compile_mode = "single"

try:
code = self.compile(source, filename, "single")
code = self.compile(source, filename, mode)
except (OverflowError, SyntaxError, ValueError):
self.showsyntaxerror(filename)
return False
Expand Down

0 comments on commit 4fb38fb

Please sign in to comment.