|
| 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" |
0 commit comments