aboutsummaryrefslogtreecommitdiff
path: root/bin/han.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-29 17:18:44 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-29 17:18:44 +1200
commit9718a5b0ac6b3c242da37da13af900553354caa1 (patch)
treeec8824cfaaa36e4a257ac676b03474b3cb35c47a /bin/han.bash
parentAdd mapfile to completions (conditional on vers) (diff)
downloaddotfiles-9718a5b0ac6b3c242da37da13af900553354caa1.tar.gz
dotfiles-9718a5b0ac6b3c242da37da13af900553354caa1.zip
Move han(1) to be shebangged
Diffstat (limited to 'bin/han.bash')
-rw-r--r--bin/han.bash38
1 files changed, 38 insertions, 0 deletions
diff --git a/bin/han.bash b/bin/han.bash
new file mode 100644
index 00000000..8536b61b
--- /dev/null
+++ b/bin/han.bash
@@ -0,0 +1,38 @@
+# Abstract calls to Bash help vs man(1)
+self=han
+
+# Ensure we're using at least version 2.05. Weird arithmetic syntax needed here
+# due to leading zeroes and trailing letters in some 2.x version numbers (e.g.
+# 2.05a).
+# shellcheck disable=SC2128
+[ -n "$BASH_VERSINFO" ] || return
+((BASH_VERSINFO[0] == 2)) &&
+ ((10#${BASH_VERSINFO[1]%%[![:digit:]]*} < 5)) &&
+ return
+
+# Figure out the options with which we can call help; Bash >=4.0 has an -m
+# option which prints the help output in a man-page like format
+declare -a helpopts
+if ((BASH_VERSINFO[0] >= 4)) ; then
+ helpopts=(-m)
+fi
+
+# Create a temporary directory with name in $td, and a trap to remove it when
+# the script exits
+td=
+cleanup() {
+ [[ -n "$td" ]] && rm -fr -- "$td"
+}
+trap cleanup EXIT
+td=$(mktd "$self") || exit
+
+# If we have exactly one argument and a call to the help builtin with that
+# argument succeeds, display its output with `pager -s`
+if (($# == 1)) &&
+ help "${helpopts[@]}" "$1" >"$td"/"$1".help 2>/dev/null ; then
+ (cd -- "$td" && "$PAGER" -s -- "$1".help)
+
+# Otherwise, just pass all the arguments to man(1)
+else
+ man "$@"
+fi