aboutsummaryrefslogtreecommitdiff
path: root/libexec/recit-help
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2022-07-14 03:40:24 +0000
committerJulio Capote <jcapote@gmail.com>2022-07-14 03:40:24 +0000
commit891f946e86f8a0911f67983d52fccacc9f8ebbad (patch)
tree36b032deb891f4adf5c047b255eb0ca6da275fef /libexec/recit-help
downloadrecit-891f946e86f8a0911f67983d52fccacc9f8ebbad.tar.gz
initial
Diffstat (limited to 'libexec/recit-help')
-rwxr-xr-xlibexec/recit-help104
1 files changed, 104 insertions, 0 deletions
diff --git a/libexec/recit-help b/libexec/recit-help
new file mode 100755
index 0000000..1c6371b
--- /dev/null
+++ b/libexec/recit-help
@@ -0,0 +1,104 @@
+#!/usr/bin/env bash
+set -e
+
+print_summaries() {
+ local commands=()
+ local summaries=()
+ local longest_command=0
+ local command
+
+ for command in $(recit-commands); do
+ local file="$(command_path "$command")"
+ if [ ! -h "$file" ]; then
+ local summary="$(summary "$file")"
+ if [ -n "$summary" ]; then
+ commands["${#commands[@]}"]="$command"
+ summaries["${#summaries[@]}"]="$summary"
+
+ if [ "${#command}" -gt "$longest_command" ]; then
+ longest_command="${#command}"
+ fi
+ fi
+ fi
+ done
+
+ local index
+ local columns="$(tput cols)"
+ local summary_length=$(( $columns - $longest_command - 5 ))
+
+ for (( index=0; index < ${#commands[@]}; index++ )); do
+ printf " %-${longest_command}s %s\n" "${commands[$index]}" \
+ "$(truncate "$summary_length" "${summaries[$index]}")"
+ done
+}
+
+print_help() {
+ local file="$1"
+ local usage="$(usage "$file")"
+
+ if [ -n "$usage" ]; then
+ echo "$usage"
+
+ local summary="$(summary "$file")"
+ [ -n "$summary" ] && echo "Summary: $summary"
+
+ local help="$(help "$file")"
+ [ -n "$help" ] && echo && echo "$help"
+ else
+ echo "Sorry, this command isn't documented yet."
+ fi
+}
+
+command_path() {
+ command -v "recit-$command" || command -v "recit-sh-$command" || true
+}
+
+summary() {
+ sed -n "s/^# Summary: \(.*\)/\1/p" "$1"
+}
+
+usage() {
+ sed -n "s/^# \(Usage: .*\)/\1/p" "$1"
+}
+
+help() {
+ awk '/^[^#]/{p=0} /^# Help:/{p=1} p' "$1" | sed "s/^# Help: //;s/^# //;s/^#//"
+}
+
+truncate() {
+ local max_length="$1"
+ local string="$2"
+
+ if [ "${#string}" -gt "$max_length" ]; then
+ local length=$(( $max_length - 3 ))
+ echo "${string:0:$length}..."
+ else
+ echo "$string"
+ fi
+}
+
+# Provide recit completions
+if [ "$1" = "--complete" ]; then
+ exec "recit-commands"
+ exit
+fi
+
+command="$1"
+case "$command" in
+"") echo "Usage: recit <command> [<args>]
+
+Some useful recit commands are:
+$(print_summaries)
+
+See 'recit help <command>' for information on a specific command."
+;;
+*)
+ file="$(command_path "$command")"
+
+ if [ -n "$file" ]; then
+ print_help "$file"
+ else
+ echo "recit: no such command \`$command'" >&2
+ exit 1
+ fi
+esac