File: pause.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # pause [options...] [command...] [args...]
  27 #
  28 # Show a prompt and wait for the user to press the enter key. If a command
  29 # is given, that command is run right before, along with any/all arguments
  30 # given after it. The prompt message is shown to the standard error.
  31 #
  32 # The options are, available both in single and double-dash versions
  33 #
  34 #   -h, -help       show this help message
  35 #   -p, -prompt     change the default prompt message
  36 #   -v, -verbose    also show on stderr when the command given is done
  37 
  38 
  39 verbose=0
  40 prompt='Press the Enter key to continue...'
  41 
  42 while [ $# -gt 0 ]; do
  43     case "$1" in
  44         -h|--h|-help|--help)
  45             awk '/^# +pause /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  46             exit 0
  47         ;;
  48 
  49         -p|--p|-prompt|--prompt)
  50             if [ $# -lt 2 ]; then
  51                 printf "forgot the prompt message\n" >&2
  52                 exit 1
  53             fi
  54             prompt="$2"
  55             shift 2
  56             continue
  57         ;;
  58 
  59         -v|--v|-verbose|--verbose) verbose=1; shift; continue ;;
  60 
  61         --) shift; break ;;
  62 
  63         -*)
  64             printf "%s: unsupported option %s\n" "$0" "$1" >&2
  65             exit 1
  66         ;;
  67     esac
  68 
  69     break
  70 done
  71 
  72 if [ $# -gt 0 ]; then
  73     "$@"
  74     code=$?
  75 
  76     if [ "${verbose}" -eq 1 ]; then
  77         if [ "${NO_COLOR}" = 1 ]; then
  78             printf "%s is done\n" "$*" >&2
  79         else
  80             printf "\033[7m%s\033[0m is done\n" "$*" >&2
  81         fi
  82     fi
  83     printf "%s" "${prompt}" >&2
  84     read -r ignored
  85     exit "${code}"
  86 fi
  87 
  88 printf "%s" "${prompt}" >&2
  89 read -r ignored