File: pot.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 # 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      reverse-sort entries by latest sampled CPU use
  36 #   -cpu    reverse-sort entries by latest sampled CPU use
  37 #
  38 #   -h      show this help message
  39 #   -help   show this help message
  40 #
  41 #   -m      reverse-sort entries by latest sampled RSS (memory) use
  42 #   -mem    reverse-sort entries by latest sampled RSS (memory) use
  43 #   -rss    reverse-sort entries by latest sampled RSS (memory) use
  44 #
  45 #   -u      sort/group entries by user
  46 #   -user   sort/group entries by user
  47 
  48 
  49 case "$1" in
  50     -h|--h|-help|--help)
  51         awk '/^# +pot /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  52         # ps --help
  53         exit 0
  54     ;;
  55 esac
  56 
  57 for arg in "$@"; do
  58     case "${arg}" in
  59         --) break;;
  60         a|u|x|au|ax|ua|ux|xa|xu) select="${select}${arg}";;
  61         aux|axu|uax|uxa|xau|xua) select="${select}${arg}";;
  62         -c|--c|-cpu|--cpu) sort="--sort=-%cpu,-rss";;
  63         -i|--i|-id|--id|-pid|--pid) sort="--sort=pid";;
  64         -m|--m|-mem|--mem|-rss|--rss) sort="--sort=-rss,-%cpu";;
  65         -u|--u|-user|--user) sort="--sort=user,-%cpu,-rss";;
  66         # -u|--u|-user|--user) sort="--sort=user,-rss,-%cpu";;
  67         --sort=*) sort="${arg}";;
  68         -compact|--compact|-tight|--tight) compact=1;;
  69         *)
  70             if echo "${arg}" | grep -q '^[1-9][0-9]*$' > /dev/null; then
  71                 seconds="${arg}"
  72             fi
  73         ;;
  74     esac
  75 done
  76 
  77 options="${select:-aux} ${sort:---sort=pid}"
  78 
  79 gather='res="$(ps '"${options}"')"; echo "${res}"'
  80 
  81 # sed to color-code values and make them more readable
  82 big="s-([0-9]{1,3})([0-9]{6}|[0-9]{3})( |$)-\x1b[36m\1\x1b[0m\2\3-g"
  83 zeros="s- (0\.0|0:00) - \x1b[34m\1\x1b[0m -g"
  84 root="/^root/s-\x1b\[0m-\x1b[0m\x1b[33m-g; /^root/s-^-\x1b[33m-"
  85 colors="${big}; ${root}; ${zeros}; s-\\\$-\x1b[0m-"
  86 
  87 if [ "${compact}" = 1 ]; then
  88     exec watch -n "${seconds:-2}" -c "${gather}"' | sed -E "'"${colors}"'"'
  89 else
  90     # awk code to insert empty lines, and more easily follow lines horizontally
  91     lines="(NR - 1) % 5 == 1 { print empty } 1"
  92     exec watch -n "${seconds:-2}" -c \
  93         "${gather}"' | sed -E "'"${colors}"'" | awk "'"${lines}"'"'
  94 fi