Skip to content

Commit e45b858

Browse files
authored
[std/terminal] improve docs a bit (#18296)
* Revert "add missing import to asynchttpserver's example" This reverts commit 7ef364a. * alternative to #18185 * add std/mutexes * cvlose #17696 * Revert "add std/mutexes" This reverts commit 69abc8b. * tiny * test * improve terminal docs * follow advice
1 parent 6030e13 commit e45b858

File tree

1 file changed

+45
-70
lines changed

1 file changed

+45
-70
lines changed

lib/pure/terminal.nim

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@
1616
## Similarly, if you hide the cursor, make sure to unhide it with
1717
## `showCursor` before quitting.
1818

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+
1947
import macros
2048
import strformat
2149
from strutils import toLowerAscii, `%`
@@ -418,6 +446,9 @@ else:
418446

419447
proc eraseLine*(f: File) =
420448
## Erases the entire current line.
449+
runnableExamples("-r:off"):
450+
write(stdout, "never mind")
451+
stdout.eraseLine() # nothing will be printed on the screen
421452
when defined(windows):
422453
let h = conHandle(f)
423454
var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
@@ -480,7 +511,7 @@ proc resetAttributes*(f: File) =
480511
gBG = 0
481512

482513
type
483-
Style* = enum ## different styles for text output
514+
Style* = enum ## Different styles for text output.
484515
styleBright = 1, ## bright text
485516
styleDim, ## dim text
486517
styleItalic, ## italic (or reverse on terminals not supporting)
@@ -534,7 +565,7 @@ proc writeStyled*(txt: string, style: set[Style] = {styleBright}) =
534565
stdout.write(ansiStyleCode(gBG))
535566

536567
type
537-
ForegroundColor* = enum ## terminal's foreground colors
568+
ForegroundColor* = enum ## Terminal's foreground colors.
538569
fgBlack = 30, ## black
539570
fgRed, ## red
540571
fgGreen, ## green
@@ -546,7 +577,7 @@ type
546577
fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
547578
fgDefault ## default terminal foreground color
548579

549-
BackgroundColor* = enum ## terminal's background colors
580+
BackgroundColor* = enum ## Terminal's background colors.
550581
bgBlack = 40, ## black
551582
bgRed, ## red
552583
bgGreen, ## green
@@ -701,14 +732,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
701732
## When some argument is `Style`, `set[Style]`, `ForegroundColor`,
702733
## `BackgroundColor` or `TerminalCmd` then it is not sent directly to
703734
## `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+
712739
var reset = false
713740
result = newNimNode(nnkStmtList)
714741

@@ -731,14 +758,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
731758

732759
template styledWriteLine*(f: File, args: varargs[untyped]) =
733760
## 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+
742765
styledWrite(f, args)
743766
write(f, "\n")
744767

@@ -747,7 +770,7 @@ template styledEcho*(args: varargs[untyped]) =
747770
stdout.styledWriteLine(args)
748771

749772
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.
751774
## The character is not printed to the terminal.
752775
when defined(windows):
753776
let fd = getStdHandle(STD_INPUT_HANDLE)
@@ -852,7 +875,7 @@ when defined(windows):
852875
import os
853876

854877
proc enableTrueColors*() =
855-
## Enable true color.
878+
## Enables true color.
856879
var term = getTerminal()
857880
when defined(windows):
858881
var
@@ -885,7 +908,7 @@ proc enableTrueColors*() =
885908
term.trueColorIsEnabled = term.trueColorIsSupported
886909

887910
proc disableTrueColors*() =
888-
## Disable true color.
911+
## Disables true color.
889912
var term = getTerminal()
890913
when defined(windows):
891914
if term.trueColorIsSupported:
@@ -902,51 +925,3 @@ proc newTerminal(): owned(PTerminal) =
902925
new result
903926
when defined(windows):
904927
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

Comments
 (0)