Skip to content

Instantly share code, notes, and snippets.

@victor-almanzar
Created July 28, 2026 02:09
Show Gist options
  • Select an option

  • Save victor-almanzar/78368e3b60f2aaecf264d68a34b17740 to your computer and use it in GitHub Desktop.

Select an option

Save victor-almanzar/78368e3b60f2aaecf264d68a34b17740 to your computer and use it in GitHub Desktop.
def str-cols [] {
let head = $in
$head | columns | where {|c| ($head | get $c | describe) == 'string' }
}
def pad-strings [] {
let t = $in
$t | first | str-cols | reduce -f $t {|c, acc|
let w = ($acc | get $c | each {|s| $s | str length} | math max)
$acc | update $c {|| $in | fill --width $w }
}
}
def hl-strings [style: record] {
let row = $in
$row | str-cols | reduce -f $row {|c, acc|
$acc | update $c {|| $"(ansi --escape $style)($in)(ansi reset)" }
}
}
def psmon [
--interval: duration = 10sec
--threshold: float = 10.0
--style: record = {fg: '#ffffff' bg: '#ff0000'}
--pad: int = 55
] {
loop {
let term = (term size)
let rows = if ($term.rows - 4) < 1 { 1 } else { $term.rows - 4 }
let namew = if ($term.columns - $pad) < 8 { 8 } else { $term.columns - $pad }
let frame = (
ps
| sort-by -r cpu
| first $rows
| update name {|| $in | fill --width $namew }
| pad-strings
| each {|r| if $r.cpu > $threshold { $r | hl-strings $style } else { $r } }
| table --width $term.columns
| into string
| lines
| each {|l| $"($l)(ansi reset)(ansi -e 'K')" }
| str join "\r\n"
)
print -n $"(ansi -e '?2026h')(ansi home)($frame)(ansi -e 'J')(ansi -e '?2026l')"
sleep $interval
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment