More command splitting cleanup

This commit is contained in:
Florian Bruhin 2014-06-03 06:46:13 +02:00
parent 1d2d435231
commit 2eb26c96c4
2 changed files with 42 additions and 27 deletions

View File

@ -141,12 +141,42 @@ class CommandManager:
self._cmd = None self._cmd = None
self._args = [] self._args = []
def parse(self, text, aliases=True): def _get_alias(self, text, alias_no_args):
"""Get an alias from the config.
Args:
text: The text to parse.
alias_no_args: Whether to apply an alias if there are no arguments.
Return:
None if no alias was found.
The new command string if an alias was found.
"""
parts = text.strip().split(maxsplit=1)
try:
alias = config.get('aliases', parts[0])
except (config.NoOptionError, config.NoSectionError):
return None
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
if alias_no_args:
new_cmd = alias
else:
new_cmd = parts[0]
if text.endswith(' '):
new_cmd += ' '
return new_cmd
def parse(self, text, aliases=True, fallback=False, alias_no_args=True):
"""Split the commandline text into command and arguments. """Split the commandline text into command and arguments.
Args: Args:
text: Text to parse. text: Text to parse.
aliases: Whether to handle aliases. aliases: Whether to handle aliases.
fallback: Whether to do a fallback splitting when the command was
unknown.
alias_no_args: Whether to apply an alias if there are no arguments.
Raise: Raise:
NoSuchCommandError if a command wasn't found. NoSuchCommandError if a command wasn't found.
@ -159,24 +189,20 @@ class CommandManager:
raise NoSuchCommandError("No command given") raise NoSuchCommandError("No command given")
cmdstr = parts[0] cmdstr = parts[0]
if aliases: if aliases:
try: new_cmd = self._get_alias(text, alias_no_args)
alias = config.get('aliases', cmdstr) if new_cmd is not None:
except (config.NoOptionError, config.NoSectionError):
pass
else:
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
new_cmd = alias
if text.endswith(' '):
new_cmd += ' '
logger.debug("Re-parsing with '{}'.".format(new_cmd)) logger.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False) return self.parse(new_cmd, aliases=False)
try: try:
cmd = cmdutils.cmd_dict[cmdstr] cmd = cmdutils.cmd_dict[cmdstr]
except KeyError: except KeyError:
if fallback:
parts = text.split(' ')
if text.endswith(' '):
parts.append('')
return parts
else:
raise NoSuchCommandError('{}: no such command'.format(cmdstr)) raise NoSuchCommandError('{}: no such command'.format(cmdstr))
if len(parts) == 1: if len(parts) == 1:
args = [] args = []
elif cmd.split: elif cmd.split:

View File

@ -25,7 +25,6 @@ import qutebrowser.keyinput.modeman as modeman
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
from qutebrowser.widgets.misc import MinimalLineEdit from qutebrowser.widgets.misc import MinimalLineEdit
from qutebrowser.commands.managers import CommandManager from qutebrowser.commands.managers import CommandManager
from qutebrowser.commands.exceptions import NoSuchCommandError
from qutebrowser.keyinput.modeparsers import STARTCHARS from qutebrowser.keyinput.modeparsers import STARTCHARS
from qutebrowser.utils.log import completion as logger from qutebrowser.utils.log import completion as logger
from qutebrowser.models.cmdhistory import (History, HistoryEmptyError, from qutebrowser.models.cmdhistory import (History, HistoryEmptyError,
@ -99,25 +98,15 @@ class Command(MinimalLineEdit):
@property @property
def parts(self): def parts(self):
"""Property to get the text split up in parts.""" """Property to get the text split up in parts."""
text = self.text() text = self.text()[len(self.prefix):]
if not text: if not text:
return [] return []
text = text[len(self.prefix):]
logger.debug("Splitting '{}'".format(text)) logger.debug("Splitting '{}'".format(text))
manager = CommandManager() manager = CommandManager()
original_cmd = text.strip().split(maxsplit=1) parts = manager.parse(text, fallback=True, alias_no_args=False)
try:
parts = manager.parse(text)
except NoSuchCommandError:
parts = text.split(' ')
if text.endswith(' '):
parts.append('')
logger.debug("Split parts: {}".format(parts)) logger.debug("Split parts: {}".format(parts))
if len(parts) == 1 and parts[0]:
parts = original_cmd
return parts return parts
@pyqtSlot() @pyqtSlot()
def _update_cursor_part(self): def _update_cursor_part(self):
"""Get the part index of the commandline where the cursor is over.""" """Get the part index of the commandline where the cursor is over."""