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 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
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
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
TerminalCmd = enum resetStyle, ## reset attributes fgColor, ## set foreground's true color bgColor ## set background's true color
ansiResetCode = "\e[0m"
proc terminalWidthIoctl(handles: openArray[Handle]): int {...}{.raises: [], tags: [].}
proc terminalHeightIoctl(handles: openArray[Handle]): int {...}{.raises: [], tags: [].}
proc terminalWidth(): int {...}{.raises: [], tags: [].}
y < x
. proc terminalHeight(): int {...}{.raises: [], tags: [].}
y < x
. proc terminalSize(): tuple[w, h: int] {...}{.raises: [], tags: [].}
proc hideCursor(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc showCursor(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc setCursorPos(f: File; x, y: int) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc setCursorXPos(f: File; x: int) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc setCursorYPos(f: File; y: int) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc cursorUp(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc cursorDown(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc cursorForward(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc cursorBackward(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc eraseLine(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc eraseScreen(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
proc resetAttributes(f: File) {...}{.raises: [Exception], tags: [RootEffect].}
proc ansiStyleCode(style: int): string {...}{.raises: [], tags: [].}
&
. proc setStyle(f: File; style: set[Style]) {...}{.raises: [Exception], tags: [RootEffect].}
proc writeStyled(txt: string; style: set[Style] = {styleBright}) {...}{. raises: [Exception, IOError], tags: [RootEffect, WriteIOEffect].}
proc setForegroundColor(f: File; fg: ForegroundColor; bright = false) {...}{. raises: [Exception], tags: [RootEffect].}
proc setBackgroundColor(f: File; bg: BackgroundColor; bright = false) {...}{. raises: [Exception], tags: [RootEffect].}
proc ansiForegroundColorCode(fg: ForegroundColor; bright = false): string {...}{.raises: [], tags: [].}
proc ansiForegroundColorCode(color: Color): string {...}{.raises: [], tags: [].}
&
. proc ansiBackgroundColorCode(color: Color): string {...}{.raises: [], tags: [].}
&
. proc setForegroundColor(f: File; color: Color) {...}{.raises: [Exception, IOError], tags: [RootEffect, WriteIOEffect].}
proc setBackgroundColor(f: File; color: Color) {...}{.raises: [Exception, IOError], tags: [RootEffect, WriteIOEffect].}
proc isatty(f: File): bool {...}{.raises: [], tags: [].}
proc getch(): char {...}{.raises: [], tags: [].}
proc readPasswordFromStdin(prompt: string; password: var TaintedString): bool {...}{. tags: [ReadIOEffect, WriteIOEffect], raises: [IOError].}
nil
! Returns false
if the end of the file has been reached, true
otherwise. proc readPasswordFromStdin(prompt = "password: "): TaintedString {...}{.raises: [IOError], tags: [ReadIOEffect, WriteIOEffect].}
proc resetAttributes() {...}{.noconv, raises: [Exception], tags: [RootEffect].}
system.addQuitProc(resetAttributes)
. proc isTrueColorSupported(): bool {...}{.raises: [Exception], tags: [RootEffect].}
proc enableTrueColors() {...}{.raises: [Exception], tags: [RootEffect, ReadEnvEffect].}
proc disableTrueColors() {...}{.raises: [Exception], tags: [RootEffect, ReadEnvEffect].}
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:
stdout.styledWrite(fgRed, "red text ") stdout.styledWrite(fgGreen, "green text")
template ansiStyleCode(style: Style): string
template ansiStyleCode(style: static[Style]): string
template ansiForegroundColorCode(fg: static[ForegroundColor]; bright: static[bool] = false): string
template ansiForegroundColorCode(color: static[Color]): string
template ansiBackgroundColorCode(color: static[Color]): string
template styledWriteLine(f: File; args: varargs[untyped])
Calls styledWrite
and appends a newline at the end.
Example:
proc error(msg: string) = styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
template styledEcho(args: varargs[untyped])
styledWriteLine
. template hideCursor()
template showCursor()
template setCursorPos(x, y: int)
template setCursorXPos(x: int)
template setCursorYPos(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)
template setForegroundColor(color: Color)
template setBackgroundColor(color: Color)
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/terminal.html