File: sf.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 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 # sf [options...] [folders...]
  27 #
  28 # Show Files (and folders), coloring folders and links, using ANSI styles to
  29 # make things easier to scan/read.
  30 #
  31 # The only options are those for `systemctl status`, as well as any of `-h`,
  32 # `--h`, `-help`, or `--help`, to show this help message.
  33 
  34 
  35 case "$1" in
  36     -h|--h|-help|--help)
  37         awk '/^# +sf /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  38         exit 0
  39     ;;
  40 esac
  41 
  42 gap=0
  43 options='-MKiCRS'
  44 if [ $# -le 1 ]; then
  45     options='--header=1 -MKiCRS'
  46 fi
  47 
  48 for arg in "${@:-.}"; do
  49     [ "${gap}" -gt 0 ] && printf "\n"
  50     printf "\e[7m%s\e[0m\n\n" "$(realpath "${arg}")"
  51     gap=1
  52 
  53     # ls -al --file-type --color=never --time-style iso "${arg}" | awk '
  54     #     BEGIN {
  55     #         drep = "\x1b[38;2;0;135;255m\x1b[48;2;228;228;228m&\x1b[0m"
  56     #         lrep = "\x1b[38;2;0;135;95m\x1b[48;2;228;228;228m&\x1b[0m"
  57     #     }
  58     #
  59     #     (NR - 1) % 5 == 1 { print "" }
  60     #
  61     #     {
  62     #         gsub(/^(d[rwx-]+)/, drep)
  63     #         gsub(/^(l[rwx-]+)/, lrep)
  64     #         printf "%6d  %s\n", NR - 1, $0; fflush()
  65     #     }
  66     # '
  67 
  68     ls -al --file-type --color=never --time-style iso "${arg}" | awk '
  69         BEGIN {
  70             drep = "\x1b[38;2;0;135;255m\x1b[48;2;228;228;228m&\x1b[0m"
  71             lrep = "\x1b[38;2;0;135;95m\x1b[48;2;228;228;228m&\x1b[0m"
  72         }
  73 
  74         NR < 4 { next }
  75         (NR - 3) % 5 == 1 && (NR - 3) > 1 { print "" }
  76 
  77         {
  78             gsub(/^(d[rwx-]+)/, drep)
  79             gsub(/^(l[rwx-]+)/, lrep)
  80             printf "%6d  %s\n", NR - 3, $0; fflush()
  81         }
  82     '
  83 done |
  84 
  85 # (re)style all long-enough digit-runs/numbers in lines
  86 awk '
  87 {
  88     line = $0
  89 
  90     # restyle digit-runs which are longer than 3
  91     while (match(line, /[0-9]{4,}/)) {
  92         len = RLENGTH
  93         lead = len % 3
  94         alt = lead > 0
  95 
  96         printf "%s", substr(line, 1, RSTART + lead - 1)
  97         line = substr(line, RSTART + lead)
  98         len -= lead
  99 
 100         while (len > 0) {
 101             if (alt == 1) {
 102                 printf "\x1b[38;2;168;168;168m%s\x1b[0m", substr(line, 1, 3)
 103             } else {
 104                 printf "%s", substr(line, 1, 3)
 105             }
 106 
 107             alt = 1 - alt
 108             line = substr(line, 4)
 109             len -= 3
 110         }
 111     }
 112 
 113     printf "%s\n", line; fflush()
 114 }
 115 ' |
 116 
 117 less "${options}"