Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdlib: clean up coloredResult #962

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions lib/experimental/colortext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -700,27 +700,18 @@ template coloredResult*(indentationStep: int = 2): untyped =
##
## aux(node)
##
## Injected procs
## Injected routines
##
## - `addf(string, varargs)`
## - `add(string)`, `add(string, string)`
## - `addIndent(int)`, `addi(int, string)`
## - `endResult()` - return colored result. Required for proper work
## of the code in the compile-time context, otherwise modification
## of the `addr result` does not work properly.
static:
when not declared(result):
{.error: "'coloredResult' template can only be called inside of the procedure returning 'ColText' as a result, or other environment that has `var result: ColText` defined."}
when not declared(result):
{.error: "'coloredResult' template can only be called in an environment " &
"where a `result` variable is available"}

var outPtr {.used.}: ptr ColText = addr result

template endResult(): untyped {.used.} =
when nimvm:
return outPtr[]

else:
return

proc addf(format: string, args: varargs[ColText, toColText]) =
template addf(format: string, args: varargs[untyped]) {.used.} =
outPtr[].addf(format, args)

template add(arg: untyped): untyped {.used.} = outPtr[].add arg
Expand Down
1 change: 0 additions & 1 deletion tests/stdlib/algorithm/tcolortext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ let exp: int
add "1" + fgRed
addi 2, "2"
add "]"
endResult()

let col = reesc($colres())
let want = r"[ 0\e[31m1\e[39m 2]"
Expand Down
Loading