#!/usr/bin/env bash set -e print_summaries() { local commands=() local summaries=() local longest_command=0 local command for command in $(iron-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 help="$(help "$file")" [ -n "$help" ] && echo && echo "$help" else echo "Sorry, this command isn't documented yet." fi } command_path() { command -v "iron-$command" || command -v "iron-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 iron completions if [ "$1" = "--complete" ]; then exec "iron-commands" exit fi command="$1" case "$command" in "") echo "Usage: iron [] Some useful iron commands are: $(print_summaries) See 'iron help ' for information on a specific command." ;; *) file="$(command_path "$command")" if [ -n "$file" ]; then print_help "$file" else echo "iron: no such command \`$command'" >&2 exit 1 fi esac # This file is part of Iron # Copyright (c) 2016-2024 Paolo Lulli. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see .