Skip to content

Commit

Permalink
[python console] Move the >>> prompt into the margin to fix a gazilli…
Browse files Browse the repository at this point in the history
…on issues
  • Loading branch information
nirvn committed Jun 3, 2020
1 parent d9c87b3 commit 296fc6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 46 deletions.
1 change: 0 additions & 1 deletion python/console/console_output.py
Expand Up @@ -152,7 +152,6 @@ def __init__(self, parent=None):
self.setMarginWidth(0, 0)
self.setMarginWidth(1, 0)
self.setMarginWidth(2, 0)
# fm = QFontMetrics(font)
self.setMarginsFont(font)
self.setMarginWidth(1, "00000")
self.setMarginLineNumbers(1, True)
Expand Down
64 changes: 19 additions & 45 deletions python/console/console_sci.py
Expand Up @@ -83,9 +83,17 @@ def __init__(self, parent=None):

self.new_input_line = True

# Set the default font
font = QFontDatabase.systemFont(QFontDatabase.FixedFont)
self.setFont(font)
self.setMarginsFont(font)
# Margin 0 is used for line numbers
self.setMarginWidth(0, 0)
self.setMarginWidth(1, 0)
self.setMarginWidth(2, 0)
self.setMarginsFont(font)
self.setMarginWidth(1, "00000")
self.setMarginType(1, 5)

self.buffer = []

Expand Down Expand Up @@ -171,6 +179,8 @@ def refreshSettingsShell(self):
QColor(self.MATCHED_BRACE_BACKGROUND_COLOR))))
self.setMatchedBraceForegroundColor(QColor(self.settings.value("pythonConsole/matchedBraceForegroundColor",
QColor(self.MATCHED_BRACE_FOREGROUND_COLOR))))
self.setMarginsBackgroundColor(
QColor(self.settings.value("pythonConsole/marginBackgroundColor", QColor(self.MARGIN_BACKGROUND_COLOR))))

# Sets minimum height for input area based of font metric
self._setMinimumHeight()
Expand All @@ -195,12 +205,8 @@ def autoCompleteKeyBinding(self):
def commandConsole(self, commands):
if not self.is_cursor_on_last_line():
self.move_cursor_to_end()
line, pos = self.getCursorPosition()
selCmdLength = len(self.text(line))
self.setSelection(line, 4, line, selCmdLength)
self.removeSelectedText()
for cmd in commands:
self.append(cmd)
self.setText(cmd)
self.entered()
self.move_cursor_to_end()
self.setFocus()
Expand Down Expand Up @@ -310,11 +316,6 @@ def is_cursor_on_last_line(self):
cline, _ = self.getCursorPosition()
return cline == self.lines() - 1

def is_cursor_on_edition_zone(self):
""" Return True if the cursor is in the edition zone """
cline, cindex = self.getCursorPosition()
return cline == self.lines() - 1 and cindex >= 4

def new_prompt(self, prompt):
"""
Print a new prompt and save its (line, index) position
Expand All @@ -326,8 +327,7 @@ def new_prompt(self, prompt):
self.ensureLineVisible(line)

def displayPrompt(self, more=False):
self.append("... ") if more else self.append(">>> ")
self.move_cursor_to_end()
self.SendScintilla(QsciScintilla.SCI_MARGINSETTEXT, 0, str.encode("..." if more else ">>>"))

def updateHistory(self, command):
if isinstance(command, list):
Expand Down Expand Up @@ -389,38 +389,30 @@ def clearHistorySession(self):

def showPrevious(self):
if self.historyIndex < len(self.history) and self.history:
line, pos = self.getCursorPosition()
selCmdLength = len(self.text(line))
self.setSelection(line, 4, line, selCmdLength)
self.removeSelectedText()
self.historyIndex += 1
if self.historyIndex == len(self.history):
self.insert("")
self.setText("")
pass
else:
self.insert(self.history[self.historyIndex])
self.setText(self.history[self.historyIndex])
self.move_cursor_to_end()
# self.SendScintilla(QsciScintilla.SCI_DELETEBACK)

def showNext(self):
if self.historyIndex > 0 and self.history:
line, pos = self.getCursorPosition()
selCmdLength = len(self.text(line))
self.setSelection(line, 4, line, selCmdLength)
self.removeSelectedText()
self.historyIndex -= 1
if self.historyIndex == len(self.history):
self.insert("")
self.setText("")
else:
self.insert(self.history[self.historyIndex])
self.setText(self.history[self.historyIndex])
self.move_cursor_to_end()
# self.SendScintilla(QsciScintilla.SCI_DELETEBACK)

def keyPressEvent(self, e):
startLine, startPos, endLine, endPos = self.getSelection()

# handle invalid cursor position and multiline selections
if not self.is_cursor_on_edition_zone() or startLine < endLine:
if startLine < endLine:
# allow copying and selecting
if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier):
if e.key() == Qt.Key_C:
Expand Down Expand Up @@ -455,24 +447,9 @@ def keyPressEvent(self, e):

elif e.key() in (Qt.Key_Left, Qt.Key_Home):
QsciScintilla.keyPressEvent(self, e)
# check whether the cursor is moved out of the edition zone
newline, newindex = self.getCursorPosition()
if newline < line or newindex < 4:
# fix selection and the cursor position
if self.hasSelectedText():
self.setSelection(line, self.getSelection()[3], line, 4)
else:
self.setCursorPosition(line, 4)

elif e.key() in (Qt.Key_Backspace, Qt.Key_Delete):
QsciScintilla.keyPressEvent(self, e)
# check whether the cursor is moved out of the edition zone
_, newindex = self.getCursorPosition()
if newindex < 4:
# restore the prompt chars (if removed) and
# fix the cursor position
self.insert(cmd[:3 - newindex] + " ")
self.setCursorPosition(line, 4)
self.recolor()

elif (e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier) and e.key() == Qt.Key_V) or \
Expand Down Expand Up @@ -624,10 +601,8 @@ def entered(self):
self.move_cursor_to_end()

def currentCommand(self):
linenr, index = self.getCursorPosition()
string = self.text()
cmdLine = string[4:]
cmd = cmdLine
cmd = string
return cmd

def runCommand(self, cmd):
Expand Down Expand Up @@ -664,8 +639,7 @@ 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')
sys.stdout.write('>>> ' + txt + '\n')

def runsource(self, source, filename='<input>', symbol='single'):
if sys.stdout:
Expand Down

0 comments on commit 296fc6c

Please sign in to comment.