File: nps.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 # nps [seconds...] [options...] [ps options...]
  27 #
  28 #
  29 # Nice `Process Show` displays all current processes using ANSI styles, so
  30 # things are easier to scan/read. All data come from the `ps` command: when
  31 # not given any `ps`-specific options, `ps aux` is run by default.
  32 #
  33 # The options are the same as those for `ps`, as well as the ones listed
  34 # below, available in single and double-dashed versions
  35 #
  36 #   -c, -cpu          reverse-sort entries by latest sampled CPU use
  37 #   -h, -help         show this help message
  38 #   -i, -id, -pid     sort entries by PID
  39 #   -m, -mem, -rss    reverse-sort entries by latest sampled RSS (memory) use
  40 
  41 
  42 case "$1" in
  43     -h|--h|-help|--help)
  44         awk '/^# +nps /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  45         ps --help
  46         exit 0
  47     ;;
  48 esac
  49 
  50 for arg in "$@"; do
  51     if [ "${arg}" = '--' ]; then
  52         break
  53     fi
  54 
  55     case "${arg}" in
  56         a|u|x|au|ax|ua|ux|xa|xu) options="${options} ${arg}";;
  57         aux|axu|uax|uxa|xau|xua) options="${options} ${arg}";;
  58         -c|--c|-cpu|--cpu) options="${options} --sort=-%cpu,-rss";;
  59         -i|--i|-id|--id|-pid|--pid) options="${options} --sort=pid";;
  60         -m|--m|-mem|--mem|-rss|--rss) options="${options} --sort=-rss,-%cpu";;
  61         -u|--u|-user|--user) options="${options} --sort=user,-%cpu,-rss";;
  62         --sort=*) options="${options} ${arg}";;
  63     esac
  64 done
  65 
  66 res="$(ps ${options:-aux})"
  67 code=$?
  68 if [ "${code}" -ne 0 ]; then
  69     return "${code}"
  70 fi
  71 
  72 echo "${res}" |
  73 
  74 # restyle numbers with at least 4 digits to make them easier to read; restyle
  75 # lines where root is the user; make 0 values stand out in any line
  76 sed -E \
  77     -e 's-([0-9]{1,3})([0-9]{6}|[0-9]{3})( |$)-\x1b[36m\1\x1b[0m\2\3-g' \
  78     -e '/^root/s-\x1b\[0m-\x1b[0m\x1b[33m-g; /^root/s-^-\x1b[33m-' \
  79     -e 's-0(\.00*|:00)-\x1b[34m0\1\x1b[0m-g' |
  80 
  81 # add a header line with the current time/date and empty lines to chunk things
  82 awk '
  83     BEGIN {
  84         now = strftime("%a %b %d %Y  %H:%M:%S")
  85         printf "%26s\x1b[7m%s\x1b[0m\n\n", "", now
  86     }
  87     (NR - 1) % 5 == 1 { print "" }
  88     1
  89 ' |
  90 
  91 { less -MKiCRS --header=3 2> /dev/null || cat; }