Skip to content

Commit

Permalink
[pyqgis-console] restore the correct prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
slarosa authored and nyalldawson committed Oct 4, 2020
1 parent 3b811bd commit 8533d9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
12 changes: 12 additions & 0 deletions python/console/console_base.py
Expand Up @@ -31,6 +31,8 @@ class QgsPythonConsoleBase(QgsCodeEditorPython):

MARKER_NUM = 6

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

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

Expand Down Expand Up @@ -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
37 changes: 12 additions & 25 deletions python/console/console_sci.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("... ", "")
Expand All @@ -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('')
Expand All @@ -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:
Expand Down

0 comments on commit 8533d9a

Please sign in to comment.