[std/terminal] improve docs a bit (#18296)
* Revert "add missing import to asynchttpserver's example"
This reverts commit 7ef364a402.
* alternative to #18185
* add std/mutexes
* cvlose #17696
* Revert "add std/mutexes"
This reverts commit 69abc8b64954206da6ffe5fc40a1142b39777762.
* tiny
* test
* improve terminal docs
* follow advice
This commit is contained in:
parent
6030e139b5
commit
e45b858f04
1 changed files with 45 additions and 70 deletions
|
|
@ -16,6 +16,34 @@
|
||||||
## Similarly, if you hide the cursor, make sure to unhide it with
|
## Similarly, if you hide the cursor, make sure to unhide it with
|
||||||
## `showCursor` before quitting.
|
## `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 macros
|
||||||
import strformat
|
import strformat
|
||||||
from strutils import toLowerAscii, `%`
|
from strutils import toLowerAscii, `%`
|
||||||
|
|
@ -418,6 +446,9 @@ else:
|
||||||
|
|
||||||
proc eraseLine*(f: File) =
|
proc eraseLine*(f: File) =
|
||||||
## Erases the entire current line.
|
## Erases the entire current line.
|
||||||
|
runnableExamples("-r:off"):
|
||||||
|
write(stdout, "never mind")
|
||||||
|
stdout.eraseLine() # nothing will be printed on the screen
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
let h = conHandle(f)
|
let h = conHandle(f)
|
||||||
var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
|
var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
|
||||||
|
|
@ -480,7 +511,7 @@ proc resetAttributes*(f: File) =
|
||||||
gBG = 0
|
gBG = 0
|
||||||
|
|
||||||
type
|
type
|
||||||
Style* = enum ## different styles for text output
|
Style* = enum ## Different styles for text output.
|
||||||
styleBright = 1, ## bright text
|
styleBright = 1, ## bright text
|
||||||
styleDim, ## dim text
|
styleDim, ## dim text
|
||||||
styleItalic, ## italic (or reverse on terminals not supporting)
|
styleItalic, ## italic (or reverse on terminals not supporting)
|
||||||
|
|
@ -534,7 +565,7 @@ proc writeStyled*(txt: string, style: set[Style] = {styleBright}) =
|
||||||
stdout.write(ansiStyleCode(gBG))
|
stdout.write(ansiStyleCode(gBG))
|
||||||
|
|
||||||
type
|
type
|
||||||
ForegroundColor* = enum ## terminal's foreground colors
|
ForegroundColor* = enum ## Terminal's foreground colors.
|
||||||
fgBlack = 30, ## black
|
fgBlack = 30, ## black
|
||||||
fgRed, ## red
|
fgRed, ## red
|
||||||
fgGreen, ## green
|
fgGreen, ## green
|
||||||
|
|
@ -546,7 +577,7 @@ type
|
||||||
fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
|
fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
|
||||||
fgDefault ## default terminal foreground color
|
fgDefault ## default terminal foreground color
|
||||||
|
|
||||||
BackgroundColor* = enum ## terminal's background colors
|
BackgroundColor* = enum ## Terminal's background colors.
|
||||||
bgBlack = 40, ## black
|
bgBlack = 40, ## black
|
||||||
bgRed, ## red
|
bgRed, ## red
|
||||||
bgGreen, ## green
|
bgGreen, ## green
|
||||||
|
|
@ -701,14 +732,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
|
||||||
## When some argument is `Style`, `set[Style]`, `ForegroundColor`,
|
## When some argument is `Style`, `set[Style]`, `ForegroundColor`,
|
||||||
## `BackgroundColor` or `TerminalCmd` then it is not sent directly to
|
## `BackgroundColor` or `TerminalCmd` then it is not sent directly to
|
||||||
## `f`, but instead corresponding terminal style proc is called.
|
## `f`, but instead corresponding terminal style proc is called.
|
||||||
##
|
runnableExamples("-r:off"):
|
||||||
## Example:
|
stdout.styledWrite(fgRed, "red text ")
|
||||||
##
|
stdout.styledWrite(fgGreen, "green text")
|
||||||
## .. code-block:: nim
|
|
||||||
##
|
|
||||||
## stdout.styledWrite(fgRed, "red text ")
|
|
||||||
## stdout.styledWrite(fgGreen, "green text")
|
|
||||||
##
|
|
||||||
var reset = false
|
var reset = false
|
||||||
result = newNimNode(nnkStmtList)
|
result = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
|
|
@ -731,14 +758,10 @@ macro styledWrite*(f: File, m: varargs[typed]): untyped =
|
||||||
|
|
||||||
template styledWriteLine*(f: File, args: varargs[untyped]) =
|
template styledWriteLine*(f: File, args: varargs[untyped]) =
|
||||||
## Calls `styledWrite` and appends a newline at the end.
|
## Calls `styledWrite` and appends a newline at the end.
|
||||||
##
|
runnableExamples:
|
||||||
## Example:
|
proc error(msg: string) =
|
||||||
##
|
styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
|
||||||
## .. code-block:: nim
|
|
||||||
##
|
|
||||||
## proc error(msg: string) =
|
|
||||||
## styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
|
|
||||||
##
|
|
||||||
styledWrite(f, args)
|
styledWrite(f, args)
|
||||||
write(f, "\n")
|
write(f, "\n")
|
||||||
|
|
||||||
|
|
@ -747,7 +770,7 @@ template styledEcho*(args: varargs[untyped]) =
|
||||||
stdout.styledWriteLine(args)
|
stdout.styledWriteLine(args)
|
||||||
|
|
||||||
proc getch*(): char =
|
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.
|
## The character is not printed to the terminal.
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
let fd = getStdHandle(STD_INPUT_HANDLE)
|
let fd = getStdHandle(STD_INPUT_HANDLE)
|
||||||
|
|
@ -852,7 +875,7 @@ when defined(windows):
|
||||||
import os
|
import os
|
||||||
|
|
||||||
proc enableTrueColors*() =
|
proc enableTrueColors*() =
|
||||||
## Enable true color.
|
## Enables true color.
|
||||||
var term = getTerminal()
|
var term = getTerminal()
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
var
|
var
|
||||||
|
|
@ -885,7 +908,7 @@ proc enableTrueColors*() =
|
||||||
term.trueColorIsEnabled = term.trueColorIsSupported
|
term.trueColorIsEnabled = term.trueColorIsSupported
|
||||||
|
|
||||||
proc disableTrueColors*() =
|
proc disableTrueColors*() =
|
||||||
## Disable true color.
|
## Disables true color.
|
||||||
var term = getTerminal()
|
var term = getTerminal()
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
if term.trueColorIsSupported:
|
if term.trueColorIsSupported:
|
||||||
|
|
@ -902,51 +925,3 @@ proc newTerminal(): owned(PTerminal) =
|
||||||
new result
|
new result
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
initTerminal(result)
|
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"
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue