From 8533d9a89b3cbc8d8ecc5771b7f0bf596e85210d Mon Sep 17 00:00:00 2001 From: Salvatore Larosa Date: Sat, 3 Oct 2020 16:50:19 +0200 Subject: [PATCH] [pyqgis-console] restore the correct prompt --- python/console/console_base.py | 12 +++++++++++ python/console/console_sci.py | 37 +++++++++++----------------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/python/console/console_base.py b/python/console/console_base.py index f41bc40c5e4..757bd8aa692 100644 --- a/python/console/console_base.py +++ b/python/console/console_base.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 @@ class QgsPythonConsoleBase(QgsCodeEditorPython): 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 diff --git a/python/console/console_sci.py b/python/console/console_sci.py index 8c08b87d7f5..f4c377ef162 100644 --- a/python/console/console_sci.py +++ b/python/console/console_sci.py @@ -66,7 +66,7 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): self.buffer = [] - self.displayPrompt(False) + self.displayPrompt(self.buffer) for line in _init_commands: self.runsource(line) @@ -212,7 +212,7 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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""" @@ -220,7 +220,7 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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,35 +517,22 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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) @@ -554,6 +540,7 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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 @@ class ShellScintilla(QgsPythonConsoleBase, code.InteractiveInterpreter): 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='', symbol='single'): if sys.stdout: