Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
A way to write type-agnostic script/function runnables
- X-seq: zsh-users 24464
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: A way to write type-agnostic script/function runnables
- Date: Mon, 25 Nov 2019 15:22:53 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=dIJLxcWOk0cZECJ+V0P9dlkuaKUX5KV+Dld9QhmU3iE=; b=WKsETvnea1jGyfWuPhR8wjc+IlBjfR1gt6ci4Qjt6Pl+7Zj1rEZdyR4MYQiX7LepT0 3CeFwz+g1XhbsnXf/mZTreJr7+49XFH1kw5WFcF0zc+QVKYrv77Mspec57dytlNCenMU nLudNyOFkibCMOLe3orWlVuirsA1dpOrDvo+Q0M9k5fisQdunY9/Od2d7k41iPW3pG3x ePKx1QwR9UaF5T83QgQPua0KVLJCTrY7KxYN9fT/CBV34xLGI5jdSx9LC/u227ckY1b8 7nguoeObOPSDfH0RnpUxDuwcHUBJtdpsx91xvHYJZRRIjcX9hhqahYFefbkueck4pADe 8S9w==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
Hello,
I thought that I share a way of writing runnable objects in such a way
that they can be both invoked as a script and as a function. The main
contribution is a concise method of getting the directory path where
the object is located:
# Run as script? ZSH_SCRIPT is a Zsh 5.3 addition
if [[ $0 != example-script || -n $ZSH_SCRIPT ]]; then
0=${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}
0=${${(M)0##/*}:-$PWD/$0}
# Such global variable is expected to be typeset'd -g
# in the plugin.zsh file. Here it's restored in case
# of the function being ran as a script
typeset MY_PLUGIN_DIR=${0:h}
fi
The code is also available at Zplugin's repo – it is being added as an
example object when creating a plugin with the `create' subcommand:
https://github.com/zdharma/zplugin/blob/master/doc/example-script
I wonder if one could get the directory where the function is being
located directly, without the global *_DIR parameter? Without use of
the $*trace parameters (I don't trust them;).
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
Messages sorted by:
Reverse Date,
Date,
Thread,
Author