Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[console] Allow breaking execution of scripts via Ctrl-C #6213

Merged
merged 1 commit into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/console/console_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, shellOut, out=None, style=None):
self.sO = shellOut
self.out = None
self.style = style
self.fire_keyboard_interrupt = False

def write(self, m):
if self.style == "_traceback":
Expand All @@ -62,6 +63,10 @@ def write(self, m):
if self.style != "_traceback":
QCoreApplication.processEvents()

if self.fire_keyboard_interrupt:
self.fire_keyboard_interrupt = False
raise KeyboardInterrupt

def move_cursor_to_end(self):
"""Move cursor to end of text"""
line, index = self.get_end_pos()
Expand Down
18 changes: 16 additions & 2 deletions python/console/console_sci.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,16 @@ def keyPressEvent(self, e):
if not self.is_cursor_on_edition_zone() or startLine < endLine:
# allow copying and selecting
if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier):
if e.key() in (Qt.Key_C, Qt.Key_A):
if e.key() == Qt.Key_C:
# only catch and return from Ctrl-C here if there's a selection
if self.hasSelectedText():
QsciScintilla.keyPressEvent(self, e)
return
elif e.key() == Qt.Key_A:
QsciScintilla.keyPressEvent(self, e)
return
return
else:
return
# allow selection
if e.modifiers() & Qt.ShiftModifier:
if e.key() in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Home, Qt.Key_End):
Expand All @@ -389,6 +396,11 @@ def keyPressEvent(self, e):
# all other keystrokes get sent to the input line
self.move_cursor_to_end()

if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier) and e.key() == Qt.Key_C and not self.hasSelectedText():
# keyboard interrupt
sys.stdout.fire_keyboard_interrupt = True
return

line, index = self.getCursorPosition()
cmd = self.text(line)

Expand Down Expand Up @@ -602,12 +614,14 @@ def write(self, txt):
sys.stderr.write(txt)

def writeCMD(self, txt):
sys.stdout.fire_keyboard_interrupt = False
if len(txt) > 0:
getCmdString = self.text()
prompt = getCmdString[0:4]
sys.stdout.write(prompt + txt + '\n')

def runsource(self, source, filename='<input>', symbol='single'):
sys.stdout.fire_keyboard_interrupt = False
hook = sys.excepthook
try:
def excepthook(etype, value, tb):
Expand Down