File: verdict.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 # verdict [command...] [arguments...]
  27 #
  28 # Run the command given, colorfully showing its exit code on failure. On
  29 # success, the exit code is 0, but it's not shown explicitly.
  30 #
  31 # A colorblind-friendly blue is used instead of green if either environment
  32 # variable COLORBLIND or COLOR_BLIND is declared and set to 1.
  33 #
  34 # When variable NO_COLOR is declared and set to 1, an invert/highlight style
  35 # is used instead of colors, no matter the exit code of the command given.
  36 #
  37 # The options are, available both in single and double-dash versions
  38 #
  39 #   -h, -help     show this help message
  40 #   -q, -quiet    don't show command success, only show failure
  41 
  42 
  43 quiet=0
  44 
  45 case "$1" in
  46     -h|--h|-help|--help)
  47         awk '/^# +verdict /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  48         exit 0
  49     ;;
  50 
  51     -q|--q|-quiet|--quiet) quiet=1; shift ;;
  52 
  53     -) ;;
  54 
  55     --) shift ;;
  56 
  57     -*)
  58         printf "unsupported option '%s'\n" "$1" >&2
  59         exit 1
  60     ;;
  61 esac
  62 
  63 if [ $# -eq 0 ]; then
  64     awk '/^# +verdict /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  65     exit 0
  66 fi
  67 
  68 command -v "$1" > /dev/null
  69 code=$?
  70 
  71 if [ "${code}" -ne 0 ]; then
  72     printf "%s: command not found" "$1" >&2
  73     if [ "${NO_COLOR}" = 1 ]; then
  74         printf "\n%s \033[7m failed with error code %d \033[0m\n" "$*" "${code}"
  75     else
  76         printf "\n\033[38;2;204;0;0m%s \033[48;2;204;0;0m\033[38;2;255;255;255m failed with error code %d \033[0m\n" "$*" "${code}"
  77     fi >&2
  78     exit "${code}"
  79 fi
  80 
  81 # handle interruption signals, which usually happen by pressing Ctrl+C
  82 if [ "${NO_COLOR}" = 1 ]; then
  83     trap "printf \"\\n%s \\033[7m failed with error code 130 \\033[0m\\n\" \"$*\" >&2; exit 130" INT
  84 else
  85     trap "printf \"\\n\\033[38;2;204;0;0m%s \\033[48;2;204;0;0m\\033[38;2;255;255;255m failed with error code 130 \\033[0m\\n\" \"$*\" >&2; exit 130" INT
  86 fi
  87 
  88 "$@"
  89 code=$?
  90 
  91 if [ "${code}" = 0 ]; then
  92     # quiet-mode avoids showing successes
  93     [ "${quiet}" -eq 1 ] && exit 0
  94 
  95     if [ "${NO_COLOR}" = 1 ]; then
  96         printf "\n%s \033[7m succeeded \033[0m\n" "$*"
  97     elif [ "${COLORBLIND}" = 1 ] || [ "${COLOR_BLIND}" = 1 ]; then
  98         # color-blind-friendly version using a blue `success` message
  99         printf "\n\033[38;2;0;95;215m%s \033[48;2;0;95;215m\033[38;2;255;255;255m succeeded \033[0m\n" "$*"
 100     else
 101         printf "\n\033[38;2;0;135;95m%s \033[48;2;0;135;95m\033[38;2;255;255;255m succeeded \033[0m\n" "$*"
 102     fi
 103 else
 104     if [ "${NO_COLOR}" = 1 ]; then
 105         printf "\n%s \033[7m failed with error code %d \033[0m\n" "$*" "${code}"
 106     else
 107         printf "\n\033[38;2;204;0;0m%s \033[48;2;204;0;0m\033[38;2;255;255;255m failed with error code %d \033[0m\n" "$*" "${code}"
 108     fi
 109 fi >&2
 110 
 111 exit "${code}"