Make CommandPrompt aware of its prompt.
This commit is contained in:
parent
4318a01774
commit
3ea0f0d8a6
@ -44,12 +44,8 @@ class ConsoleLineEdit(CommandLineEdit):
|
||||
sys.ps1 = '>>> '
|
||||
if not hasattr(sys, 'ps2'):
|
||||
sys.ps2 = '... '
|
||||
|
||||
def validator(text):
|
||||
"""Check if a given input is valid."""
|
||||
return any(text.startswith(p) for p in (sys.ps1, sys.ps2))
|
||||
|
||||
super().__init__(parent, validator)
|
||||
super().__init__(parent)
|
||||
self.set_prompt(sys.ps1)
|
||||
self.setFont(config.get('fonts', 'debug-console'))
|
||||
self._more = False
|
||||
self._buffer = []
|
||||
@ -90,6 +86,7 @@ class ConsoleLineEdit(CommandLineEdit):
|
||||
# printed and don't ooen a crashdialog.
|
||||
with fake_io(self.write.emit), disabled_excepthook():
|
||||
self._more = self._interpreter.runsource(source, '<console>')
|
||||
self.set_prompt(self.curprompt)
|
||||
if not self._more:
|
||||
self._buffer = []
|
||||
|
||||
|
@ -52,15 +52,10 @@ class CommandLineEdit(QLineEdit):
|
||||
_validator: The current command validator.
|
||||
"""
|
||||
|
||||
def __init__(self, parent, validator):
|
||||
"""Constructor.
|
||||
|
||||
Args:
|
||||
validator: A function which checks if a given input is valid.
|
||||
"""
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.history = History()
|
||||
self._validator = _CommandValidator(validator, parent=self)
|
||||
self._validator = _CommandValidator(self)
|
||||
self.setValidator(self._validator)
|
||||
self.textEdited.connect(self.on_text_edited)
|
||||
|
||||
@ -69,6 +64,11 @@ class CommandLineEdit(QLineEdit):
|
||||
"""Slot for textEdited. Stop history browsing."""
|
||||
self.history.stop()
|
||||
|
||||
def set_prompt(self, text):
|
||||
self._validator.prompt = text
|
||||
# FIXME we should also adjust offsets so the user can't put the cursor
|
||||
# to the left of the prompt.
|
||||
|
||||
def __repr__(self):
|
||||
return '<{} "{}">'.format(self.__class__.__name__, self.text())
|
||||
|
||||
@ -77,9 +77,9 @@ class _CommandValidator(QValidator):
|
||||
|
||||
"""Validator to prevent the : from getting deleted."""
|
||||
|
||||
def __init__(self, validator, parent=None):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.validator = validator
|
||||
self.prompt = None
|
||||
|
||||
def validate(self, string, pos):
|
||||
"""Override QValidator::validate.
|
||||
@ -91,7 +91,7 @@ class _CommandValidator(QValidator):
|
||||
Return:
|
||||
A tuple (status, string, pos) as a QValidator should.
|
||||
"""
|
||||
if self.validator(string):
|
||||
if self.prompt is None or string.startswith(self.prompt):
|
||||
return (QValidator.Acceptable, string, pos)
|
||||
else:
|
||||
return (QValidator.Invalid, string, pos)
|
||||
|
@ -77,12 +77,7 @@ class Command(MinimalLineEditMixin, CommandLineEdit):
|
||||
# for a possible fix.
|
||||
|
||||
def __init__(self, parent=None):
|
||||
|
||||
def validator(text):
|
||||
"""Check if a given input is valid."""
|
||||
return any(text.startswith(p) for p in STARTCHARS)
|
||||
|
||||
CommandLineEdit.__init__(self, parent, validator)
|
||||
CommandLineEdit.__init__(self, parent)
|
||||
MinimalLineEditMixin.__init__(self)
|
||||
self.cursor_part = 0
|
||||
self.history.history = QApplication.instance().cmd_history.data
|
||||
@ -185,7 +180,7 @@ class Command(MinimalLineEditMixin, CommandLineEdit):
|
||||
strings: A list of strings to set.
|
||||
"""
|
||||
text = ' '.join(strings)
|
||||
if not any(text.startswith(c) for c in STARTCHARS):
|
||||
if not text[0] in STARTCHARS:
|
||||
raise CommandError("Invalid command text '{}'.".format(text))
|
||||
self.set_cmd_text(text)
|
||||
|
||||
@ -303,3 +298,14 @@ class Command(MinimalLineEditMixin, CommandLineEdit):
|
||||
"""Extend focusInEvent to enter command mode."""
|
||||
modeman.maybe_enter(KeyMode.command, 'cmd focus')
|
||||
super().focusInEvent(e)
|
||||
|
||||
def setText(self, text):
|
||||
"""Extend setText to set prefix and make sure the prompt is ok."""
|
||||
if not text:
|
||||
pass
|
||||
elif text[0] in STARTCHARS:
|
||||
super().set_prompt(text[0])
|
||||
else:
|
||||
raise AssertionError("setText got called with invalid text "
|
||||
"'{}'!".format(text))
|
||||
super().setText(text)
|
||||
|
Loading…
Reference in New Issue
Block a user