summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-06-15 14:51:20 +0200
committerAndreas Rumpf <rumpf_a@web.de>2015-06-15 14:51:20 +0200
commit0272da01881dc81f3095d0dfeda2ff78f04e2618 (patch)
treed008571589b03c4a8742662be68e9465ce28e071 /lib
parent8c671d22d6a7c55e13e7c02eb4fd65bbabb52131 (diff)
parentb4d3a27753e03b1da3b510e651378077ec4d1879 (diff)
downloadNim-0272da01881dc81f3095d0dfeda2ff78f04e2618.tar.gz
Merge pull request #2906 from nanoant/patch/more-about-colors
More flexible msg colors
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/terminal.nim35
1 files changed, 28 insertions, 7 deletions
diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim
index 15e2eefec..2efdf72d5 100644
--- a/lib/pure/terminal.nim
+++ b/lib/pure/terminal.nim
@@ -406,22 +406,43 @@ proc isatty*(f: File): bool =
 
   result = isatty(getFileHandle(f)) != 0'i32
 
-proc styledEchoProcessArg(s: string) = write stdout, s
-proc styledEchoProcessArg(style: Style) = setStyle({style})
-proc styledEchoProcessArg(style: set[Style]) = setStyle style
-proc styledEchoProcessArg(color: ForegroundColor) = setForegroundColor color
-proc styledEchoProcessArg(color: BackgroundColor) = setBackgroundColor color
+type
+  TerminalCmd* = enum  ## commands that can be expressed as arguments
+    resetStyle         ## reset attributes
+
+template styledEchoProcessArg(s: string) = write stdout, s
+template styledEchoProcessArg(style: Style) = setStyle({style})
+template styledEchoProcessArg(style: set[Style]) = setStyle style
+template styledEchoProcessArg(color: ForegroundColor) = setForegroundColor color
+template styledEchoProcessArg(color: BackgroundColor) = setBackgroundColor color
+template styledEchoProcessArg(cmd: TerminalCmd) =
+  when cmd == resetStyle:
+    resetAttributes()
 
 macro styledEcho*(m: varargs[expr]): stmt =
   ## to be documented.
   let m = callsite()
+  var reset = false
   result = newNimNode(nnkStmtList)
 
   for i in countup(1, m.len - 1):
-    result.add(newCall(bindSym"styledEchoProcessArg", m[i]))
+    let item = m[i]
+    case item.kind
+    of nnkStrLit..nnkTripleStrLit:
+      if i == m.len - 1:
+        # optimize if string literal is last, just call writeln
+        result.add(newCall(bindSym"writeln", bindSym"stdout", item))
+        if reset: result.add(newCall(bindSym"resetAttributes"))
+        return
+      else:
+        # if it is string literal just call write, do not enable reset
+        result.add(newCall(bindSym"write", bindSym"stdout", item))
+    else:
+      result.add(newCall(bindSym"styledEchoProcessArg", item))
+      reset = true
 
   result.add(newCall(bindSym"write", bindSym"stdout", newStrLitNode("\n")))
-  result.add(newCall(bindSym"resetAttributes"))
+  if reset: result.add(newCall(bindSym"resetAttributes"))
 
 when defined(nimdoc):
   proc getch*(): char =