File: nls.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 # nls [options...] [files/folders...]
  27 #
  28 # Nice LS runs `ls` to show files (and folders), then colors folders and links
  29 # using ANSI styles to make things easier to scan/read. Long numbers are also
  30 # made easier to read using alternating ANSI styles.
  31 #
  32 # To declutter the output, the `.` and `..` entries are ignored from the `ls`
  33 # results.
  34 #
  35 # Besides the usual `ls`-specific options, this script offers easier-to-use
  36 # extra options, where leading double-dashes are also allowed:
  37 #
  38 #     -group, -grouped    show folders before regular files and links
  39 #     -help               show this help message
  40 #     -iso                show date/time metadata using the long ISO format
  41 #     -sort, -sorted      reverse-sort results by size
  42 
  43 
  44 list='ls -al --color=never'
  45 
  46 while [ $# -gt 0 ]; do
  47     if [ "$1" = '--' ]; then
  48         shift
  49         break
  50     fi
  51 
  52     case "$1" in
  53         -group|--group|-grouped|--grouped)
  54             list="${list} --group-directories-first"
  55             shift
  56             continue
  57         ;;
  58 
  59         --h|-help|--help)
  60             awk '/^# +nls /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  61             exit 0
  62         ;;
  63 
  64         -iso|--iso)
  65             list="${list} --time-style long-iso"
  66             shift
  67             continue
  68         ;;
  69 
  70         -sort|--sort|-sorted|--sorted)
  71             list="${list} -S"
  72             shift
  73             continue
  74         ;;
  75 
  76         -*)
  77             list="${list} $1"
  78             shift
  79             continue
  80         ;;
  81     esac
  82 
  83     break
  84 done
  85 
  86 less_options='-MKiCRS'
  87 if [ $# -le 1 ]; then
  88     less_options='-MKiCRS --header=1'
  89 fi
  90 
  91 restyle='
  92     / \.\.?\/?$/ || /^total / { next }
  93 
  94     {
  95         underline = ((n + 1) % 5 == 0 && n != 1)
  96         folder = /^d[^ ]+/
  97         link = /^l[^ ]+/
  98 
  99         gsub(/^(d[^ ]+)/, "\x1b[38;2;0;165;225m&\x1b[0m")
 100         gsub(/^(l[^ ]+)/, "\x1b[38;2;0;185;45m&\x1b[0m")
 101         if (folder) gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;0;165;225m")
 102         if (link) gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;0;185;45m")
 103         if (underline) gsub(/\x1b\[0m/, "\x1b[0m\x1b[4m")
 104 
 105         fmt = underline ? "%5d  \x1b[4m%s" : "%5d  %s"
 106         printf fmt, ++n, $0
 107         printf (underline || folder || link) ? "\x1b[0m\n" : "\n"
 108     }
 109 '
 110 
 111 alternate_digits='
 112     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\2\3\4\5\6\7-g;
 113     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\2\3\4\5\6-g;
 114     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\2\3\4\5-g;
 115     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\2\3\4-g;
 116     s-([0-9]{1,3})([0-9]{3})([0-9]{3})-\1\2\3-g;
 117     s-([0-9]{1,3})([0-9]{3})-\1\2-g;
 118 '
 119 
 120 # show all non-existing files/folders given
 121 failed=0
 122 for arg in "$@"; do
 123     if [ "${arg}" = "-" ]; then
 124         continue
 125     fi
 126     if [ ! -e "${arg}" ]; then
 127         printf "no file/folder named \"%s\"\n" "${arg}" >&2
 128         failed=1
 129     fi
 130 done
 131 
 132 if [ "${failed}" -gt 0 ]; then
 133     exit 2
 134 fi
 135 
 136 gap=0
 137 for arg in "${@:-.}"; do
 138     arg="$(realpath "${arg}")"
 139     [ "${gap}" -gt 0 ] && printf "\n"
 140     printf "\e[7m%-80s\e[0m\n\n" "${arg}"
 141     gap=1
 142     ${list} "${arg}" | sed -E "${alternate_digits}" | awk "${restyle}"
 143 done | { less ${less_options} 2> /dev/null || less -RIMS 2> /dev/null || cat; }