Skip to content

Commit

Permalink
[std/terminal] improve docs a bit (#18296)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ringabout authored Jun 20, 2021
1 parent 6030e13 commit e45b858
Showing 1 changed file with 45 additions and 70 deletions.
115 changes: 45 additions & 70 deletions lib/pure/terminal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@
## Similarly, if you hide the cursor, make sure to unhide it with
## `showCursor` before quitting.

##[
## Playing with colorful and styled text
]##

## Procs like `styledWriteLine`, `styledEcho` etc. have a temporary effect on
## text parameters. Style parameters only affect the text parameter right after them.
## After being called, these procs will reset the default style of the terminal.
## While `setBackGroundColor`, `setForeGroundColor` etc. have a lasting
## influence on the terminal, you can use `resetAttributes` to
## reset the default style of the terminal.
runnableExamples("-r:off"):
stdout.styledWriteLine({styleBright, styleBlink, styleUnderscore}, "styled text ")
stdout.styledWriteLine(fgRed, "red text ")
stdout.styledWriteLine(fgWhite, bgRed, "white text in red background")
stdout.styledWriteLine(" ordinary text without style ")

stdout.setBackGroundColor(bgCyan, true)
stdout.setForeGroundColor(fgBlue)
stdout.write("blue text in cyan background")
stdout.resetAttributes()

# You can specify multiple text parameters. Style parameters
# only affect the text parameter right after them.
styledEcho styleBright, fgGreen, "[PASS]", resetStyle, fgGreen, " Yay!"

stdout.styledWriteLine(fgRed, "red text ", styleBright, "bold red", fgDefault, " bold text")


import macros
import strformat
from strutils import toLowerAscii, `%`
Expand Down Expand Up @@ -418,6 +446,9 @@ else:

proc eraseLine*(f: File) =
## Erases the entire current line.
runnableExamples("-r:off"):
write(stdout, "never mind")
stdout.eraseLine() # nothing will be printed on the screen
when defined(windows):
let h = conHandle(f)
var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
Expand Down Expand Up @@ -480,7 +511,7 @@ proc resetAttributes*(f: File) =
gBG = 0

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

type
ForegroundColor* = enum ## terminal's foreground colors
ForegroundColor* = enum ## Terminal's foreground colors.
fgBlack = 30, ## black
fgRed, ## red
fgGreen, ## green
Expand All @@ -546,7 +577,7 @@ type
fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
fgDefault ## default terminal foreground color

BackgroundColor* = enum ## terminal's background colors
BackgroundColor* = enum ## Terminal's background colors.
bgBlack = 40, ## black
bgRed, ## red
bgGreen, ## green
Expand Down Expand Up @@ -701,14 +732,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
## 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
##
## stdout.styledWrite(fgRed, "red text ")
## stdout.styledWrite(fgGreen, "green text")
##
runnableExamples("-r:off"):
stdout.styledWrite(fgRed, "red text ")
stdout.styledWrite(fgGreen, "green text")

var reset = false
result = newNimNode(nnkStmtList)

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

template styledWriteLine*(f: File, args: varargs[untyped]) =
## Calls `styledWrite` and appends a newline at the end.
##
## Example:
##
## .. code-block:: nim
##
## proc error(msg: string) =
## styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
##
runnableExamples:
proc error(msg: string) =
styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)

styledWrite(f, args)
write(f, "\n")

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

proc getch*(): char =
## Read a single character from the terminal, blocking until it is entered.
## Reads a single character from the terminal, blocking until it is entered.
## The character is not printed to the terminal.
when defined(windows):
let fd = getStdHandle(STD_INPUT_HANDLE)
Expand Down Expand Up @@ -852,7 +875,7 @@ when defined(windows):
import os

proc enableTrueColors*() =
## Enable true color.
## Enables true color.
var term = getTerminal()
when defined(windows):
var
Expand Down Expand Up @@ -885,7 +908,7 @@ proc enableTrueColors*() =
term.trueColorIsEnabled = term.trueColorIsSupported

proc disableTrueColors*() =
## Disable true color.
## Disables true color.
var term = getTerminal()
when defined(windows):
if term.trueColorIsSupported:
Expand All @@ -902,51 +925,3 @@ proc newTerminal(): owned(PTerminal) =
new result
when defined(windows):
initTerminal(result)

when not defined(testing) and isMainModule:
assert ansiStyleCode(styleBright) == "\e[1m"
assert ansiStyleCode(styleStrikethrough) == "\e[9m"
# exitprocs.addExitProc(resetAttributes)
write(stdout, "never mind")
stdout.eraseLine()
stdout.styledWriteLine({styleBright, styleBlink, styleUnderscore}, "styled text ")
stdout.styledWriteLine("italic text ", {styleItalic})
stdout.setBackGroundColor(bgCyan, true)
stdout.setForeGroundColor(fgBlue)
stdout.write("blue text in cyan background")
stdout.resetAttributes()
echo ""
stdout.writeLine("ordinary text")
echo "more ordinary text"
styledEcho styleBright, fgGreen, "[PASS]", resetStyle, fgGreen, " Yay!"
echo "ordinary text again"
styledEcho styleBright, fgRed, "[FAIL]", resetStyle, fgRed, " Nay :("
echo "ordinary text again"
setForeGroundColor(fgGreen)
echo "green text"
echo "more green text"
setForeGroundColor(fgBlue)
echo "blue text"
resetAttributes()
echo "ordinary text"

stdout.styledWriteLine(fgRed, "red text ")
stdout.styledWriteLine(fgWhite, bgRed, "white text in red background")
stdout.styledWriteLine(" ordinary text ")
stdout.styledWriteLine(fgGreen, "green text")

writeStyled("underscored text", {styleUnderscore})
stdout.styledWrite(fgRed, " red text ")
writeStyled("bright text ", {styleBright})
echo "ordinary text"

stdout.styledWrite(fgRed, "red text ")
stdout.styledWrite(fgWhite, bgRed, "white text in red background")
stdout.styledWrite(" ordinary text ")
stdout.styledWrite(fgGreen, "green text")
echo ""
echo "ordinary text"
stdout.styledWriteLine(fgRed, "red text ", styleBright, "bold red", fgDefault, " bold text")
stdout.styledWriteLine(bgYellow, "text in yellow bg", styleBright,
" bold text in yellow bg", bgDefault, " bold text")
echo "ordinary text"

0 comments on commit e45b858

Please sign in to comment.