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 awk '/^# +hydra/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 41 exit 0 42 ;; 43 esac 44 45 cmd="bat" 46 # debian linux uses a different name for the `bat` app 47 if [ -e "/usr/bin/batcat" ]; then 48 cmd="batcat" 49 fi 50 51 first=1 52 maxlines=10 53 54 for a in "$@"; do 55 # non-negative-integer arguments change the line limit 56 if echo "${a}" | grep -E -q '^\+?[0-9]+$'; then 57 maxlines="${a}" 58 continue 59 fi 60 61 # visually separate results with extra empty lines 62 if [ "${first}" -eq 0 ]; then 63 printf "\n" 64 fi 65 first=0 66 67 # show file using syntax-coloring, if available 68 "${cmd}" --style=plain,header,numbers \ 69 --theme='Monokai Extended Light' \ 70 --wrap=never \ 71 --color=always \ 72 --paging=never \ 73 --line-range :"${maxlines}" "${a}" | 74 # make colors readable on a wide range of backgrounds 75 sed 's-\x1b\[38;5;70m-\x1b\[38;5;28m-g' 76 77 # the previous way, kept for reference... or preference 78 # printf "\e[7m%-80s\e[0m\n" "${a}" 79 # head -n "${maxlines}" "${a}" | cat -n 80 done