Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add named constants to designate interpreter states
  • Loading branch information
YoannQDQ authored and nyalldawson committed Apr 24, 2023
1 parent 29016d0 commit 139ee63
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions python/console/console_sci.py
Expand Up @@ -71,6 +71,12 @@
]


# States of the interpreter
PS1 = 0 # Writing a new command
PS2 = 1 # Continuation of a multi-line command
SUBPROCESS = 2 # Sending input to a subprocess


class PythonInterpreter(QgsCodeInterpreter, code.InteractiveInterpreter):

def __init__(self, shell):
Expand All @@ -90,15 +96,15 @@ def __init__(self, shell):
def execCommandImpl(self, cmd, show_input=True):

# Child process running, input should be sent to it
if self.currentState() == 2:
if self.currentState() == SUBPROCESS:
sys.stdout.write(cmd + "\n")
self.sub_process.write(cmd)
return 0

if show_input:
self.writeCMD(cmd)

if self.currentState() == 0:
if self.currentState() == PS1:

# This line makes single line commands with leading spaces work
cmd = cmd.strip()
Expand Down Expand Up @@ -161,8 +167,7 @@ def writeCMD(self, txt):
if sys.stdout:
sys.stdout.fire_keyboard_interrupt = False
if len(txt) > 0:
prompt = "... " if self.currentState() == 1 else ">>> "
sys.stdout.write(prompt + txt + '\n')
sys.stdout.write(f'{self.promptForState()} {txt}\n')

def runsource(self, source, filename='<input>', symbol='single'):
if sys.stdout:
Expand All @@ -181,13 +186,15 @@ def excepthook(etype, value, tb):

def currentState(self):
if self.sub_process:
return 2
return SUBPROCESS
return super().currentState()

def promptForState(self, state):
if state == 2:
def promptForState(self, state=-1):
if state == -1:
state = self.currentState()
if state == SUBPROCESS:
return " : "
elif state == 1:
elif state == PS2:
return "..."
else:
return ">>>"
Expand Down

0 comments on commit 139ee63

Please sign in to comment.