Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias multi commands #1577

Merged
merged 4 commits into from Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 13 additions & 15 deletions qutebrowser/commands/runners.py
Expand Up @@ -26,7 +26,7 @@

from qutebrowser.config import config, configexc
from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.utils import message, log, objreg, qtutils
from qutebrowser.utils import message, objreg, qtutils
from qutebrowser.misc import split


Expand Down Expand Up @@ -80,21 +80,23 @@ def __init__(self, win_id, partial_match=False, parent=None):
self._partial_match = partial_match
self._win_id = win_id

def _get_alias(self, text):
def _get_alias(self, text, default=None):
"""Get an alias from the config.

Args:
text: The text to parse.
default : Default value to return when alias was not found. By
default it is set to None.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: No point in stating this here as there's already default=None above 😉 I'll fix it up before merging unless other things come up while reviewing.


Return:
None if no alias was found.
The new command string if an alias was found.
The new command string if an alias was found. Default value
otherwise.
"""
parts = text.strip().split(maxsplit=1)
try:
alias = config.get('aliases', parts[0])
except (configexc.NoOptionError, configexc.NoSectionError):
return None
return default
try:
new_cmd = '{} {}'.format(alias, parts[1])
except IndexError:
Expand All @@ -103,19 +105,23 @@ def _get_alias(self, text):
new_cmd += ' '
return new_cmd

def parse_all(self, text, *args, **kwargs):
def parse_all(self, text, aliases=True, *args, **kwargs):
"""Split a command on ;; and parse all parts.

If the first command in the commandline is a non-split one, it only
returns that.

Args:
text: Text to parse.
aliases: Whether to handle aliases.
*args/**kwargs: Passed to parse().

Yields:
ParseResult tuples.
"""
if aliases:
text = self._get_alias(text, text)

if ';;' in text:
# Get the first command and check if it doesn't want to have ;;
# split.
Expand Down Expand Up @@ -159,12 +165,11 @@ def _parse_fallback(self, text, count, keep):
cmdline = text.split()
return ParseResult(cmd=None, args=None, cmdline=cmdline, count=count)

def parse(self, text, *, aliases=True, fallback=False, keep=False):
def parse(self, text, *, fallback=False, keep=False):
"""Split the commandline text into command and arguments.

Args:
text: Text to parse.
aliases: Whether to handle aliases.
fallback: Whether to do a fallback splitting when the command was
unknown.
keep: Whether to keep special chars and whitespace
Expand All @@ -178,13 +183,6 @@ def parse(self, text, *, aliases=True, fallback=False, keep=False):
if not cmdstr and not fallback:
raise cmdexc.NoSuchCommandError("No command given")

if aliases:
new_cmd = self._get_alias(text)
if new_cmd is not None:
log.commands.debug("Re-parsing with '{}'.".format(new_cmd))
return self.parse(new_cmd, aliases=False, fallback=fallback,
keep=keep)

if self._partial_match:
cmdstr = self._completion_match(cmdstr)

Expand Down
4 changes: 2 additions & 2 deletions qutebrowser/completion/completer.py
Expand Up @@ -354,7 +354,7 @@ def update_completion(self):
if completion.enabled:
completion.show()

def split(self, keep=False, aliases=False):
def split(self, keep=False):
"""Get the text split up in parts.

Args:
Expand All @@ -371,7 +371,7 @@ def split(self, keep=False, aliases=False):
# the whitespace.
return [text]
runner = runners.CommandRunner(self._win_id)
result = runner.parse(text, fallback=True, aliases=aliases, keep=keep)
result = runner.parse(text, fallback=True, keep=keep)
parts = result.cmdline
if self._empty_item_idx is not None:
log.completion.debug("Empty element queued at {}, "
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/commands/test_runners.py
Expand Up @@ -43,10 +43,20 @@ def test_parse_all(self, cmdline_test):
with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all(cmdline_test.cmd, aliases=False))

def test_parse_all_with_alias(self, cmdline_test, config_stub):
config_stub.data = {'aliases': {'alias_name': cmdline_test.cmd}}

cr = runners.CommandRunner(0)
if cmdline_test.valid:
assert len(list(cr.parse_all("alias_name"))) > 0
else:
with pytest.raises(cmdexc.NoSuchCommandError):
list(cr.parse_all("alias_name"))

def test_parse_with_count(self):
"""Test parsing of commands with a count."""
cr = runners.CommandRunner(0)
result = cr.parse('20:scroll down', aliases=False)
result = cr.parse('20:scroll down')
assert result.cmd.name == 'scroll'
assert result.count == 20
assert result.args == ['down']
Expand All @@ -58,5 +68,5 @@ def test_partial_parsing(self):
The same with it being disabled is tested by test_parse_all.
"""
cr = runners.CommandRunner(0, partial_match=True)
result = cr.parse('message-i', aliases=False)
result = cr.parse('message-i')
assert result.cmd.name == 'message-info'
2 changes: 1 addition & 1 deletion tests/unit/config/test_config.py
Expand Up @@ -306,7 +306,7 @@ def test_default_key_config(self):
runner = runners.CommandRunner(win_id=0)
for sectname in configdata.KEY_DATA:
for cmd in conf.get_bindings_for(sectname).values():
runner.parse(cmd, aliases=False)
runner.parse(cmd)

def test_upgrade_version(self):
"""Fail when the qutebrowser version changed.
Expand Down