Skip to content

Commit

Permalink
Merge pull request #41 from sloria/alias
Browse files Browse the repository at this point in the history
Make 'alias' commands work
  • Loading branch information
sloria committed May 13, 2018
2 parents 4cbbe38 + 61fb031 commit 9f2e4ba
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Features:
* Add "Did you mean" suggestions.
* Support setting environment variables with ``export`` commands
(:issue:`32`). Thanks :user:`asmacdo` for the suggestion.
* Support setting aliases with ``alias`` commands (:issue:`40`).

Bug fixes:

Expand Down
18 changes: 12 additions & 6 deletions doitlive/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ class SessionState(dict):
TRUTHY = set(['true', 'yes', '1'])

def __init__(self, shell, prompt_template, speed,
aliases=None, envvars=None,
aliases=None, envvars=None, extra_commands=None,
test_mode=False, commentecho=False):
aliases = aliases or []
envvars = envvars or []
extra_commands = extra_commands or []
dict.__init__(self, shell=shell, prompt_template=prompt_template,
speed=speed, aliases=aliases, envvars=envvars,
extra_commands=extra_commands,
test_mode=test_mode, commentecho=commentecho)

def add_alias(self, alias):
Expand All @@ -59,6 +61,9 @@ def add_alias(self, alias):
def add_envvar(self, envvar):
self['envvars'].append(envvar)

def add_command(self, command):
self['extra_commands'].append(command)

def set_speed(self, speed):
self['speed'] = int(speed)

Expand Down Expand Up @@ -137,14 +142,15 @@ def run(commands, shell=None, prompt_template='default', speed=1,
comment = command.lstrip("#")
secho(comment, fg='yellow', bold=True)
continue
# Handle 'export' commands by adding envvars to SessionState
elif command_as_list and command_as_list[0] == 'export':
# Handle 'export' and 'alias' commands by storing them in SessionState
elif command_as_list and command_as_list[0] in ['alias', 'export']:
magictype(command,
prompt_template=state['prompt_template'],
speed=state['speed'])
for envvar in command_as_list[1:]:
state.add_envvar(envvar)
# Handle ```python and ```ipython by running "player" shells
# Store the raw commands instead of using add_envvar and add_alias
# to avoid having to parse the command ourselves
state.add_command(command)
# Handle ```python and ```ipython by running "player" consoles
elif shell_match:
shell_name = shell_match.groups()[0].strip()
py_commands = []
Expand Down
10 changes: 7 additions & 3 deletions doitlive/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def write_commands(fp, command, args):
return None


def run_command(cmd, shell=None, aliases=None, envvars=None, test_mode=False):
def run_command(cmd, shell=None, aliases=None, envvars=None, extra_commands=None, test_mode=False):
shell = shell or get_default_shell()
command_as_list = shlex.split(ensure_utf8(cmd))
if len(command_as_list) and command_as_list[0] == 'cd':
Expand Down Expand Up @@ -93,6 +93,10 @@ def run_command(cmd, shell=None, aliases=None, envvars=None, test_mode=False):
# Write envvars and aliases
write_commands(fp, 'export', envvars)
write_commands(fp, 'alias', aliases)
if extra_commands:
for command in extra_commands:
line = '{}\n'.format(command)
fp.write(ensure_utf8(line))

cmd_line = cmd + '\n'
fp.write(ensure_utf8(cmd_line))
Expand All @@ -108,10 +112,10 @@ def run_command(cmd, shell=None, aliases=None, envvars=None, test_mode=False):


def magicrun(text, shell, prompt_template='default', aliases=None,
envvars=None, speed=1, test_mode=False, commentecho=False):
envvars=None, extra_commands=None, speed=1, test_mode=False, commentecho=False):
"""Echo out each character in ``text`` as keyboard characters are pressed,
wait for a RETURN keypress, then run the ``text`` in a shell context.
"""
magictype(text, prompt_template, speed)
run_command(text, shell, aliases=aliases, envvars=envvars,
test_mode=test_mode)
extra_commands=extra_commands, test_mode=test_mode)
3 changes: 1 addition & 2 deletions tests/sessions/alias.session
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#doitlive alias: foo="echo 42"

alias foo="echo $((41+1))"
foo
3 changes: 3 additions & 0 deletions tests/sessions/alias_comment.session
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#doitlive alias: foo="echo 42"

foo
13 changes: 11 additions & 2 deletions tests/test_doitlive.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_python_session(self, runner):

def test_alias(self, runner):
user_input = random_string(len('foo'))
result = run_session(runner, 'alias.session', user_input)
result = run_session(runner, 'alias_comment.session', user_input)
assert result.exit_code == 0
assert '42' in result.output

Expand All @@ -155,7 +155,7 @@ def test_unset_envvar(self, runner):
result = run_session(runner, 'unset.session', user_input)
assert 'fortytwo' not in result.output

def test_export(self, runner):
def test_export_sets_envvar(self, runner):
user_input = ''.join([
random_string(len('export NAME=Steve')),
'\n',
Expand All @@ -164,6 +164,15 @@ def test_export(self, runner):
result = run_session(runner, 'export.session', user_input)
assert 'Hello Steve' in result.output

def test_alias_sets_alias(self, runner):
user_input = ''.join([
random_string(len('alias foo="echo $((41+1))"')),
'\n',
random_string(len('foo'))
])
result = run_session(runner, 'alias.session', user_input)
assert '42' in result.output


def test_themes_list(runner):
result1 = runner.invoke(cli, ['themes'])
Expand Down

0 comments on commit 9f2e4ba

Please sign in to comment.