Skip to content

Commit fb71d46

Browse files
Madman10Kalehander92
authored andcommitted
refactor(src/codetracer.nim): split codetracer.nim into multiple files, removed obsolete functionality and added some Windows code
1 parent 094f88a commit fb71d46

19 files changed

+1402
-1855
lines changed

src/ct/cli/help.nim

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import
2+
std/[ osproc ],
3+
../../common/[paths]
4+
5+
proc displayHelp*: void =
6+
# echo "help: TODO"
7+
let process = startProcess(codetracerExe, args = @["--help"], options = {poParentStreams})
8+
let code = waitForExit(process)
9+
quit(code)

src/ct/cli/interactive_replay.nim

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import
2+
std/[strutils, strformat, sequtils, algorithm, rdstdin],
3+
../../common/[ trace_index, types, lang ],
4+
../trace/[ run, storage_and_import ],
5+
../utilities/[ env ],
6+
../codetracerconf,
7+
json_serialization
8+
9+
const
10+
TRACE_CMD_COLUMN_WIDTH = 70
11+
TRACE_WORKDIR_COLUMN_WIDTH = 40
12+
13+
func limitColumnLeft(text: string, width: int): string =
14+
if text.len > width:
15+
".." & text[text.len - (width - 2) .. ^1]
16+
else:
17+
text
18+
19+
20+
func limitColumnRight(text: string, width: int): string =
21+
if text.len > width:
22+
text[0 .. (width - 2) - 1] & ".."
23+
else:
24+
text
25+
26+
27+
func traceInText*(trace: Trace): string =
28+
let displayCmd = limitColumnRight(trace.program & " " & trace.args.join(" "), TRACE_CMD_COLUMN_WIDTH)
29+
let displayWorkdir = limitColumnLeft("ran in " & trace.workdir, TRACE_WORKDIR_COLUMN_WIDTH)
30+
let idColumn = fmt"{trace.id}."
31+
alignLeft(idColumn, 5) & " | " & alignLeft(displayCmd, TRACE_CMD_COLUMN_WIDTH) & " | " &
32+
alignLeft(displayWorkdir, TRACE_WORKDIR_COLUMN_WIDTH) & " | " &
33+
alignLeft(toName(trace.lang), 15) & " | " & alignLeft(trace.date, 15)
34+
35+
36+
func tracesInText*(traces: seq[Trace]): string =
37+
traces.reversed.mapIt(traceInText(it)).join("\n")
38+
39+
40+
func tracesInJson*(traces: seq[Trace]): string =
41+
Json.encode(traces)
42+
43+
44+
proc interactiveReplayMenu*(command: StartupCommand) =
45+
let recordCore = envLoadRecordCore()
46+
# ordered by id
47+
# returns the newest(biggest id) first
48+
let traces = trace_index.all(test=false)
49+
let limitedTraces = if traces.len > 10:
50+
traces[0 ..< 10]
51+
else:
52+
traces
53+
54+
echo "Select a trace to replay, entering its id:"
55+
echo ""
56+
57+
for trace in limitedTraces:
58+
echo traceInText(trace)
59+
60+
if traces.len > 10:
61+
echo "..(older traces not shown)"
62+
63+
echo ""
64+
65+
while true:
66+
let raw = readLineFromStdin("replay: ")
67+
try:
68+
let traceId = raw.parseInt
69+
let trace = trace_index.find(traceId, test=false)
70+
if not trace.isNil:
71+
if command != StartupCommand.upload:
72+
discard runRecordedTrace(trace, test=false, recordCore=recordCore)
73+
else:
74+
uploadTrace(trace)
75+
break
76+
else:
77+
echo fmt"trace with id {traceId} not found in local codetracer db, please try again"
78+
except:
79+
echo "error: ", getCurrentExceptionMsg()
80+
echo "please try again"

src/ct/cli/list.nim

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import
2+
../../common/trace_index,
3+
logging,
4+
interactive_replay
5+
6+
type
7+
ListFormat = enum FormatText, FormatJson
8+
ListTarget {.pure.} = enum Local, Remote
9+
10+
11+
proc parseListFormat(arg: string): ListFormat =
12+
if arg == "text":
13+
FormatText
14+
elif arg == "json":
15+
FormatJson
16+
else:
17+
errorMessage "error: expected --format text/json"
18+
quit(1)
19+
20+
21+
proc parseListTarget(arg: string): ListTarget =
22+
if arg == "local":
23+
ListTarget.Local
24+
elif arg == "remote":
25+
ListTarget.Remote
26+
else:
27+
errorMessage "error: expected local or remote"
28+
quit(1)
29+
30+
31+
proc listLocalTraces(format: ListFormat) =
32+
let traces = trace_index.all(test=false)
33+
case format:
34+
of FormatText:
35+
echo tracesInText(traces)
36+
of FormatJson:
37+
echo tracesInJson(traces)
38+
39+
40+
proc listCommand*(rawTarget: string, rawFormat: string) =
41+
# list [local/remote (default local)] [--format text/json (default text)]
42+
let target = parseListTarget(rawTarget)
43+
let format = parseListFormat(rawFormat)
44+
case target:
45+
of ListTarget.Local:
46+
listLocalTraces(format)
47+
of ListTarget.Remote:
48+
echo "error: unsupported currently!"
49+
# listRemoteTraces(format)

src/ct/cli/logging.nim

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import std/[ strformat ]
2+
3+
template errorMessage*(message: string) =
4+
echo message
5+
6+
proc notSupportedCommand*(commandName: string) =
7+
echo fmt"{commandName} not supported with this backend"

0 commit comments

Comments
 (0)