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