File: ndf.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 # ndf [options...] [files...]
  27 #
  28 # Nice DF runs `df` and displays its output using ANSI styles, so things are
  29 # easier to scan/read. The options are the same as those for `df`.
  30 
  31 
  32 case "$1" in
  33     -help|--help)
  34         {
  35             awk '/^# +ndf /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  36             printf "\n\n"
  37             df --help
  38         } | less -MKiCRS --header=1
  39         exit 0
  40     ;;
  41 esac
  42 
  43 df "$@" |
  44 
  45 # add empty lines to chunk things, restyle numbers with at least 4 digits
  46 # to make them easier to read, and make 0s stand out
  47 awk '
  48     (NR - 1) % 5 == 1 { print "" }
  49 
  50     {
  51         gsub(/[3-9][0-9]%/, "\x1b[38;2;204;124;0m&\x1b[0m")
  52         gsub(/[7-9][0-9]%/, "\x1b[38;2;204;0;0m&\x1b[0m")
  53         gsub(/ 0+%? /, "\x1b[38;2;0;105;185m&\x1b[0m")
  54         line = $0
  55 
  56         # restyle digit-runs which are longer than 3
  57         while (match(line, /[0-9]{4,}/)) {
  58             len = RLENGTH
  59             lead = len % 3
  60             alt = lead > 0
  61 
  62             printf "%s", substr(line, 1, RSTART + lead - 1)
  63             line = substr(line, RSTART + lead)
  64             len -= lead
  65 
  66             while (len > 0) {
  67                 fs = (alt == 1) ? "\x1b[38;2;168;168;168m%s\x1b[0m" : "%s"
  68                 printf fs, substr(line, 1, 3)
  69 
  70                 alt = 1 - alt
  71                 line = substr(line, 4)
  72                 len -= 3
  73             }
  74         }
  75 
  76         printf "%s\n", line
  77     }
  78 ' |
  79 
  80 less -MKiCRS --header=1