colors: Introduce resetStyle enum & use templates
1. Introduce TerminalCmd enum and resetStyle that can be issued to issue resetAttributes within styledEcho arguments. 2. Use templates to resolve styledEcho arguments for performance reasons. 3. Try to avoid calling trailing write "\n" and reset attributes where possible.
This commit is contained in:
parent
806dfe976d
commit
1057770ce5
1 changed files with 28 additions and 7 deletions
|
|
@ -406,22 +406,43 @@ proc isatty*(f: File): bool =
|
||||||
|
|
||||||
result = isatty(getFileHandle(f)) != 0'i32
|
result = isatty(getFileHandle(f)) != 0'i32
|
||||||
|
|
||||||
proc styledEchoProcessArg(s: string) = write stdout, s
|
type
|
||||||
proc styledEchoProcessArg(style: Style) = setStyle({style})
|
TerminalCmd* = enum ## commands that can be expressed as arguments
|
||||||
proc styledEchoProcessArg(style: set[Style]) = setStyle style
|
resetStyle ## reset attributes
|
||||||
proc styledEchoProcessArg(color: ForegroundColor) = setForegroundColor color
|
|
||||||
proc styledEchoProcessArg(color: BackgroundColor) = setBackgroundColor color
|
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 =
|
macro styledEcho*(m: varargs[expr]): stmt =
|
||||||
## to be documented.
|
## to be documented.
|
||||||
let m = callsite()
|
let m = callsite()
|
||||||
|
var reset = false
|
||||||
result = newNimNode(nnkStmtList)
|
result = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
for i in countup(1, m.len - 1):
|
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"write", bindSym"stdout", newStrLitNode("\n")))
|
||||||
result.add(newCall(bindSym"resetAttributes"))
|
if reset: result.add(newCall(bindSym"resetAttributes"))
|
||||||
|
|
||||||
when defined(nimdoc):
|
when defined(nimdoc):
|
||||||
proc getch*(): char =
|
proc getch*(): char =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue