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 for arg in "$@"; do
  47     if [ "${arg}" = '--' ]; then
  48         shift
  49         break
  50     fi
  51 
  52     case "${arg}" 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} ${arg}"
  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[rwx-]+/
  97         link = /^l[rwx-]+/
  98 
  99         gsub(/^(d[rwx-]+)/, "\x1b[38;2;0;165;225m&\x1b[0m")
 100         gsub(/^(l[rwx-]+)/, "\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\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m\5\x1b[38;2;168;168;168m\6\x1b[0m\7-g;
 113     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m\5\x1b[38;2;168;168;168m\6\x1b[0m-g;
 114     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m\5-g;
 115     s-([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m\3\x1b[38;2;168;168;168m\4\x1b[0m-g;
 116     s-([0-9]{1,3})([0-9]{3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m\3-g;
 117     s-([0-9]{1,3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m-g
 118 '
 119 
 120 gap=0
 121 for arg in "${@:-.}"; do
 122     arg="$(realpath "${arg}")"
 123     [ "${gap}" -gt 0 ] && printf "\n"
 124     printf "\e[7m%-80s\e[0m\n\n" "${arg}"
 125     gap=1
 126     ${list} "${arg}" | sed -E "${alternate_digits}" | awk "${restyle}"
 127 done | { less ${less_options} 2> /dev/null || cat; }