zsh:funcs: Rename pgrep to psgrep

Instead of replacing pgrep, have this function under a slightly
different name.

Also add support for multiple arguments as the function does not aim to
be called under the same name now.
This commit is contained in:
2022-10-07 14:44:59 +02:00
parent 32109b5bab
commit 6a1537eda8

View File

@@ -482,31 +482,21 @@ _page_readme_chpwd_handler() {
done
}
# Improved pgrep
# I sometimes find `pgrep` not matching the processes I am searching for, but
# `ps aux | grep ...` did not disappoint yet.
pgrep() {
# Fall back to real `pgrep` if options are specified. I have no interest in
# emulating `pgrep` features with `ps` and other tools. This function is
# only meant for quick searches
if (( $# > 1 )); then
command pgrep "$@"
return
fi
psgrep() {
# - Set EXTENDED_GLOB for the `b` globbing flag.
# - Set UNSET so that no arguments can be specified leading to `grep ""`
# matching everything, as `ps aux` without pipe would be my desired
# behavior.
emulate -L zsh -o extendedglob -o unset
emulate -L zsh -o extendedglob
# Substitute the captured first character with itself surrounded by
# brackets. The `(#b)` turns on backreferences, storing the match in the
# array $match (in this case with only one element).
# So for example: "pattern" -> "[p]attern"
# This has the effect that the `grep` does not grep itself in the processes
# list.
ps aux | grep "${1/(#b)(?)/[$match]}"
for arg; do
# Substitute the captured first character with itself surrounded by
# brackets. The `(#b)` turns on backreferences, storing the match in the
# array $match (in this case with only one element).
# So for example: "pattern" -> "[p]attern"
# This has the effect that the `grep` does not grep itself in the processes
# list.
ps aux | grep "${arg/(#b)(?)/[$match]}"
done
}
# Use shellcheck.net if shellcheck is not installed.