-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add styledWrite macro #8047
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
Add styledWrite macro #8047
Conversation
|
This was my naive approach for refactoring macro styledWriteLine*(f: File, args: varargs[typed]): untyped =
## Similar to ``writeLine``, but treating terminal style arguments specially.
## When some argument is ``Style``, ``set[Style]``, ``ForegroundColor``,
## ``BackgroundColor`` or ``TerminalCmd`` then it is not sent directly to
## ``f``, but instead corresponding terminal style proc is called.
##
## Example:
##
## .. code-block:: nim
##
## proc error(msg: string) =
## styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
##
result = newCall(bindSym"styledWrite")
result.add(f)
for arg in children(args):
result.add(arg)
result.add(newCall(bindSym"write", f, newStrLitNode("\n"))) |
6837e5d to
8d9cc55
Compare
|
You can fix the refactored approach like so: macro styledWriteLine*(f: File, args: varargs[typed]): untyped =
# result should be a statement list
result = newStmtList()
# create new call with `f` as first argument
result.add newCall(bindSym"styledWrite", f)
for arg in children(args):
# append all arguments to the `nnkCall` node
result[^1].add(arg)
result.add(newCall(bindSym"write", f, newStrLitNode("\n"))) |
|
Earlier when stdout.styledWriteLine(fgRed, "red text ")
stdout.styledWriteLine(fgWhite, bgRed, "white text in red background")
stdout.styledWriteLine(" ordinary text ")
stdout.styledWriteLine(fgGreen, "green text")resulted in: (Above the underlined "styled text" was blinking and so wasn't caught in that screenshot :)) Now the same test, with |
|
Except for the "callsite is deprecated" warning that was already there, I am happy with this PR now. Would be great if someone can review this further. |
|
just my two cents for the example, you changed |
Just to clarify, the As
I completely agree with that. But this example is just about displaying colorful text on stdout.
I believe, the application developer should consciously prevent adding colors based on |
|
I mean there are things that are obviously still wrong, but they have never been in a better state either. When two threads call styledWriteLine at the same time, the lines can be interleaved. With for example printf, this can't happen. Neither can it happen with |
Also: - Move the tests block to the end of the file - Fix the older tests - Add tests for existing styledEcho - Add new tests for styledWrite Fixes nim-lang#8046.
This also fixes a bug in the styledWriteLine behavior where the background color leaked onto the next newline if that command did not end with resetStyle. Now it is not necessary to end styledWriteLine calls that set BackgroundColor to end in resetStyle.
d427f29 to
abbf9ba
Compare
|
Looks good :) |


Also:
Fixes #8046.
This commit works, but I don't understand the macro syntax, and would like
styledWriteLineto reusestyledWritethat I added.Let me know if that optimization needs to be made before this can be committed.. or if someone can optimize that after this gets committed.
Also, need to fix these warnings:
Thanks!