16
16
# # Similarly, if you hide the cursor, make sure to unhide it with
17
17
# # `showCursor` before quitting.
18
18
19
+ ##[
20
+ ## Playing with colorful and styled text
21
+ ]##
22
+
23
+ # # Procs like `styledWriteLine`, `styledEcho` etc. have a temporary effect on
24
+ # # text parameters. Style parameters only affect the text parameter right after them.
25
+ # # After being called, these procs will reset the default style of the terminal.
26
+ # # While `setBackGroundColor`, `setForeGroundColor` etc. have a lasting
27
+ # # influence on the terminal, you can use `resetAttributes` to
28
+ # # reset the default style of the terminal.
29
+ runnableExamples(" -r:off" ):
30
+ stdout.styledWriteLine({styleBright, styleBlink, styleUnderscore}, " styled text " )
31
+ stdout.styledWriteLine(fgRed, " red text " )
32
+ stdout.styledWriteLine(fgWhite, bgRed, " white text in red background" )
33
+ stdout.styledWriteLine(" ordinary text without style " )
34
+
35
+ stdout.setBackGroundColor(bgCyan, true )
36
+ stdout.setForeGroundColor(fgBlue)
37
+ stdout.write(" blue text in cyan background" )
38
+ stdout.resetAttributes()
39
+
40
+ # You can specify multiple text parameters. Style parameters
41
+ # only affect the text parameter right after them.
42
+ styledEcho styleBright, fgGreen, " [PASS]" , resetStyle, fgGreen, " Yay!"
43
+
44
+ stdout.styledWriteLine(fgRed, " red text " , styleBright, " bold red" , fgDefault, " bold text" )
45
+
46
+
19
47
import macros
20
48
import strformat
21
49
from strutils import toLowerAscii, `%`
@@ -418,6 +446,9 @@ else:
418
446
419
447
proc eraseLine* (f: File) =
420
448
# # Erases the entire current line.
449
+ runnableExamples(" -r:off" ):
450
+ write(stdout, " never mind" )
451
+ stdout.eraseLine() # nothing will be printed on the screen
421
452
when defined(windows):
422
453
let h = conHandle(f)
423
454
var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
@@ -480,7 +511,7 @@ proc resetAttributes*(f: File) =
480
511
gBG = 0
481
512
482
513
type
483
- Style* = enum # # different styles for text output
514
+ Style* = enum # # Different styles for text output.
484
515
styleBright = 1 , # # bright text
485
516
styleDim, # # dim text
486
517
styleItalic, # # italic (or reverse on terminals not supporting)
@@ -534,7 +565,7 @@ proc writeStyled*(txt: string, style: set[Style] = {styleBright}) =
534
565
stdout.write(ansiStyleCode(gBG))
535
566
536
567
type
537
- ForegroundColor* = enum # # terminal 's foreground colors
568
+ ForegroundColor* = enum # # Terminal 's foreground colors.
538
569
fgBlack = 30 , # # black
539
570
fgRed, # # red
540
571
fgGreen, # # green
546
577
fg8Bit, # # 256-color (not supported, see `enableTrueColors` instead.)
547
578
fgDefault # # default terminal foreground color
548
579
549
- BackgroundColor* = enum # # terminal 's background colors
580
+ BackgroundColor* = enum # # Terminal 's background colors.
550
581
bgBlack = 40 , # # black
551
582
bgRed, # # red
552
583
bgGreen, # # green
@@ -701,14 +732,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
701
732
# # When some argument is `Style`, `set[Style]`, `ForegroundColor`,
702
733
# # `BackgroundColor` or `TerminalCmd` then it is not sent directly to
703
734
# # `f`, but instead corresponding terminal style proc is called.
704
- # #
705
- # # Example:
706
- # #
707
- # # .. code-block:: nim
708
- # #
709
- # # stdout.styledWrite(fgRed, "red text ")
710
- # # stdout.styledWrite(fgGreen, "green text")
711
- # #
735
+ runnableExamples(" -r:off" ):
736
+ stdout.styledWrite(fgRed, " red text " )
737
+ stdout.styledWrite(fgGreen, " green text" )
738
+
712
739
var reset = false
713
740
result = newNimNode(nnkStmtList)
714
741
@@ -731,14 +758,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
731
758
732
759
template styledWriteLine* (f: File, args: varargs [untyped ]) =
733
760
# # Calls `styledWrite` and appends a newline at the end.
734
- # #
735
- # # Example:
736
- # #
737
- # # .. code-block:: nim
738
- # #
739
- # # proc error(msg: string) =
740
- # # styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
741
- # #
761
+ runnableExamples:
762
+ proc error(msg: string ) =
763
+ styledWriteLine(stderr, fgRed, " Error: " , resetStyle, msg)
764
+
742
765
styledWrite(f, args)
743
766
write(f, " \n " )
744
767
@@ -747,7 +770,7 @@ template styledEcho*(args: varargs[untyped]) =
747
770
stdout.styledWriteLine(args)
748
771
749
772
proc getch* (): char =
750
- # # Read a single character from the terminal, blocking until it is entered.
773
+ # # Reads a single character from the terminal, blocking until it is entered.
751
774
# # The character is not printed to the terminal.
752
775
when defined(windows):
753
776
let fd = getStdHandle(STD_INPUT_HANDLE)
@@ -852,7 +875,7 @@ when defined(windows):
852
875
import os
853
876
854
877
proc enableTrueColors* () =
855
- # # Enable true color.
878
+ # # Enables true color.
856
879
var term = getTerminal()
857
880
when defined(windows):
858
881
var
@@ -885,7 +908,7 @@ proc enableTrueColors*() =
885
908
term.trueColorIsEnabled = term.trueColorIsSupported
886
909
887
910
proc disableTrueColors* () =
888
- # # Disable true color.
911
+ # # Disables true color.
889
912
var term = getTerminal()
890
913
when defined(windows):
891
914
if term.trueColorIsSupported:
@@ -902,51 +925,3 @@ proc newTerminal(): owned(PTerminal) =
902
925
new result
903
926
when defined(windows):
904
927
initTerminal(result )
905
-
906
- when not defined(testing) and isMainModule :
907
- assert ansiStyleCode(styleBright) == " \e [1m"
908
- assert ansiStyleCode(styleStrikethrough) == " \e [9m"
909
- # exitprocs.addExitProc(resetAttributes)
910
- write(stdout, " never mind" )
911
- stdout.eraseLine()
912
- stdout.styledWriteLine({styleBright, styleBlink, styleUnderscore}, " styled text " )
913
- stdout.styledWriteLine(" italic text " , {styleItalic})
914
- stdout.setBackGroundColor(bgCyan, true )
915
- stdout.setForeGroundColor(fgBlue)
916
- stdout.write(" blue text in cyan background" )
917
- stdout.resetAttributes()
918
- echo " "
919
- stdout.writeLine(" ordinary text" )
920
- echo " more ordinary text"
921
- styledEcho styleBright, fgGreen, " [PASS]" , resetStyle, fgGreen, " Yay!"
922
- echo " ordinary text again"
923
- styledEcho styleBright, fgRed, " [FAIL]" , resetStyle, fgRed, " Nay :("
924
- echo " ordinary text again"
925
- setForeGroundColor(fgGreen)
926
- echo " green text"
927
- echo " more green text"
928
- setForeGroundColor(fgBlue)
929
- echo " blue text"
930
- resetAttributes()
931
- echo " ordinary text"
932
-
933
- stdout.styledWriteLine(fgRed, " red text " )
934
- stdout.styledWriteLine(fgWhite, bgRed, " white text in red background" )
935
- stdout.styledWriteLine(" ordinary text " )
936
- stdout.styledWriteLine(fgGreen, " green text" )
937
-
938
- writeStyled(" underscored text" , {styleUnderscore})
939
- stdout.styledWrite(fgRed, " red text " )
940
- writeStyled(" bright text " , {styleBright})
941
- echo " ordinary text"
942
-
943
- stdout.styledWrite(fgRed, " red text " )
944
- stdout.styledWrite(fgWhite, bgRed, " white text in red background" )
945
- stdout.styledWrite(" ordinary text " )
946
- stdout.styledWrite(fgGreen, " green text" )
947
- echo " "
948
- echo " ordinary text"
949
- stdout.styledWriteLine(fgRed, " red text " , styleBright, " bold red" , fgDefault, " bold text" )
950
- stdout.styledWriteLine(bgYellow, " text in yellow bg" , styleBright,
951
- " bold text in yellow bg" , bgDefault, " bold text" )
952
- echo " ordinary text"
0 commit comments