mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-14 15:23:27 +00:00
[std/terminal] improve docs a bit (#18296)
* Revert "add missing import to asynchttpserver's example" This reverts commit7ef364a402. * alternative to #18185 * add std/mutexes * cvlose #17696 * Revert "add std/mutexes" This reverts commit69abc8b649. * tiny * test * improve terminal docs * follow advice
This commit is contained in:
@@ -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, `%`
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
@@ -852,7 +875,7 @@ when defined(windows):
|
||||
import os
|
||||
|
||||
proc enableTrueColors*() =
|
||||
## Enable true color.
|
||||
## Enables true color.
|
||||
var term = getTerminal()
|
||||
when defined(windows):
|
||||
var
|
||||
@@ -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:
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user