Start using keep=True for commandline.

This commit is contained in:
Florian Bruhin 2014-11-06 07:15:02 +01:00
parent 76b72d3438
commit d730471fd7
2 changed files with 37 additions and 29 deletions

View File

@ -190,7 +190,8 @@ class CommandRunner(QObject):
new_cmd += ' ' new_cmd += ' '
return new_cmd return new_cmd
def parse(self, text, aliases=True, fallback=False, alias_no_args=True): def parse(self, text, aliases=True, fallback=False, alias_no_args=True,
keep=False):
"""Split the commandline text into command and arguments. """Split the commandline text into command and arguments.
Args: Args:
@ -199,18 +200,14 @@ class CommandRunner(QObject):
fallback: Whether to do a fallback splitting when the command was fallback: Whether to do a fallback splitting when the command was
unknown. unknown.
alias_no_args: Whether to apply an alias if there are no arguments. alias_no_args: Whether to apply an alias if there are no arguments.
keep: Whether to keep special chars and whitespace
Return: Return:
A split string commandline, e.g ['open', 'www.google.com'] A split string commandline, e.g ['open', 'www.google.com']
""" """
parts = text.strip().split(maxsplit=1) cmdstr, sep, argstr = text.partition(' ')
if not parts: if not cmdstr:
raise cmdexc.NoSuchCommandError("No command given") raise cmdexc.NoSuchCommandError("No command given")
elif len(parts) > 1:
cmdstr, argstr = parts
else:
cmdstr = parts[0]
argstr = None
if aliases: if aliases:
new_cmd = self._get_alias(text, alias_no_args) new_cmd = self._get_alias(text, alias_no_args)
if new_cmd is not None: if new_cmd is not None:
@ -220,25 +217,33 @@ class CommandRunner(QObject):
self._cmd = cmdutils.cmd_dict[cmdstr] self._cmd = cmdutils.cmd_dict[cmdstr]
except KeyError: except KeyError:
if fallback: if fallback:
parts = text.split(' ') cmdstr, sep, argstr = text.partition(' ')
if text.endswith(' '): return [cmdstr + sep] + argstr.split(' ')
parts.append('')
return parts
else: else:
raise cmdexc.NoSuchCommandError( raise cmdexc.NoSuchCommandError(
'{}: no such command'.format(cmdstr)) '{}: no such command'.format(cmdstr))
self._split_args(argstr) self._split_args(argstr, keep)
retargs = self._args[:] retargs = self._args[:]
if text.endswith(' '): if keep:
retargs.append('') cmd = [cmdstr + sep]
return [cmdstr] + retargs else:
cmd = [cmdstr]
return cmd + retargs
def _split_args(self, argstr): def _split_args(self, argstr, keep):
"""Split the arguments from an arg string.""" """Split the arguments from an arg string.
if argstr is None:
Args:
argstr: An argument string.
keep: Whether to keep special chars and whitespace
Return:
A list containing the splitted strings.
"""
if not argstr:
self._args = [] self._args = []
elif self._cmd.split: elif self._cmd.split:
self._args = split.split(argstr) self._args = split.split(argstr, keep=keep)
else: else:
# If split=False, we still want to split the flags, but not # If split=False, we still want to split the flags, but not
# everything after that. # everything after that.

View File

@ -87,8 +87,12 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
else: else:
return '' return ''
def split(self): def split(self, keep=False):
"""Get the text split up in parts.""" """Get the text split up in parts.
Args:
keep: Whether to keep special chars and whitespace.
"""
text = self.text()[len(self.prefix()):] text = self.text()[len(self.prefix()):]
if not text: if not text:
# When only ":" is entered, we already have one imaginary part, # When only ":" is entered, we already have one imaginary part,
@ -99,7 +103,8 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
# the whitespace. # the whitespace.
return [text] return [text]
runner = runners.CommandRunner(self._win_id) runner = runners.CommandRunner(self._win_id)
parts = runner.parse(text, fallback=True, alias_no_args=False) parts = runner.parse(text, fallback=True, alias_no_args=False,
keep=keep)
if self._empty_item_idx is not None: if self._empty_item_idx is not None:
log.completion.debug("Empty element queued at {}, " log.completion.debug("Empty element queued at {}, "
"inserting.".format(self._empty_item_idx)) "inserting.".format(self._empty_item_idx))
@ -117,7 +122,7 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
else: else:
spaces = False spaces = False
cursor_pos -= len(self.prefix()) cursor_pos -= len(self.prefix())
parts = self.split() parts = self.split(keep=True)
log.completion.vdebug( log.completion.vdebug(
"text: {}, parts: {}, cursor_pos after removing prefix '{}': " "text: {}, parts: {}, cursor_pos after removing prefix '{}': "
"{}".format(self.text(), parts, self.prefix(), cursor_pos)) "{}".format(self.text(), parts, self.prefix(), cursor_pos))
@ -135,12 +140,10 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
"{}".format(cursor_pos, len(part), i, "{}".format(cursor_pos, len(part), i,
self._empty_item_idx)) self._empty_item_idx))
break break
# FIXME are spaces always 1 char? cursor_pos -= len(part)
# https://github.com/The-Compiler/qutebrowser/issues/122
cursor_pos -= (len(part) + 1)
log.completion.vdebug( log.completion.vdebug(
"Removing len({!r}) + 1 -> {} from cursor_pos -> {}".format( "Removing len({!r}) -> {} from cursor_pos -> {}".format(
part, len(part) + 1, cursor_pos)) part, len(part), cursor_pos))
log.completion.debug("cursor_part {}, spaces {}".format( log.completion.debug("cursor_part {}, spaces {}".format(
self._cursor_part, spaces)) self._cursor_part, spaces))
return return