|
| 1 | +#!/usr/bin/env bash |
| 2 | +# plsdo.sh, version 0.1.1 |
| 3 | +set -o errexit -o nounset -o pipefail |
| 4 | + |
| 5 | +_plsdo_run() { |
| 6 | + case "${1-}" in |
| 7 | + ""|help) |
| 8 | + _plsdo_help "${2-}" ;; |
| 9 | + *) |
| 10 | + "$@" ;; |
| 11 | + esac |
| 12 | +} |
| 13 | + |
| 14 | +declare -A help |
| 15 | + |
| 16 | +help[list]="Print the list of tasks" |
| 17 | +list() { |
| 18 | + declare -F | awk '{print $3}' | grep -v '^_' |
| 19 | +} |
| 20 | + |
| 21 | +_plsdo_help_task_name_width="${_plsdo_help_task_name_width:-12}" |
| 22 | + |
| 23 | +_plsdo_help() { |
| 24 | + local topic="${1-}" |
| 25 | + # print help for the topic |
| 26 | + if [ -n "$topic" ]; then |
| 27 | + if ! command -v "$topic" > /dev/null ; then |
| 28 | + _plsdo_error "No such task: $topic" |
| 29 | + return 1 |
| 30 | + fi |
| 31 | + |
| 32 | + printf "\nUsage:\n %s %s\n\n%s\n" "$0" "$topic" "${help[$topic]-}" |
| 33 | + return 0 |
| 34 | + fi |
| 35 | + |
| 36 | + # print list of tasks and their help line. |
| 37 | + [ -n "${banner-}" ] && echo "$banner" && echo |
| 38 | + for i in $(list); do |
| 39 | + printf "%-${_plsdo_help_task_name_width}s\t%s\n" "$i" "${help[$i]-}" | head -1 |
| 40 | + done |
| 41 | +} |
| 42 | + |
| 43 | +_plsdo_error() { |
| 44 | + >&2 echo "$@" |
| 45 | +} |
| 46 | + |
| 47 | +# shellcheck disable=SC2016 |
| 48 | +help[_plsdo_completion]='Print tab completion for $SHELL. |
| 49 | +
|
| 50 | +Redirect the output to a file that will be run when the shell starts, |
| 51 | +such as ~/.bashrc. |
| 52 | +
|
| 53 | + $ ./do _pldsdo_completion >> ~/.bash_complete/do |
| 54 | +' |
| 55 | +_plsdo_completion() { |
| 56 | + local shell; shell="$(basename "$SHELL" 2> /dev/null)" |
| 57 | + case "$shell" in |
| 58 | + bash) |
| 59 | + cat <<+++ |
| 60 | +_dotslashdo_completions() { |
| 61 | + if ! command -v $0 > /dev/null; then return; fi |
| 62 | + if [ "\${#COMP_WORDS[@]}" != "2" ]; then return; fi |
| 63 | + COMPREPLY=(\$(compgen -W "\$($0 list)" "\${COMP_WORDS[1]}")) |
| 64 | +} |
| 65 | +complete -F _dotslashdo_completions $0 |
| 66 | ++++ |
| 67 | + ;; |
| 68 | + "") |
| 69 | + _plsdo_error "Set \$SHELL to select tab completion." |
| 70 | + return 1 ;; |
| 71 | + *) |
| 72 | + _plsdo_error "No completetion for shell: $shell" |
| 73 | + return 1 ;; |
| 74 | + esac |
| 75 | +} |
0 commit comments