From 5d74548f218cc0de610109bbb7162b973180d344 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 28 Mar 2023 23:24:29 +0000 Subject: [PATCH] [zsh] Make prepend_to_path and append_to_path more interesting Add a +e/-e argument (parsed with getopt) that conditionally exports $path. --- zsh/func/append_to_path | 30 +++++++++++++++++++++++------- zsh/func/prepend_to_path | 30 +++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/zsh/func/append_to_path b/zsh/func/append_to_path index 3c21108..1940855 100644 --- a/zsh/func/append_to_path +++ b/zsh/func/append_to_path @@ -1,13 +1,29 @@ -#!/usr/bin/env zsh -# vim:ft=zsh: # Eryn Wells +# vim:ft=zsh: -function append_to_path -{ - if [[ -d "$1" ]]; then - path+="$1" - export path +append_to_path() { + local should_export_path=1 + + while getopts "e" opt; do + case $opt in + "e") should_export_path=1;; + "+e") should_export_path=0;; + *) >&2 echo "$0: unknown option '$opt'" + esac + done + + local path_to_add=$@[$OPTIND] + if [[ -d "$path_to_add" ]]; then + path+="$path_to_add" + + if (( $should_export_path )); then + export path + fi + + return 1 fi + + return 0 } append_to_path "$@" diff --git a/zsh/func/prepend_to_path b/zsh/func/prepend_to_path index 2d2aed4..43268e9 100644 --- a/zsh/func/prepend_to_path +++ b/zsh/func/prepend_to_path @@ -1,13 +1,29 @@ -#!/usr/bin/env zsh -# vim:ft=zsh: # Eryn Wells +# vim:ft=zsh: -function prepend_to_path -{ - if [[ -d "$1" ]]; then - path=("$1" $path) - export path +prepend_to_path() { + local should_export_path=1 + + while getopts "e" opt; do + case $opt in + "e") should_export_path=1;; + "+e") should_export_path=0;; + *) >&2 echo "$0: unknown option '$opt'" + esac + done + + local path_to_add=$@[$OPTIND] + if [[ -d "$path_to_add" ]]; then + path=("$path_to_add" $path) + + if (( $should_export_path )); then + export path + fi + + return 0 fi + + return 1 } prepend_to_path "$@"