#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # np [options...] # # Nice Processes shows all current processes using ANSI styles to make # things easier to scan/read. To get the process info, the standard `ps` # command is used: when not given any options, `ps aux` is the default. # # The options are the same as those for `ps`, as well as any of `-h`, `--h`, # `-help`, or `--help`, to show this help message. case "$1" in -h|--h|-help|--help) awk '/^# +np /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac res="$(ps "${@:-aux}")" code=$? if [ "${code}" -ne 0 ]; then return "${code}" fi echo "${res}" | # restyle numbers with at least 4 digits to make them easier to read; ignore # numbers in the command fields at the end of lines awk ' /COMMAND$/ && cut == 0 { match($0, /COMMAND$/) cut = RSTART } { s = (cut > 0) ? substr($0, 1, cut - 1) : $0 # restyle digit-runs which are longer than 3 while (match(s, /[0-9]{4,}/)) { # give up on digit-runs which are unusually long, to mitigate the # quadratic time-complexity of slicing strings in a loop if (RLENGTH > 1000) { printf "%s", substr(s, 1, RSTART + RLENGTH) s = substr(s, RSTART + RLENGTH) continue } len = RLENGTH lead = len % 3 alt = lead > 0 printf "%s", substr(s, 1, RSTART + lead - 1) s = substr(s, RSTART + lead) len -= lead while (len > 0) { triple = substr(s, 1, 3) if (alt == 1) { printf "\x1b[38;2;168;168;168m%s\x1b[0m", triple } else { printf "%s", triple } alt = 1 - alt s = substr(s, 4) len -= 3 } } if (cut > 0) { printf "%s%s\n", s, substr($0, cut); fflush() } else { printf "%s\n", s; fflush() } } ' | # add a header line with the current time/date; style lines where root is the # user differently; make 0 values stand out in any line awk ' BEGIN { d = strftime("%a %b %d") t = strftime("%H:%M:%S") # printf "\x1b[7m%30s%s %s%30s\x1b[0m\n\n", "", d, t, "" fmt = "\x1b[38;2;128;128;128m\x1b[7m%30s%s %s%30s\x1b[0m\n\n" printf fmt, "", d, t, "" } (NR - 1) % 5 == 1 && NR > 1 { print "" } $1 == "root" { gsub(/^/, "\x1b[38;2;52;101;164m") gsub(/ +/, "&\x1b[0m\x1b[38;2;52;101;164m") gsub(/$/, "\x1b[0m") } { gsub(/0\.00*/, "\x1b[38;2;135;135;175m&\x1b[0m") gsub(/0:00/, "\x1b[38;2;135;135;175m&\x1b[0m") printf "%3d %s\n", NR - 1, $0 } ' | less -JMKiCRS