#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # nls [options...] [folders...] # # Nice LS runs `ls` to show files (and folders), then colors folders and links # using ANSI styles to make things easier to scan/read. Long numbers are also # made easier to read using alternating ANSI styles. # # To declutter the output, entries for same folders, the parent folders, and # the totals are ignored from the `ls` results. # # Besides the usual `ls`-specific options, this script offers easier-to-use # extra options, where leading double-dashes are also allowed: # # -group group folders and links separately from regular files # -help show this help message # -sort reverse-sort by size case "$1" in --h|-help|--help) awk '/^# +nls /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac list='ls -al --file-type --color=never --time-style iso' for arg in "$@"; do if [ "${arg}" = '--' ]; then shift break fi case "${arg}" in -group|--group) list="${list} --group-directories-first" shift continue ;; -sort|--sort) list="${list} -S" shift continue ;; -*) list="${list} ${arg}" shift continue ;; esac break done options='-MKiCRS' if [ $# -le 1 ]; then options='--header=1 -MKiCRS' fi gap=0 for arg in "${@:-.}"; do if [ -L "${arg}" ]; then arg="$(realpath "${arg}")" fi if [ ! -d "${arg}" ]; then continue fi [ "${gap}" -gt 0 ] && printf "\n" printf "\e[7m%s\e[0m\n\n" "$(realpath "${arg}" || "${arg}")" gap=1 ${list} "${arg}" | awk ' BEGIN { drep = "\x1b[38;2;0;135;255m\x1b[48;2;228;228;228m&\x1b[0m" lrep = "\x1b[38;2;0;135;95m\x1b[48;2;228;228;228m&\x1b[0m" } / \.\.?\/$/ || /^total / { next } { gsub(/^(d[rwx-]+)/, drep) gsub(/^(l[rwx-]+)/, lrep) if (n % 5 == 0 && n > 0) print "" printf "%6d %s\n", ++n, $0 } ' | sed -E \ -e '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' \ -e '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' \ -e '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' \ -e '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' \ -e 's-([0-9]{1,3})([0-9]{3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m\3-g' \ -e 's-([0-9]{1,3})([0-9]{3})-\1\x1b[38;2;168;168;168m\2\x1b[0m-g' done | less ${options}