From 559df01edccf9ec3ae1dc1467089afd01403186b Mon Sep 17 00:00:00 2001 From: Andy Kipp Date: Wed, 14 Feb 2024 11:50:28 +0100 Subject: [PATCH] Update __init__.py --- xontrib/pipeliner/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xontrib/pipeliner/__init__.py b/xontrib/pipeliner/__init__.py index b6c8e14..f452afa 100644 --- a/xontrib/pipeliner/__init__.py +++ b/xontrib/pipeliner/__init__.py @@ -8,6 +8,11 @@ from xonsh.tools import print_color from xontrib_pipeliner_asttokens import asttokens +_default_presets = { + "strip": "line.strip()", + "split": lambda args: f"line.split({repr(args[0])})" +} + def _pl(args, stdin, stdout): err = False if len(args) == 0: @@ -18,6 +23,11 @@ def _pl(args, stdin, stdout): print('Example: echo "123" | pl "line[::-1]"', file=sys.stderr) return + presets = {**_default_presets, **__xonsh__.env.get('XONTRIB_PIPELINER_PRESETS', {})} + if args[0] in presets: + preset = presets[args[0]] + args = [preset(args[1:])] if callable(preset) else [preset] + fn = eval('lambda line, num:'+args[0], __xonsh__.ctx) if stdin is None: