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 
  44 for arg in "${@:-.}"; do
  45     [ "${gap}" -gt 0 ] && printf "\n"
  46     printf "\e[7m%s\e[0m\n\n" "$(realpath "${arg}")"
  47     gap=1
  48 
  49     ls -al --file-type --color=never --time-style iso "${arg}" | awk '
  50         BEGIN {
  51             drep = "\x1b[38;2;0;135;255m\x1b[48;2;228;228;228m&\x1b[0m"
  52             lrep = "\x1b[38;2;0;135;95m\x1b[48;2;228;228;228m&\x1b[0m"
  53         }
  54 
  55         (NR - 1) % 5 == 1 && NR > 1 { print "" }
  56 
  57         {
  58             gsub(/^(d[rwx-]+)/, drep)
  59             gsub(/^(l[rwx-]+)/, lrep)
  60             printf "%6d  %s\n", NR - 1, $0; fflush()
  61         }
  62     '
  63 done |
  64 
  65 # (re)style all long-enough digit-runs/numbers in lines
  66 awk '
  67 {
  68     line = $0
  69 
  70     # restyle digit-runs which are longer than 3
  71     while (match(line, /[0-9]{4,}/)) {
  72         len = RLENGTH
  73         lead = len % 3
  74         alt = lead > 0
  75 
  76         printf "%s", substr(line, 1, RSTART + lead - 1)
  77         line = substr(line, RSTART + lead)
  78         len -= lead
  79 
  80         while (len > 0) {
  81             if (alt == 1) {
  82                 printf "\x1b[38;2;168;168;168m%s\x1b[0m", substr(line, 1, 3)
  83             } else {
  84                 printf "%s", substr(line, 1, 3)
  85             }
  86 
  87             alt = 1 - alt
  88             line = substr(line, 4)
  89             len -= 3
  90         }
  91     }
  92 
  93     printf "%s\n", line; fflush()
  94 }
  95 ' |
  96 
  97 less -JMKiCRS