Add title heading support to `help`
This commit is contained in:
parent
6f9e6000b5
commit
33df3371f9
51
command
51
command
|
@ -18,17 +18,18 @@ EXE="${0##*/}"
|
||||||
declare -A help # associative arrays *require* declaration
|
declare -A help # associative arrays *require* declaration
|
||||||
|
|
||||||
help[main]='
|
help[main]='
|
||||||
This snippet contains the scaffolding for Bash tab completion using
|
# Sample Bash Template Command
|
||||||
the complete -C foo foo variation which allows scripts to complete
|
|
||||||
|
This `command` contains the scaffolding for Bash tab completion using
|
||||||
|
the `complete -C foo foo` variation which allows scripts to complete
|
||||||
themselves (rather than having another script somewhere to manage). To
|
themselves (rather than having another script somewhere to manage). To
|
||||||
use it simply add a function with the additional command and add the
|
use it simply add a function with the additional command and add the
|
||||||
name of it to the commands array declaration at the top of the script.
|
name of it to the commands array declaration at the top of the script.
|
||||||
Then add complete -C foo foo (or something like it) to your bashrc.
|
Then add `complete -C foo foo` to your bashrc. Begin functions with
|
||||||
Begin functions with x_ to allow useful command names to be used that
|
`command_` to allow useful command names to be used that would otherwise
|
||||||
would otherwise conflict with existing system and bash keywords. Begin
|
conflict with existing system and bash keywords. Begin functions with
|
||||||
functions with x__ when you do not want them to appear with tab
|
`command__` when you do not want them to appear with tab completion, but
|
||||||
completion, but still want them to be avaiable, just hidden.
|
still want them to be available, just hidden.'
|
||||||
'
|
|
||||||
|
|
||||||
help[foo]='The `foo` command foos.'
|
help[foo]='The `foo` command foos.'
|
||||||
|
|
||||||
|
@ -62,6 +63,8 @@ command_usage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
help[help]='
|
help[help]='
|
||||||
|
# The `help` Command
|
||||||
|
|
||||||
The `help` command prints help information. If no argument is passed
|
The `help` command prints help information. If no argument is passed
|
||||||
displays general help information (main). Otherwise, the documentation
|
displays general help information (main). Otherwise, the documentation
|
||||||
for the specific argument keyword is displayed, which usually
|
for the specific argument keyword is displayed, which usually
|
||||||
|
@ -75,29 +78,49 @@ command_help() {
|
||||||
if [[ -z "$name" ]];then
|
if [[ -z "$name" ]];then
|
||||||
for c in "${COMMANDS[@]}";do
|
for c in "${COMMANDS[@]}";do
|
||||||
[[ ${c:0:1} = _ ]] && continue;
|
[[ ${c:0:1} = _ ]] && continue;
|
||||||
command_help "$c" buildonly
|
command_help "$c" buildonly || true
|
||||||
done
|
done
|
||||||
command_help main
|
command_help main
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
local title="$EXE $name"
|
local title own body
|
||||||
[[ $name = main ]] && title="$EXE"
|
title=$(_help_title "$name")
|
||||||
|
if [[ -z "$title" ]]; then
|
||||||
|
body="${help[$name]}"
|
||||||
|
title="$EXE $name"
|
||||||
|
[[ $name = main ]] && title="$EXE"
|
||||||
|
else
|
||||||
|
body="${help[$name]}"
|
||||||
|
local eol=$'\n'
|
||||||
|
body=${body#*$title}
|
||||||
|
fi
|
||||||
local file="/tmp/help-$EXE-$name.html"
|
local file="/tmp/help-$EXE-$name.html"
|
||||||
if _have pandoc ; then
|
if _have pandoc ; then
|
||||||
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
||||||
pandoc -s --metadata title="$title" \
|
pandoc -s --metadata title="$title" \
|
||||||
-o "$file" <<< "${help[$name]}"
|
-o "$file" <<< "$body"
|
||||||
[[ -z "$2" ]] && cd /tmp && exec "$HELP_BROWSER" "$file"
|
[[ -z "$2" ]] && cd /tmp && exec "$HELP_BROWSER" "$file"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
pandoc -s --metadata title="$title" \
|
pandoc -s --metadata title="$title" \
|
||||||
-t plain <<< "${help[$name]}" | "$PAGER"
|
-t plain <<< "$body" | "$PAGER"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
echo "${help[$name]}" | "$PAGER"
|
echo -e "$title\n\n$body" | "$PAGER"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --------------------- completion and delegation --------------------
|
# --------------------- completion and delegation --------------------
|
||||||
|
# (better than . <(foo bloated_completion) in .bashrc)
|
||||||
|
|
||||||
|
_help_title() {
|
||||||
|
_filter "$@" && return $?;
|
||||||
|
local name="$1"
|
||||||
|
while IFS= read -r line; do
|
||||||
|
[[ $line =~ ^[:space]*$ ]] && continue
|
||||||
|
[[ $line =~ ^#\ (.+) ]] && echo "${BASH_REMATCH[1]}" && return 0
|
||||||
|
return 1
|
||||||
|
done <<< "${help[$name]}"
|
||||||
|
}
|
||||||
|
|
||||||
_have(){ type "$1" &>/dev/null; }
|
_have(){ type "$1" &>/dev/null; }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue