From b05a5d4bf5dab402e1eb6547c98d839afb65bf1c Mon Sep 17 00:00:00 2001 From: Nathan Hoad Date: Sat, 22 Apr 2017 10:52:34 +1000 Subject: [PATCH] Implement an alias alias, matching what sh's alias does. --- news/alias-command.rst | 13 +++++++++++++ xontrib/bashisms.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 news/alias-command.rst diff --git a/news/alias-command.rst b/news/alias-command.rst new file mode 100644 index 000000000..1e8d52264 --- /dev/null +++ b/news/alias-command.rst @@ -0,0 +1,13 @@ +**Added:** + +* Added an alias command, matching bash's implementation, available as part of bashisms. + +**Changed:** None + +**Deprecated:** None + +**Removed:** None + +**Fixed:** None + +**Security:** None diff --git a/xontrib/bashisms.py b/xontrib/bashisms.py index 74ad5d4cb..269bdbddb 100644 --- a/xontrib/bashisms.py +++ b/xontrib/bashisms.py @@ -1,7 +1,12 @@ """Bash-like interface extensions for xonsh.""" +import shlex +import sys + from prompt_toolkit.keys import Keys from prompt_toolkit.filters import Condition, EmacsInsertMode, ViInsertMode +__all__ = () + @events.on_transform_command def bash_preproc(cmd, **kw): @@ -26,3 +31,28 @@ def custom_keybindings(bindings, **kw): def recall_last_arg(event): arg = __xonsh_history__[-1].cmd.split()[-1] event.current_buffer.insert_text(arg) + + +def alias(args, stdin=None): + ret = 0 + + if args: + for arg in args: + if '=' in arg: + # shlex.split to remove quotes, e.g. "foo='echo hey'" into + # "foo=echo hey" + name, cmd = shlex.split(arg)[0].split('=', 1) + aliases[name] = shlex.split(cmd) + elif arg in aliases: + print('{}={}'.format(arg, aliases[arg])) + else: + print("alias: {}: not found".format(arg), file=sys.stderr) + ret = 1 + else: + for alias, cmd in aliases.items(): + print('{}={}'.format(alias, cmd)) + + return ret + + +aliases['alias'] = alias