aboutsummaryrefslogtreecommitdiff
path: root/sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-03-29 15:27:56 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-03-29 15:27:56 +1300
commitfb2b15e272e7c29348f1d23bd717d52cd6b69660 (patch)
treea2c9a933d7e9a8cce6d68d59b6612216a357a0b9 /sh
parent4686614813ae6a8cadcfa52e136ba00ac5a693dd (diff)
parent91aa4123f4c90b0176682a80e9dfe7d9b90bf553 (diff)
downloaddotfiles-fb2b15e272e7c29348f1d23bd717d52cd6b69660.tar.gz
Merge branch 'master' into port/bsd/freebsd
Diffstat (limited to 'sh')
-rw-r--r--sh/shrc.d/gt.sh28
-rw-r--r--sh/shrc.d/lgt.sh28
-rw-r--r--sh/shrc.d/path.sh1
3 files changed, 56 insertions, 1 deletions
diff --git a/sh/shrc.d/gt.sh b/sh/shrc.d/gt.sh
new file mode 100644
index 00000000..d18a4ab8
--- /dev/null
+++ b/sh/shrc.d/gt.sh
@@ -0,0 +1,28 @@
+# If the argument is a directory, change to it. If it's a file, change to its
+# parent. Stands for "get to".
+gt() {
+
+ # Check argument count
+ if [ "$#" -gt 1 ] ; then
+ printf >&2 'gd(): Too many arguments\n'
+ return 2
+ fi
+
+ # Strip trailing slash
+ set -- "${1%/}"
+
+ # If target doesn't have a leading slash, add PWD prefix
+ case $1 in
+ /*) ;;
+ *) set -- "${PWD%/}"/"$1"
+ esac
+
+ # If target isn't a directory, chop to its parent
+ [ -d "$1" ] || set -- "${1%/*}"
+
+ # If target is now empty, go to the root
+ [ -n "$1" ] || set -- /
+
+ # Try to change into the determined directory
+ command cd -- "$@"
+}
diff --git a/sh/shrc.d/lgt.sh b/sh/shrc.d/lgt.sh
new file mode 100644
index 00000000..fbe43369
--- /dev/null
+++ b/sh/shrc.d/lgt.sh
@@ -0,0 +1,28 @@
+# Run loc(1df) with given arguments and then run gt() to get to the first
+# argument found
+lgt() {
+
+ # Check argument count
+ if [ "$#" -eq 0 ] ; then
+ printf >&2 'lgt(): Need a search term\n'
+ return 2
+ fi
+
+ # Change the positional parameters from the loc(1df) arguments to the first
+ # result with a trailing slash
+ set -- "$(
+ loc "$@" | {
+ IFS= read -r target
+ printf '%s/' "$target"
+ }
+ )"
+
+ # Strip the trailing slash
+ set -- "${1%/}"
+
+ # If the subshell printed nothing, return with failure
+ [ -n "$1" ] || return
+
+ # Run gt() with the new arguments
+ gt "$@"
+}
diff --git a/sh/shrc.d/path.sh b/sh/shrc.d/path.sh
index 79b48fd5..b6b1820f 100644
--- a/sh/shrc.d/path.sh
+++ b/sh/shrc.d/path.sh
@@ -6,7 +6,6 @@ path() {
# List current directories in PATH
list|'') (
- # shellcheck disable=SC2030
path=$PATH:
while [ -n "$path" ] ; do
dir=${path%%:*}