Use a validator function for ConsoleLineEdit.
The prompts provided (e.g. sys.ps1/sys.ps2) could change during the lifetime of the validator.
This commit is contained in:
parent
73214836da
commit
9c69ea3c45
@ -43,7 +43,12 @@ class ConsoleLineEdit(CommandLineEdit):
|
||||
sys.ps1 = '>>> '
|
||||
if not hasattr(sys, 'ps2'):
|
||||
sys.ps2 = '... '
|
||||
super().__init__(parent, prompts=[sys.ps1, 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)
|
||||
self.setFont(config.get('fonts', 'debug-console'))
|
||||
self._more = False
|
||||
self._buffer = []
|
||||
|
@ -51,10 +51,15 @@ class CommandLineEdit(QLineEdit):
|
||||
_validator: The current command validator.
|
||||
"""
|
||||
|
||||
def __init__(self, parent, prompts):
|
||||
def __init__(self, parent, validator):
|
||||
"""Constructor.
|
||||
|
||||
Args:
|
||||
validator: A function which checks if a given input is valid.
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.history = History()
|
||||
self._validator = _CommandValidator(prompts, parent=self)
|
||||
self._validator = _CommandValidator(validator, parent=self)
|
||||
self.setValidator(self._validator)
|
||||
|
||||
def __repr__(self):
|
||||
@ -65,9 +70,9 @@ class _CommandValidator(QValidator):
|
||||
|
||||
"""Validator to prevent the : from getting deleted."""
|
||||
|
||||
def __init__(self, prompts, parent=None):
|
||||
def __init__(self, validator, parent=None):
|
||||
super().__init__(parent)
|
||||
self.prompts = prompts
|
||||
self.validator = validator
|
||||
|
||||
def validate(self, string, pos):
|
||||
"""Override QValidator::validate.
|
||||
@ -79,7 +84,7 @@ class _CommandValidator(QValidator):
|
||||
Return:
|
||||
A tuple (status, string, pos) as a QValidator should.
|
||||
"""
|
||||
if any(string.startswith(c) for c in self.prompts):
|
||||
if self.validator(string):
|
||||
return (QValidator.Acceptable, string, pos)
|
||||
else:
|
||||
return (QValidator.Invalid, string, pos)
|
||||
|
@ -77,7 +77,12 @@ class Command(MinimalLineEditMixin, CommandLineEdit):
|
||||
# for a possible fix.
|
||||
|
||||
def __init__(self, parent=None):
|
||||
CommandLineEdit.__init__(self, parent, STARTCHARS)
|
||||
|
||||
def validator(text):
|
||||
"""Check if a given input is valid."""
|
||||
return any(text.startswith(p) for p in STARTCHARS)
|
||||
|
||||
CommandLineEdit.__init__(self, parent, validator)
|
||||
MinimalLineEditMixin.__init__(self)
|
||||
self.cursor_part = 0
|
||||
self.history.history = QApplication.instance().cmd_history.data
|
||||
|
Loading…
Reference in New Issue
Block a user