This module contains a few procedures to control the terminal (also called console). On UNIX, the implementation simply uses ANSI escape sequences and does not depend on any other module, on Windows it uses the Windows API. Changing the style is permanent even after program termination! Use the code exitprocs.addExitProc(resetAttributes) to restore the defaults. Similarly, if you hide the cursor, make sure to unhide it with showCursor before quitting.
Progress bar
Basic progress bar example:
Example: cmd: -r:off
import std/terminal import std/[os, strutils] for i in 0..100: stdout.styledWriteLine(fgRed, "0% ", fgWhite, '#'.repeat i, if i > 50: fgGreen else: fgYellow, "\t", $i , "%") sleep 42 cursorUp 1 eraseLine() stdout.resetAttributes()
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.Example: cmd: -r:off
import std/terminal 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")
Types
BackgroundColor = enum bgBlack = 40, ## black bgRed, ## red bgGreen, ## green bgYellow, ## yellow bgBlue, ## blue bgMagenta, ## magenta bgCyan, ## cyan bgWhite, ## white bg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.) bgDefault ## default terminal background color
- Terminal's background colors. Source Edit
ForegroundColor = enum fgBlack = 30, ## black fgRed, ## red fgGreen, ## green fgYellow, ## yellow fgBlue, ## blue fgMagenta, ## magenta fgCyan, ## cyan fgWhite, ## white fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.) fgDefault ## default terminal foreground color
- Terminal's foreground colors. Source Edit
Style = enum styleBright = 1, ## bright text styleDim, ## dim text styleItalic, ## italic (or reverse on terminals not supporting) styleUnderscore, ## underscored text styleBlink, ## blinking/bold text styleBlinkRapid, ## rapid blinking/bold text (not widely supported) styleReverse, ## reverse styleHidden, ## hidden text styleStrikethrough ## strikethrough
- Different styles for text output. Source Edit
Procs
proc ansiForegroundColorCode(color: Color): string {....raises: [ValueError], tags: [], forbids: [].}
- Source Edit
proc ansiForegroundColorCode(fg: ForegroundColor; bright = false): string {. ...raises: [ValueError], tags: [], forbids: [].}
- Source Edit
proc cursorBackward(f: File; count = 1) {....raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
-
Moves the cursor backward by count columns.
Example: cmd: -r:off
stdout.cursorBackward(2) write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this
Source Edit proc cursorForward(f: File; count = 1) {....raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
-
Moves the cursor forward by count columns.
Example: cmd: -r:off
stdout.cursorForward(2) write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this
Source Edit proc setBackgroundColor(f: File; bg: BackgroundColor; bright = false) {. ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
- Sets the terminal's background color. Source Edit
proc setForegroundColor(f: File; color: Color) {....raises: [IOError, ValueError], tags: [RootEffect, WriteIOEffect], forbids: [].}
- Sets the terminal's foreground true color. Source Edit
proc setForegroundColor(f: File; fg: ForegroundColor; bright = false) {. ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
- Sets the terminal's foreground color. Source Edit
Macros
macro styledWrite(f: File; m: varargs[typed]): untyped
-
Similar to write, but treating terminal style arguments specially. 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: cmd: -r:off
stdout.styledWrite(fgRed, "red text ") stdout.styledWrite(fgGreen, "green text")
Source Edit
Templates
template setBackgroundColor(bg: BackgroundColor; bright = false)
- Source Edit
template setForegroundColor(fg: ForegroundColor; bright = false)
- Source Edit