*Pimped up* the test runner with colorful output
Added: terminal.styledEcho macro
This commit is contained in:
parent
e3deb5b502
commit
4a444bf6db
2 changed files with 39 additions and 7 deletions
|
|
@ -14,6 +14,8 @@
|
||||||
## Changing the style is permanent even after program termination! Use the
|
## Changing the style is permanent even after program termination! Use the
|
||||||
## code ``system.addQuitProc(resetAttributes)`` to restore the defaults.
|
## code ``system.addQuitProc(resetAttributes)`` to restore the defaults.
|
||||||
|
|
||||||
|
import macros
|
||||||
|
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
import windows, os
|
import windows, os
|
||||||
|
|
||||||
|
|
@ -210,24 +212,32 @@ type
|
||||||
|
|
||||||
when not defined(windows):
|
when not defined(windows):
|
||||||
var
|
var
|
||||||
|
# XXX: These better be thread-local
|
||||||
gFG = 0
|
gFG = 0
|
||||||
gBG = 0
|
gBG = 0
|
||||||
|
|
||||||
proc WriteStyled*(txt: string, style: set[TStyle] = {styleBright}) =
|
proc setStyle*(style: set[TStyle]) =
|
||||||
## writes the text `txt` in a given `style`.
|
## sets the terminal style
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
var a = 0'i16
|
var a = 0'i16
|
||||||
if styleBright in style: a = a or int16(FOREGROUND_INTENSITY)
|
if styleBright in style: a = a or int16(FOREGROUND_INTENSITY)
|
||||||
if styleBlink in style: a = a or int16(BACKGROUND_INTENSITY)
|
if styleBlink in style: a = a or int16(BACKGROUND_INTENSITY)
|
||||||
if styleReverse in style: a = a or 0x4000'i16 # COMMON_LVB_REVERSE_VIDEO
|
if styleReverse in style: a = a or 0x4000'i16 # COMMON_LVB_REVERSE_VIDEO
|
||||||
if styleUnderscore in style: a = a or 0x8000'i16 # COMMON_LVB_UNDERSCORE
|
if styleUnderscore in style: a = a or 0x8000'i16 # COMMON_LVB_UNDERSCORE
|
||||||
var old = getAttributes()
|
|
||||||
discard SetConsoleTextAttribute(conHandle, old or a)
|
discard SetConsoleTextAttribute(conHandle, old or a)
|
||||||
stdout.write(txt)
|
|
||||||
discard SetConsoleTextAttribute(conHandle, old)
|
|
||||||
else:
|
else:
|
||||||
for s in items(style):
|
for s in items(style):
|
||||||
stdout.write("\e[" & $ord(s) & 'm')
|
stdout.write("\e[" & $ord(s) & 'm')
|
||||||
|
|
||||||
|
proc WriteStyled*(txt: string, style: set[TStyle] = {styleBright}) =
|
||||||
|
## writes the text `txt` in a given `style`.
|
||||||
|
when defined(windows):
|
||||||
|
var old = getAttributes()
|
||||||
|
setStyle(style)
|
||||||
|
stdout.write(txt)
|
||||||
|
discard SetConsoleTextAttribute(conHandle, old)
|
||||||
|
else:
|
||||||
|
setStyle(style)
|
||||||
stdout.write(txt)
|
stdout.write(txt)
|
||||||
resetAttributes()
|
resetAttributes()
|
||||||
if gFG != 0:
|
if gFG != 0:
|
||||||
|
|
@ -298,6 +308,24 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
|
||||||
if bright: inc(gBG, 60)
|
if bright: inc(gBG, 60)
|
||||||
stdout.write("\e[" & $gBG & 'm')
|
stdout.write("\e[" & $gBG & 'm')
|
||||||
|
|
||||||
|
# XXX:
|
||||||
|
# These should be private, but there is no yet
|
||||||
|
# facility for binding local symbols within macros
|
||||||
|
proc styledEchoProcessArg*(s: string) = write stdout, s
|
||||||
|
proc styledEchoProcessArg*(style: TStyle) = setStyle {style}
|
||||||
|
proc styledEchoProcessArg*(style: set[TStyle]) = setStyle style
|
||||||
|
proc styledEchoProcessArg*(color: TForegroundColor) = setForeGroundColor color
|
||||||
|
proc styledEchoProcessArg*(color: TBackgroundColor) = setBackGroundColor color
|
||||||
|
|
||||||
|
macro styledEcho*(m: stmt): stmt =
|
||||||
|
result = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
|
for i in countup(1, m.len - 1):
|
||||||
|
result.add(newCall(!"styledEchoProcessArg", m[i]))
|
||||||
|
|
||||||
|
result.add(newCall(!"write", newIdentNode("stdout"), newStrLitNode("\n")))
|
||||||
|
result.add(newCall(!"resetAttributes"))
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
system.addQuitProc(resetAttributes)
|
system.addQuitProc(resetAttributes)
|
||||||
write(stdout, "never mind")
|
write(stdout, "never mind")
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
##
|
##
|
||||||
|
|
||||||
import
|
import
|
||||||
macros
|
macros, terminal
|
||||||
|
|
||||||
type
|
type
|
||||||
TestStatus* = enum OK, FAILED
|
TestStatus* = enum OK, FAILED
|
||||||
|
|
@ -45,6 +45,10 @@ template suite*(name: expr, body: stmt): stmt =
|
||||||
|
|
||||||
body
|
body
|
||||||
|
|
||||||
|
proc printStatus*(s: TestStatus, name: string) =
|
||||||
|
var color = (if s == OK: fgGreen else: fgRed)
|
||||||
|
styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"
|
||||||
|
|
||||||
template test*(name: expr, body: stmt): stmt =
|
template test*(name: expr, body: stmt): stmt =
|
||||||
if bind shouldRun(name):
|
if bind shouldRun(name):
|
||||||
bind checkpoints = @[]
|
bind checkpoints = @[]
|
||||||
|
|
@ -56,7 +60,7 @@ template test*(name: expr, body: stmt): stmt =
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
TestTeardownIMPL()
|
TestTeardownIMPL()
|
||||||
echo "[" & $TestStatusIMPL & "] " & name & "\n"
|
printStatus(TestStatusIMPL, name)
|
||||||
|
|
||||||
proc checkpoint*(msg: string) =
|
proc checkpoint*(msg: string) =
|
||||||
checkpoints.add(msg)
|
checkpoints.add(msg)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue