File: hydra.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 # hydra [options...] [args...]
  27 #
  28 #
  29 # Show the first few lines of all input(s) given. The name comes from the
  30 # multi-headed monster of ancient mythology, a nod to the standard cmd-line
  31 # app `head`, which also limits inputs up to the number of lines given.
  32 #
  33 # The default line limit is 10, but you can change it with non-negative
  34 # integer numbers you pass along the arguments, letting you use different
  35 # max-numbers of lines for different files/inputs.
  36 #
  37 # The options are, available both in single and double-dash versions
  38 #
  39 #    -h, -help    show this help message
  40 
  41 
  42 case "$1" in
  43     -h|--h|-help|--help)
  44         awk '/^# +hydra /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  45         exit 0
  46     ;;
  47 
  48     -) ;;
  49 
  50     --) shift ;;
  51 
  52     -*)
  53         printf "unsupported option '%s'\n" "$1" >&2
  54         exit 1
  55     ;;
  56 esac
  57 
  58 cmd="bat"
  59 # debian linux uses a different name for the `bat` app
  60 if [ -e /usr/bin/batcat ]; then
  61     cmd="batcat"
  62 fi
  63 
  64 first=1
  65 maxlines=10
  66 errcode=0
  67 
  68 for a in "$@"; do
  69     # non-negative-integer arguments change the line limit
  70     if echo "${a}" | grep -E -q '^\+?[0-9]+$'; then
  71         maxlines="${a}"
  72         continue
  73     fi
  74 
  75     # visually separate results with extra empty lines
  76     if [ "${first}" -eq 0 ]; then
  77         printf "\n"
  78     fi
  79     first=0
  80 
  81     # show file using syntax-coloring, if available
  82     "${cmd}" --style=plain,header,numbers \
  83         --theme='Monokai Extended Light' \
  84         --wrap=never \
  85         --color=always \
  86         --paging=never \
  87         --line-range :"${maxlines}" "${a}" |
  88             # make colors readable on a wide range of backgrounds
  89             sed 's-\x1b\[38;5;70m-\x1b\[38;5;28m-g'
  90 
  91     # the previous way, kept for reference... or preference
  92     # printf "\033[7m%-80s\033[0m\n" "${a}"
  93     # head -n "${maxlines}" "${a}" | cat -n
  94 done
  95 
  96 exit "${errcode}"