Skip to content
Permalink
Browse files

[pyqgis-console] restore the correct prompt

  • Loading branch information
slarosa authored and nyalldawson committed Oct 4, 2020
1 parent 3b811bd commit 8533d9a89b3cbc8d8ecc5771b7f0bf596e85210d
Showing with 24 additions and 25 deletions.
  1. +12 −0 python/console/console_base.py
  2. +12 −25 python/console/console_sci.py
@@ -31,6 +31,8 @@ class QgsPythonConsoleBase(QgsCodeEditorPython):

MARKER_NUM = 6

HANDY_COMMANDS = ['_pyqgis', '_api', '_cookbook']

def __init__(self, parent=None):
super().__init__(parent)

@@ -137,6 +139,16 @@ def searchPyQGIS(self):
version = '.'.join(Qgis.QGIS_VERSION.split('.')[0:2])
QDesktopServices.openUrl(QUrl('https://qgis.org/pyqgis/' + version + '/search.html?q=' + text))

def handyCommands(self, hcmd):
version = 'master' if 'master' in Qgis.QGIS_VERSION.lower() else re.findall(r'^\d.[0-9]*', Qgis.QGIS_VERSION)[0]
if hcmd == '_pyqgis':
QDesktopServices.openUrl(QUrl("https://qgis.org/pyqgis/{}".format(version)))
elif hcmd == '_api':
QDesktopServices.openUrl(QUrl("https://qgis.org/api/{}".format('' if version == 'master' else version)))
elif hcmd == '_cookbook':
QDesktopServices.openUrl(QUrl("https://docs.qgis.org/{}/en/docs/pyqgis_developer_cookbook/".format(
'testing' if version == 'master' else version)))


if __name__ == "__main__":
pass
@@ -66,7 +66,7 @@ def __init__(self, parent=None):

self.buffer = []

self.displayPrompt(False)
self.displayPrompt(self.buffer)

for line in _init_commands:
self.runsource(line)
@@ -212,15 +212,15 @@ def move_cursor_to_start(self):
self.setCursorPosition(0, 0)
self.ensureCursorVisible()
self.ensureLineVisible(0)
self.displayPrompt(False)
self.displayPrompt(self.buffer)

def move_cursor_to_end(self):
"""Move cursor to end of text"""
line, index = self.get_end_pos()
self.setCursorPosition(line, index)
self.ensureCursorVisible()
self.ensureLineVisible(line)
self.displayPrompt(False)
self.displayPrompt(self.buffer)

def is_cursor_on_last_line(self):
"""Return True if cursor is on the last line"""
@@ -417,8 +417,7 @@ def keyPressEvent(self, e):
self.setCursorPosition(line, index + 7)
QsciScintilla.keyPressEvent(self, e)

if len(self.text(0)) == 0 or hasSelectedText:
self.displayPrompt(False)
self.displayPrompt(self.buffer)

def contextMenuEvent(self, e):
menu = QMenu(self)
@@ -505,7 +504,7 @@ def insertFromDropPaste(self, textDP):
cleanLine = line.replace(">>> ", "").replace("... ", "")
self.insert(cleanLine)
self.move_cursor_to_end()
self.runCommand(self.currentCommand())
self.runCommand(self.text())
if pasteList[-1] != "":
line = pasteList[-1]
cleanLine = line.replace(">>> ", "").replace("... ", "")
@@ -518,42 +517,30 @@ def insertTextFromFile(self, listOpenFile):
self.append(line)
self.move_cursor_to_end()
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
self.runCommand(self.currentCommand())
self.runCommand(self.text())
self.append(listOpenFile[-1])
self.move_cursor_to_end()
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)

def entered(self):
self.move_cursor_to_end()
self.runCommand(self.currentCommand())
self.runCommand(self.text())
self.setFocus()
self.move_cursor_to_end()

def currentCommand(self):
string = self.text()
cmd = string
return cmd

def runCommand(self, cmd):
self.writeCMD(cmd)
import webbrowser
self.updateHistory(cmd)
version = 'master' if 'master' in Qgis.QGIS_VERSION.lower() else re.findall(r'^\d.[0-9]*', Qgis.QGIS_VERSION)[0]
if cmd in ('_pyqgis', '_api', '_cookbook'):
if cmd == '_pyqgis':
webbrowser.open("https://qgis.org/pyqgis/{}".format(version))
elif cmd == '_api':
webbrowser.open("https://qgis.org/api/{}".format('' if version == 'master' else version))
elif cmd == '_cookbook':
webbrowser.open("https://docs.qgis.org/{}/en/docs/pyqgis_developer_cookbook/".format(
'testing' if version == 'master' else version))
if cmd in self.HANDY_COMMANDS:
self.handyCommands(cmd)
more = False
else:
self.buffer.append(cmd)
src = "\n".join(self.buffer)
more = self.runsource(src)
if not more:
self.buffer = []

# prevents to commands with more lines to break the console
# in the case they have a eol different from '\n'
self.setText('')
@@ -567,8 +554,8 @@ def writeCMD(self, txt):
if sys.stdout:
sys.stdout.fire_keyboard_interrupt = False
if len(txt) > 0:
getCmdString = self.text()
sys.stdout.write('>>> ' + txt + '\n')
prompt = "... " if self.buffer else ">>> "
sys.stdout.write(prompt + txt + '\n')

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

0 comments on commit 8533d9a

Please sign in to comment.
You can’t perform that action at this time.