summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.comy>2011-10-07 16:34:04 +0300
committerZahary Karadjov <zahary@gmail.comy>2011-10-07 17:10:04 +0300
commit4a444bf6dbf973faea020b1e82650e50eccf7d54 (patch)
tree660df418fce1076c16cd7ab97574d0c6c12c6605 /lib
parente3deb5b502888fce847ef21b86bbfc7e91c7dea4 (diff)
downloadNim-4a444bf6dbf973faea020b1e82650e50eccf7d54.tar.gz
*Pimped up* the test runner with colorful output
Added: terminal.styledEcho macro
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/terminal.nim38
-rw-r--r--lib/pure/unittest.nim8
2 files changed, 39 insertions, 7 deletions
diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim
index 232601640..ab9c31c8f 100755
--- a/lib/pure/terminal.nim
+++ b/lib/pure/terminal.nim
@@ -14,6 +14,8 @@
 ## Changing the style is permanent even after program termination! Use the

 ## code ``system.addQuitProc(resetAttributes)`` to restore the defaults.

 

+import macros

+

 when defined(windows):

   import windows, os

 

@@ -210,24 +212,32 @@ type
 

 when not defined(windows):

   var

+    # XXX: These better be thread-local

     gFG = 0

     gBG = 0

 

-proc WriteStyled*(txt: string, style: set[TStyle] = {styleBright}) =

-  ## writes the text `txt` in a given `style`.

+proc setStyle*(style: set[TStyle]) =

+  ## sets the terminal style

   when defined(windows):

     var a = 0'i16

     if styleBright in style: a = a or int16(FOREGROUND_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 styleUnderscore in style: a = a or 0x8000'i16 # COMMON_LVB_UNDERSCORE

-    var old = getAttributes()

     discard SetConsoleTextAttribute(conHandle, old or a)

-    stdout.write(txt)

-    discard SetConsoleTextAttribute(conHandle, old)

   else:

     for s in items(style):

       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)

     resetAttributes()

     if gFG != 0:

@@ -298,6 +308,24 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
     if bright: inc(gBG, 60)

     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:

   system.addQuitProc(resetAttributes)

   write(stdout, "never mind")

diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim
index 2322ffd69..db3e5a1db 100644
--- a/lib/pure/unittest.nim
+++ b/lib/pure/unittest.nim
@@ -18,7 +18,7 @@
 ##

 

 import

-  macros

+  macros, terminal

 

 type

   TestStatus* = enum OK, FAILED

@@ -45,6 +45,10 @@ template suite*(name: expr, body: stmt): stmt =
 

     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 =

   if bind shouldRun(name):

     bind checkpoints = @[]

@@ -56,7 +60,7 @@ template test*(name: expr, body: stmt): stmt =
 

     finally:

       TestTeardownIMPL()

-      echo "[" & $TestStatusIMPL & "] " & name & "\n"

+      printStatus(TestStatusIMPL, name)

 

 proc checkpoint*(msg: string) =

   checkpoints.add(msg)