From 891f946e86f8a0911f67983d52fccacc9f8ebbad Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Wed, 13 Jul 2022 23:40:24 -0400 Subject: initial --- libexec/recit-help | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100755 libexec/recit-help (limited to 'libexec/recit-help') 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 [] + +Some useful recit commands are: +$(print_summaries) + +See 'recit help ' 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 -- cgit v1.2.3