Awesome functions that make writing Z shell CLI applications easier.
To install this library for the current user run:
/bin/zsh -c "$(curl -fsSL https://raw.githubusercontent.com/astzweig/zshlib/main/bootstrap.sh)"
To install this library system wide run that as root.
You can also just download the latest release and put in in your $fpath
.
To use this library in you own project add it as a git-submodule
to your repository.
# Run inside your project directory
git submodule add https://github.com/astzweig/zshlib
Just add the zshlib directory to the beginning of your PATH
variable and you
can call the commands from the command line. Alternatively you can enter the
whole path e.g. /path/to/repo/hio --help
.
Caveat: Remember that
sudo
does not inherit all environment variables. To pass your modifiedPATH
variable runsudo --preserve-env=PATH
.
Just source the specific commands that you want inside your script. Alternatively you can enable Zsh autoload:
# This will register all commands to autoload
FPATH="/path/to/zshlib:${FPATH}"
local funcNames=("${(@f)$(find ./zshlib -type f -perm +u=x | awk -F/ '{ print $NF }')}")
autoload -Uz "${funcNames[@]}"
Or if you want to autoload only specific commands:
# This will register only hio and getPrefDir to autoload
FPATH="/path/to/zshlib:${FPATH}"
autoload -Uz hio getPrefDir
name | description | dependencies | supported platforms |
---|---|---|---|
pf | Convenience function for highlighted output, combining print and tput. | tput , docopts 1 |
all |
hio | Highlighted output for predefined text styles for pf . |
pf 2, isTerminalBackgroundDark 2, docopts 1 |
all |
lop | Log messages to syslog, file or print to command line with highlight. | hio 2, pf 2, isTerminalBackgroundDark 2, docopts 1 |
all |
indicateActivity | Show a spinner during run of a function if output is to a terminal else print three dots and done or failed afterwards. | lop 2, hio 2, pf 2, isTerminalBackgroundDark 2, docopts 1 |
all |
trim | Remove leading and trailing whitespace from string. | docopts 1 |
all |
loadModules | Find executable modules in module search paths and allow user to filter or inverse the list. | docopts 1 |
all |
askUserModuleQuestions | Ask user questions on behalf of modules with possibility to cache (or store) answers for future calls, e.g. using config. | docopts 1 |
all |
showSpinner | Show a loading animation during execution of a function. | traps 2, docopts 1 |
all |
askUser | Ask user for input. Supports questions, password, selections or confirmations. | hio 2, traps 2, docopts 1 |
all |
getPrefDir | Get system specific preferences directory. | docopts 1 |
macOS, Linux, Windows Subsystem for Linux |
traps | A trap handler for zsh scripts. | docopts 1 |
macOS, Linux, Windows Subsystem for Linux |
config | Config file writer and reader. | getPrefDir 2, PlistBuddy 3, docopts 1 |
macOS, Linux, Windows Subsystem for Linux |
abbreviatePaths | Truncate the passed paths so that they are minimal in length and pairwise distinct. Useful to save the user long path specifications. | docopts 1 |
all |
isTerminalBackgroundDark | Queries the terminal for its background color and returns zero if the color is rather dark or 1 otherwise. If the output is not connected to a terminal, code 10 will be returned. | traps 2 |
all |
Run any command with the --help
option to read about its usage. e.g.
/path/to/zshlib/hio --help
.
Footnotes
-
docopts
with-f, --function
option needed. See the Astzweig fork for example. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 -
A command of Zsh Lib (this library). ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14
-
/usr/libexec/PlistBuddy
is a utility found on macOS systems. Apple does not provide any documentation besides the man page and the programs--help
output. ↩