aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-01-25 16:30:16 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-01-25 16:32:20 +1300
commitae16b1077311c228048f040f93977d3ae862340b (patch)
tree978fb422893c0237d37506f4d1ed42179db0fc65
parentAvoid a fork in options detection (diff)
downloaddotfiles-ae16b1077311c228048f040f93977d3ae862340b.tar.gz
dotfiles-ae16b1077311c228048f040f93977d3ae862340b.zip
Add "pop" and "shift" methods to path()
-rw-r--r--IDEAS.markdown1
-rw-r--r--bash/bash_completion.d/path.bash2
-rw-r--r--sh/shrc.d/path.sh38
3 files changed, 39 insertions, 2 deletions
diff --git a/IDEAS.markdown b/IDEAS.markdown
index 39d9511e..25b79516 100644
--- a/IDEAS.markdown
+++ b/IDEAS.markdown
@@ -8,5 +8,4 @@ Ideas
manageable
* Have eds(1df) accept stdin with the "starting content" for the script
* Convert all the manual pages to mandoc maybe? <https://en.wikipedia.org/wiki/Mandoc>
-* Add "pop" and "shift" methods to path()
* edio(1df), like vipe(1)
diff --git a/bash/bash_completion.d/path.bash b/bash/bash_completion.d/path.bash
index a65e10ce..ba2dcb79 100644
--- a/bash/bash_completion.d/path.bash
+++ b/bash/bash_completion.d/path.bash
@@ -6,7 +6,7 @@ _path() {
# Complete operation as first word
local cmd
- for cmd in list insert append remove check help ; do
+ for cmd in list insert append remove shift pop check help ; do
[[ $cmd == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
COMPREPLY[${#COMPREPLY[@]}]=$cmd
done
diff --git a/sh/shrc.d/path.sh b/sh/shrc.d/path.sh
index 23eec75b..2759e11f 100644
--- a/sh/shrc.d/path.sh
+++ b/sh/shrc.d/path.sh
@@ -79,6 +79,40 @@ path() {
PATH=${PATH%%:}
;;
+ # Remove the first directory in $PATH
+ shift)
+ case $PATH in
+ '')
+ printf >&2 'path(): %s: PATH is empty!\n' "$@"
+ return 1
+ ;;
+ *:*)
+ PATH=${PATH#*:}
+ ;;
+ *)
+ # shellcheck disable=SC2123
+ PATH=
+ ;;
+ esac
+ ;;
+
+ # Remove the last directory in $PATH
+ pop)
+ case $PATH in
+ '')
+ printf >&2 'path(): %s: PATH is empty!\n' "$@"
+ return 1
+ ;;
+ *:*)
+ PATH=${PATH%:*}
+ ;;
+ *)
+ # shellcheck disable=SC2123
+ PATH=
+ ;;
+ esac
+ ;;
+
# Check whether a directory is in PATH
check)
path _argcheck "$@" || return
@@ -103,6 +137,10 @@ USAGE:
Add directory DIR (default $PWD) to the end of PATH
path remove [DIR]
Remove directory DIR (default $PWD) from PATH
+ path shift
+ Remove the first directory from PATH
+ path pop
+ Remove the last directory from PATH
path check [DIR]
Return whether directory DIR (default $PWD) is a component of PATH
path help