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 #   -h, -help         show this help message
  37 
  38 
  39 # This version is compatible with busybox/alpine-linux.
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +nps /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         ps --help
  45         exit 0
  46     ;;
  47 esac
  48 
  49 [ "$1" = '--' ] && shift
  50 
  51 if [ $# -eq 0 ]; then
  52     res="$(ps aux)"
  53     code=$?
  54 else
  55     res="$(ps "$@")"
  56     code=$?
  57 fi
  58 
  59 if [ "${code}" -ne 0 ]; then
  60     return "${code}"
  61 fi
  62 
  63 echo "${res}" |
  64 
  65 # restyle numbers with at least 4 digits to make them easier to read; restyle
  66 # lines where root is the user; make 0 values stand out in any line
  67 sed -E \
  68     -e 's-([0-9]{1,3})([0-9]{6}|[0-9]{3})( |$)-\1\2\3-g' \
  69     -e '/^root/s-\[0m--g; /^root/s-^--' \
  70     -e 's-0(\.00*|:00)-0\1-g' |
  71 
  72 # add a header line with the current time/date and periodically underline rows
  73 # to chunk things visually
  74 awk '
  75     BEGIN {
  76         now = strftime("%a %b %d %Y  %H:%M:%S")
  77         printf "%26s\x1b[7m%s\x1b[0m\n\n", "", now
  78     }
  79 
  80     NR == 1 || (NR - 1) % 5 == 0 {
  81         gsub(/\x1b\[0m/, "\x1b[0m\x1b[4m")
  82         printf("\x1b[4m%s\x1b[0m\n", $0)
  83         next
  84     }
  85 
  86     1
  87 ' \
  88 | { less -MKiCRS --header=3 2> /dev/null || less -RIMS 2> /dev/null || cat; }