File: pot.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 # pot [options...]
  27 #
  28 # A task watcher (similar to `top`) which shows all current processes using
  29 # colors, so things are easier to scan/read. All data come from periodically
  30 # running the `ps` command: when not given any `ps`-specific options, the
  31 # default is to run `ps aux --sort=pid`.
  32 #
  33 # The options are, available in single and double-dashed versions
  34 #
  35 #   -c, -cpu          reverse-sort entries by latest sampled CPU use
  36 #   -h, -help         show this help message
  37 #   -m, -mem, -rss    reverse-sort entries by latest sampled RSS (memory) use
  38 #   -u, -user         sort/group entries by user
  39 
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +pot /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         # ps --help
  45         exit 0
  46     ;;
  47 esac
  48 
  49 for arg in "$@"; do
  50     case "${arg}" in
  51         --) break;;
  52         a|u|x|au|ax|ua|ux|xa|xu) select="${select}${arg}";;
  53         aux|axu|uax|uxa|xau|xua) select="${select}${arg}";;
  54         -c|--c|-cpu|--cpu) sort="--sort=-%cpu,-rss";;
  55         -i|--i|-id|--id|-pid|--pid) sort="--sort=pid";;
  56         -m|--m|-mem|--mem|-rss|--rss) sort="--sort=-rss,-%cpu";;
  57         -u|--u|-user|--user) sort="--sort=user,-%cpu,-rss";;
  58         # -u|--u|-user|--user) sort="--sort=user,-rss,-%cpu";;
  59         --sort=*) sort="${arg}";;
  60         -compact|--compact|-tight|--tight) compact=1;;
  61         *)
  62             if echo "${arg}" | grep -q '^[1-9][0-9]*$' > /dev/null; then
  63                 seconds="${arg}"
  64             fi
  65         ;;
  66     esac
  67 done
  68 
  69 options="${select:-aux} ${sort:---sort=pid}"
  70 
  71 gather='res="$(ps '${options}')"; echo "${res}"'
  72 
  73 # sed to color-code values and make them more readable
  74 big="s-([0-9]{1,3})([0-9]{6}|[0-9]{3})( |$)-\x1b[36m\1\x1b[0m\2\3-g"
  75 zeros="s- (0\.0|0:00) - \x1b[34m\1\x1b[0m -g"
  76 root="/^root/s-\x1b\[0m-\x1b[0m\x1b[33m-g; /^root/s-^-\x1b[33m-"
  77 colors="${big}; ${root}; ${zeros}; s-\\\$-\x1b[0m-"
  78 
  79 if [ "${compact}" = 1 ]; then
  80     exec watch -n "${seconds:-2}" -c "${gather}"' | sed -E "'"${colors}"'"'
  81 else
  82     # awk code to insert empty lines, and more easily follow lines horizontally
  83     lines="(NR - 1) % 5 == 1 { print empty } 1"
  84     exec watch -n "${seconds:-2}" -c \
  85         "${gather}"' | sed -E "'"${colors}"'" | awk "'"${lines}"'"'
  86 fi