Skip to content

Commit

Permalink
TUI colors, fixes, group by cmdline
Browse files Browse the repository at this point in the history
  • Loading branch information
inv2004 committed Mar 25, 2024
1 parent cae0987 commit 54aecdb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/ttop/format.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ proc formatSPair*(b: int): (float, string) =

return (b.float, ".")

proc formatN3*(a: int): string =
if a > 999:
fmt "{(a div 1000):2}k"
else:
fmt "{a:3}"

proc formatS*(a: int): string =
let (n, s) = formatSPair(a)
if a < 1024:
Expand Down
25 changes: 15 additions & 10 deletions src/ttop/procfs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type PidInfo* = object
netInDiff*, netOutDiff*: uint
parents*:seq[uint] # generated from ppid, used to build tree
threads*: int
count*: int
docker*: string

type CpuInfo* = object
Expand Down Expand Up @@ -550,15 +551,18 @@ proc sort*(info: FullInfoRef, sortOrder = Pid, threads = false, group = false) =
elif sortOrder != Pid:
sort(info.pidsInfo, sortFunc(sortOrder))

proc id(cmd: string): string =
let idx = cmd.find(' ')
if idx >= 0:
cmd[0..<idx]
else:
cmd

proc group*(pidsInfo: OrderedTableRef[uint, PidInfo]): OrderedTableRef[uint, PidInfo] =
var grpInfo = initOrderedTable[string, PidInfo]()
for _, pi in pidsInfo:
let name =
if pi.name.startsWith("Relay("):
"init"
else:
pi.name
var g = grpInfo.getOrDefault(name)
let id = id(pi.cmd)
var g = grpInfo.getOrDefault(id)
if g.state == "":
g.state = pi.state
else:
Expand All @@ -571,14 +575,15 @@ proc group*(pidsInfo: OrderedTableRef[uint, PidInfo]): OrderedTableRef[uint, Pid
g.uptime = pi.uptime
else:
g.uptime = min(g.uptime, pi.uptime)
g.name = name
g.name = id
g.mem += pi.mem
g.rss += pi.rss
g.cpu += pi.cpu
g.ioReadDiff += pi.ioReadDiff
g.ioWriteDiff += pi.ioWriteDiff
g.threads.inc
grpInfo[name] = g
g.threads += pi.threads
g.count.inc
grpInfo[id] = g

result = newOrderedTable[uint, PidInfo]()
var i: uint = 0
Expand All @@ -591,4 +596,4 @@ when isMainModule:
let pi = group(fi.pidsInfo)
fi.pidsInfo.clear()
for o, pi in pi:
echo o, ": ", pi
echo o, ": ", pi.name
32 changes: 21 additions & 11 deletions src/ttop/tui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,21 @@ proc header(tui: Tui, tb: var TerminalBuffer, info: FullInfoRef, cnt: int,
blog: string) =
let mi = info.mem
tb.setCursorPos offset, 1
tb.write bgCyan, info.sys.hostname, fgWhite, ": ",
tb.write info.sys.hostname, " ",
info.sys.datetime.format(
"yyyy-MM-dd HH:mm:ss")
tb.write styleDim
if tui.hist > 0:
tb.write fmt" {blog}: {tui.hist} / {cnt} "
tb.write fmt" {blog} {tui.hist}/{cnt} "
elif blog == "":
tb.write fmt" autoupdate log: empty "
else:
tb.write fmt" autoupdate {blog}: {cnt} "
tb.write fmt" autoupdate {blog} {cnt}/{cnt} "
let curX = tb.getCursorXPos()
if tb.width - curX - 2 > 0:
tb.write ' '.repeat(tb.width - curX - 2)
tb.setCursorXPos curX
tb.write resetStyle
# let powerStr = fmt"{float(info.power) / 1000000:5.2f} W"
let procStr = fmt"PROCS: {$info.pidsInfo.len}"
tb.writeR procStr
Expand Down Expand Up @@ -278,25 +280,31 @@ proc checkFilter(filter: string, p: PidInfo): bool =
proc table(tui: Tui, tb: var TerminalBuffer, pi: OrderedTableRef[uint, PidInfo],
statsLen: int) =
var y = tb.getCursorYPos() + 1
tb.write styleBright
tb.write(offset, y, bgBlue, fmt"""{"S":1}""")
tb.write styleDim
tb.write(offset, y, fmt"""{"S":1}""")
if not tui.group:
tb.write fmt""" {"PID":>6}"""
tb.write fmt""" {"USER":<8} {"RSS":>10} {"MEM%":>5} {"CPU%":>5} {"r/w IO":>9} {"UP":>8}"""
if tui.threads:
if tui.group:
tb.write fmt""" {"CNT":>3}"""
if tui.threads or tui.group:
tb.write fmt""" {"THR":>3} """
elif tui.group:
tb.write fmt""" {"CNT":>3} """
if tb.width - 63 > 0:
tb.write ' '.repeat(tb.width-63), bgNone
inc y
var i: uint = 0
var i = 0
tb.setStyle {}
tb.write fgColor
if tui.scrollY > 0:
tb.setCursorPos (tb.width div 2)-1, tb.getCursorYPos()+1
tb.write "..."
inc y
dec i
for (_, p) in pi.pairs:
if tui.filter.isSome:
if checkFilter(tui.filter.get, p):
continue
elif i < uint tui.scrollY:
elif i < tui.scrollY:
inc i
continue
tb.setCursorPos offset, y
Expand Down Expand Up @@ -325,8 +333,10 @@ proc table(tui: Tui, tb: var TerminalBuffer, pi: OrderedTableRef[uint, PidInfo],

let lvl = p.parents.len
var cmd = ""
if tui.group:
tb.write " ", p.count.formatN3()
if tui.threads or tui.group:
tb.write " ", ($p.threads).cut(3, true, tui.scrollX), " "
tb.write " ", p.threads.formatN3(), " "
if lvl > 0:
tb.write fgCyan, repeat("·", lvl)
else:
Expand Down
2 changes: 1 addition & 1 deletion ttop.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "1.4.2"
version = "1.4.3"
author = "inv2004"
description = "Monitoring tool with historical snapshots and alerts"
license = "MIT"
Expand Down

0 comments on commit 54aecdb

Please sign in to comment.