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

View File

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