Skip to content

Commit

Permalink
initial history
Browse files Browse the repository at this point in the history
  • Loading branch information
inv2004 committed Nov 27, 2022
1 parent a985774 commit 08634a9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 13 deletions.
16 changes: 15 additions & 1 deletion src/ttop.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import ttop/tui
import ttop/save

import os

proc isSave(): bool =
if paramCount() >= 1 and paramStr(1) == "-s":
true
elif getAppFilename().extractFilename() == "ttop":
false
else:
false

proc main() =
run()
if isSave():
save()
else:
run()

when isMainModule:
main()
45 changes: 45 additions & 0 deletions src/ttop/save.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import procfs
import marshal
import zippy
import streams

const blog = "/tmp/1.blog"

proc count*(): int =
let s = newFileStream(blog, fmRead)
if s == nil:
return
while not s.atEnd():
let sz = s.readUInt32()
discard s.readStr(int(sz))
discard s.readUInt32()
inc result

proc save*() =
let info = fullInfo()
let buf = compress($$info)
let s = newFileStream(blog, fmAppend)
s.write buf.len.uint32
s.write buf
s.write buf.len.uint32
s.close()

proc hist*(ii: int): FullInfo =
let f = open(blog, fmRead)
defer: f.close()
let s = newFileStream(f)
s.setPosition(f.getFileSize().int)
for i in 1..ii:
if s.getPosition() - 4 < 0:
return
s.setPosition(s.getPosition().int - 4)
let sz = s.readUInt32.int
s.setPosition(s.getPosition().int - 8 - sz)

let sz = s.readUInt32.int
let buf = s.readStr(sz)
to[FullInfo](uncompress(buf))

when isMainModule:
echo count()
echo hist(6)
40 changes: 29 additions & 11 deletions src/ttop/tui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import times
import limits
import format
import sequtils
import save

proc exitProc() {.noconv.} =
illwillDeinit()
Expand All @@ -22,12 +23,13 @@ proc writeR(tb: var TerminalBuffer, s: string) =
tb.setCursorXPos x
tb.write(s)

proc header(tb: var TerminalBuffer, info: FullInfo) =
proc header(tb: var TerminalBuffer, info: FullInfo, hist: int) =
let mi = info.mem
tb.write(offset, 1, fgWhite)
tb.write fgBlue, info.sys.hostname, fgWhite, ": ", info.sys.datetime.format(
"yyyy-MM-dd HH:mm:ss")
tb.setCursorXPos 70
if hist > 0:
tb.write " HIST: ", $hist
tb.writeR fmt"PROCS: {$info.pidsInfo.len} "
tb.setCursorPos(offset, 2)
if info.cpu.cpu > cpuLimit:
Expand Down Expand Up @@ -85,9 +87,13 @@ proc help(tb: var TerminalBuffer, curSort: SortField, scrollX, scrollY: int) =
tb.setCursorXPos 0+tb.getCursorXPos()

tb.write " ", HelpCol, "/", fgNone, " - filter "
if count() > 0:
tb.write " ", HelpCol, "[]", fgNone, " - timeshift "
else:
tb.write " ", styleDim, "[] - timeshift ", styleBright, fgNone
tb.write " ", HelpCol, "Q", fgNone, " - quit "

tb.setForegroundColor(fgBlack, true)
# tb.setForegroundColor(fgBlack, true)
let x = tb.getCursorXPos()

let (w, h) = terminalSize()
Expand All @@ -101,7 +107,7 @@ proc help(tb: var TerminalBuffer, curSort: SortField, scrollX, scrollY: int) =

if x + 15 < w:
tb.setCursorXPos(w - 15)
tb.write fmt " W: {w} H: {h} "
tb.write fmt " WH: {w}x{h} "

proc table(tb: var TerminalBuffer, pi: OrderedTable[uint, PidInfo],
curSort: SortField, scrollX, scrollY: int,
Expand Down Expand Up @@ -156,19 +162,23 @@ proc filter(tb: var TerminalBuffer, filter: string) =
tb.write " ", HelpCol, "Esc", fgNone, " - Back Filter: ", bgBlue, filter[
1..^1], bgNone

proc redraw(curSort: SortField, scrollX, scrollY: int, filter: string) =
proc redraw(curSort: SortField, scrollX, scrollY: int, filter: string, hist: int) =
let (w, h) = terminalSize()
var tb = newTerminalBuffer(w, h)

let info = fullInfo(curSort)
let info =
if hist == 0:
fullInfo(curSort)
else:
hist(hist)

if info.cpu.cpu >= cpuCoreLimit:
tb.setForegroundColor(fgRed, true)
else:
tb.setForegroundColor(fgBlack, true)
tb.drawRect(0, 0, w-1, h-1)

header(tb, info)
header(tb, info, hist)
table(tb, info.pidsInfo, curSort, scrollX, scrollY, filter)
if filter.len > 0:
filter(tb, filter)
Expand All @@ -181,10 +191,11 @@ proc run*() =
setControlCHook(exitProc)
hideCursor()
var draw = false
var hist = 0
var curSort = Cpu
var scrollX, scrollY = 0
var filter = ""
redraw(curSort, scrollX, scrollY, filter)
redraw(curSort, scrollX, scrollY, filter, hist)

var refresh = 0
while true:
Expand Down Expand Up @@ -216,6 +227,12 @@ proc run*() =
of Key.N: curSort = Name; draw = true
of Key.C: curSort = Cpu; draw = true
of Key.Slash: filter = " "; draw = true
of Key.LeftBracket:
inc hist
draw = true
of Key.RightBracket:
if hist > 0: dec hist
draw = true
else: discard
else:
case key
Expand All @@ -238,11 +255,12 @@ proc run*() =
else: discard

if draw or refresh == 10:
redraw(curSort, scrollX, scrollY, filter)
redraw(curSort, scrollX, scrollY, filter, hist)
refresh = 0
if not draw:
if draw:
draw = false
else:
sleep 100
draw = false
else:
inc refresh
sleep 100
Expand Down
4 changes: 3 additions & 1 deletion ttop.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ bin = @["ttop"]
requires "nim >= 1.6.4"

requires "terminaltables"
requires "illwill"
requires "illwill"
requires "zippy"

0 comments on commit 08634a9

Please sign in to comment.