Skip to content

Commit

Permalink
fix empty blog
Browse files Browse the repository at this point in the history
  • Loading branch information
inv2004 committed Dec 18, 2022
1 parent c2e42f8 commit 286684e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
15 changes: 10 additions & 5 deletions src/ttop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import ttop/save
import os

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

proc main() =
if isSave():
save()
else:
run()
try:
if isSave():
save()
else:
run()
except:
let ex = getCurrentException()
echo ex.msg
echo ex.getStackTrace()

when isMainModule:
main()
25 changes: 13 additions & 12 deletions src/ttop/save.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ type StatV1* = object
mem*: uint
io*: uint

proc blogPath(mode = fmRead): string =
let dir = getCacheDir("ttop")
if mode != fmRead and not dirExists(dir):
createDir(dir)
os.joinPath(dir, now().format("yyyy-MM-dd")).addFileExt("blog")

proc saveStat*(s: FileStream, f: FullInfoRef) =
var io: uint = 0
for _, disk in f.disk:
Expand Down Expand Up @@ -67,7 +61,7 @@ proc hist*(ii: int, blog: string): (FullInfoRef, seq[StatV1]) =
else:
result[0] = fullInfo()

proc moveBlog*(d: int, b: string, hist, cnt: int): (string, int) =
proc moveBlog*(d: int, b: string, hist, cnt: int, toSave = false): (string, int) =
if d < 0 and hist == 0 and cnt > 0:
return (b, cnt)
elif d < 0 and hist > 1:
Expand All @@ -77,7 +71,14 @@ proc moveBlog*(d: int, b: string, hist, cnt: int): (string, int) =
let dir = getCacheDir("ttop")
let files = sorted toSeq(walkFiles(os.joinPath(dir, "*.blog")))
if d == 0 or b == "":
return (files[^1], 0)
if files.len > 0:
return (files[^1], 0)
elif toSave:
if not dirExists(dir):
createDir(dir)
return (os.joinPath(dir, now().format("yyyy-MM-dd")).addFileExt("blog"), 0)
else:
return ("", 0)
else:
let idx = files.find(b)
if d < 0:
Expand All @@ -94,13 +95,13 @@ proc moveBlog*(d: int, b: string, hist, cnt: int): (string, int) =
doAssert false

proc save*() =
var (prev, _) = hist(-1, moveBlog(0, "", 0, 0)[0])
var blog = moveBlog(0, "", 0, 0, toSave = true)[0]
var (prev, _) = hist(-1, blog)
let info = if prev == nil: fullInfo() else: fullInfo(prev)
let buf = compress($$info[])
let path = blogPath(fmAppend)
let s = newFileStream(path, fmAppend)
let s = newFileStream(blog, fmAppend)
if s == nil:
raise newException(IOError, "cannot open " & path)
raise newException(IOError, "cannot open " & blog)
defer: s.close()
s.saveStat info
s.write buf.len.uint32
Expand Down
4 changes: 4 additions & 0 deletions src/ttop/tui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ proc header(tb: var TerminalBuffer, info: FullInfoRef, hist, cnt: int,
let blogShort = extractFilename blog
if hist > 0:
tb.write fmt" {blogShort}: {hist} / {cnt}"
elif blog == "":
tb.write fmt" autoupdate log: disabled"
else:
tb.write fmt" autoupdate {blogShort}: {cnt}"
tb.writeR fmt"PROCS: {$info.pidsInfo.len} "
Expand Down Expand Up @@ -313,6 +315,8 @@ proc run*() =
else: discard

if draw or refresh == 10:
if hist == 0:
blog = moveBlog(+1, blog, stats.len, stats.len)[0]
(info, stats) = hist(hist, blog)
redraw(info, curSort, scrollX, scrollY, filter, hist, stats, blog)
refresh = 0
Expand Down

0 comments on commit 286684e

Please sign in to comment.