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 system.addQuitProc(resetAttributes)
to restore the defaults. Similarly, if you hide the cursor, make sure to unhide it with showCursor
before quitting.
Style = enum styleBright = 1, ## bright text styleDim, ## dim text styleUnknown, ## unknown styleUnderscore = 4, ## underscored text styleBlink, ## blinking/bold text styleReverse = 7, ## unknown styleHidden ## hidden text
ForegroundColor = enum fgBlack = 30, ## black fgRed, ## red fgGreen, ## green fgYellow, ## yellow fgBlue, ## blue fgMagenta, ## magenta fgCyan, ## cyan fgWhite ## white
BackgroundColor = enum bgBlack = 40, ## black bgRed, ## red bgGreen, ## green bgYellow, ## yellow bgBlue, ## blue bgMagenta, ## magenta bgCyan, ## cyan bgWhite ## white
TerminalCmd = enum resetStyle ## reset attributes
proc terminalWidthIoctl(handles: openArray[Handle]): int {.raises: [], tags: [].}
proc terminalHeightIoctl(handles: openArray[Handle]): int {.raises: [], tags: [].}
proc terminalWidth(): int {.raises: [], tags: [].}
proc terminalHeight(): int {.raises: [], tags: [].}
proc terminalSize(): tuple[w, h: int] {.raises: [], tags: [].}
proc hideCursor(f: File) {.raises: [OSError], tags: [].}
proc showCursor(f: File) {.raises: [OSError], tags: [].}
proc setCursorPos(f: File; x, y: int) {.raises: [OSError], tags: [].}
proc setCursorXPos(f: File; x: int) {.raises: [OSError], tags: [].}
proc setCursorYPos(f: File; y: int) {.raises: [OSError], tags: [].}
proc cursorUp(f: File; count = 1) {.raises: [OSError], tags: [].}
proc cursorDown(f: File; count = 1) {.raises: [OSError], tags: [].}
proc cursorForward(f: File; count = 1) {.raises: [OSError], tags: [].}
proc cursorBackward(f: File; count = 1) {.raises: [OSError], tags: [].}
proc eraseLine(f: File) {.raises: [OSError], tags: [].}
proc eraseScreen(f: File) {.raises: [OSError], tags: [].}
proc resetAttributes(f: File) {.raises: [], tags: [].}
proc setStyle(f: File; style: set[Style]) {.raises: [], tags: [].}
proc writeStyled(txt: string; style: set[Style] = {styleBright}) {.raises: [IOError], tags: [WriteIOEffect].}
proc setForegroundColor(f: File; fg: ForegroundColor; bright = false) {.raises: [], tags: [].}
proc setBackgroundColor(f: File; bg: BackgroundColor; bright = false) {.raises: [], tags: [].}
proc isatty(f: File): bool {.raises: [], tags: [].}
proc getch(): char {.raises: [AssertionError], tags: [].}
proc resetAttributes() {.noconv, raises: [], tags: [].}
system.addQuitProc(resetAttributes)
. macro styledWriteLine(f: File; m: varargs[typed]): untyped
Similar to writeLine
, 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:
proc error(msg: string) = styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
macro styledEcho(args: varargs[untyped]): untyped
styledWriteLine
. template hideCursor()
template showCursor()
template setCursorPos(x, y: int)
template setCursorXPos(x: int)
template cursorUp(count = 1)
template cursorDown(count = 1)
template cursorForward(count = 1)
template cursorBackward(count = 1)
template eraseLine()
template eraseScreen()
template setStyle(style: set[Style])
template setForegroundColor(fg: ForegroundColor; bright = false)
template setBackgroundColor(bg: BackgroundColor; bright = false)
© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/terminal.html