Source Edit

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

  1. import std/terminal
  2. import std/[os, strutils]
  3. for i in 0..100:
  4. stdout.styledWriteLine(fgRed, "0% ", fgWhite, '#'.repeat i, if i > 50: fgGreen else: fgYellow, "\t", $i , "%")
  5. sleep 42
  6. cursorUp 1
  7. eraseLine()
  8. 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

  1. import std/terminal
  2. stdout.styledWriteLine({styleBright, styleBlink, styleUnderscore}, "styled text ")
  3. stdout.styledWriteLine(fgRed, "red text ")
  4. stdout.styledWriteLine(fgWhite, bgRed, "white text in red background")
  5. stdout.styledWriteLine(" ordinary text without style ")
  6. stdout.setBackGroundColor(bgCyan, true)
  7. stdout.setForeGroundColor(fgBlue)
  8. stdout.write("blue text in cyan background")
  9. stdout.resetAttributes()
  10. # You can specify multiple text parameters. Style parameters
  11. # only affect the text parameter right after them.
  12. styledEcho styleBright, fgGreen, "[PASS]", resetStyle, fgGreen, " Yay!"
  13. stdout.styledWriteLine(fgRed, "red text ", styleBright, "bold red", fgDefault, " bold text")

Imports

macros, strformat, strutils, colors, winlean, winlean, os, os

Types

  1. BackgroundColor = enum
  2. bgBlack = 40, ## black
  3. bgRed, ## red
  4. bgGreen, ## green
  5. bgYellow, ## yellow
  6. bgBlue, ## blue
  7. bgMagenta, ## magenta
  8. bgCyan, ## cyan
  9. bgWhite, ## white
  10. bg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
  11. bgDefault ## default terminal background color

Terminal’s background colors. Source Edit

  1. ForegroundColor = enum
  2. fgBlack = 30, ## black
  3. fgRed, ## red
  4. fgGreen, ## green
  5. fgYellow, ## yellow
  6. fgBlue, ## blue
  7. fgMagenta, ## magenta
  8. fgCyan, ## cyan
  9. fgWhite, ## white
  10. fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
  11. fgDefault ## default terminal foreground color

Terminal’s foreground colors. Source Edit

  1. Style = enum
  2. styleBright = 1, ## bright text
  3. styleDim, ## dim text
  4. styleItalic, ## italic (or reverse on terminals not supporting)
  5. styleUnderscore, ## underscored text
  6. styleBlink, ## blinking/bold text
  7. styleBlinkRapid, ## rapid blinking/bold text (not widely supported)
  8. styleReverse, ## reverse
  9. styleHidden, ## hidden text
  10. styleStrikethrough ## strikethrough

Different styles for text output. Source Edit

  1. TerminalCmd = enum
  2. resetStyle, ## reset attributes
  3. fgColor, ## set foreground's true color
  4. bgColor ## set background's true color

commands that can be expressed as arguments Source Edit

Consts

  1. ansiResetCode = "\e[0m"

Source Edit

Procs

  1. proc ansiBackgroundColorCode(color: Color): string {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc ansiForegroundColorCode(color: Color): string {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc ansiForegroundColorCode(fg: ForegroundColor; bright = false): string {.
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc ansiStyleCode(style: int): string {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc cursorBackward(f: File; count = 1) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Moves the cursor backward by count columns.

Example: cmd: -r:off

  1. stdout.cursorBackward(2)
  2. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this

Source Edit

  1. proc cursorDown(f: File; count = 1) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Moves the cursor down by count rows.

Example: cmd: -r:off

  1. stdout.cursorDown(2)
  2. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this

Source Edit

  1. proc cursorForward(f: File; count = 1) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Moves the cursor forward by count columns.

Example: cmd: -r:off

  1. stdout.cursorForward(2)
  2. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this

Source Edit

  1. proc cursorUp(f: File; count = 1) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Moves the cursor up by count rows.

Example: cmd: -r:off

  1. stdout.cursorUp(2)
  2. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this

Source Edit

  1. proc disableTrueColors() {....raises: [], tags: [RootEffect, ReadEnvEffect],
  2. forbids: [].}

Disables true color. Source Edit

  1. proc enableTrueColors() {....raises: [], tags: [RootEffect, ReadEnvEffect],
  2. forbids: [].}

Enables true color. Source Edit

  1. proc eraseLine(f: File) {....raises: [OSError], tags: [RootEffect], forbids: [].}

Erases the entire current line.

Example: cmd: -r:off

  1. write(stdout, "never mind")
  2. stdout.eraseLine() # nothing will be printed on the screen

Source Edit

  1. proc eraseScreen(f: File) {....raises: [OSError], tags: [RootEffect], forbids: [].}

Erases the screen with the background colour and moves the cursor to home. Source Edit

  1. proc getch(): char {....raises: [], tags: [], forbids: [].}

Reads a single character from the terminal, blocking until it is entered. The character is not printed to the terminal. Source Edit

  1. proc hideCursor(f: File) {....raises: [OSError], tags: [RootEffect], forbids: [].}

Hides the cursor. Source Edit

  1. proc isatty(f: File): bool {....raises: [], tags: [], forbids: [].}

Returns true if f is associated with a terminal device. Source Edit

  1. proc isTrueColorSupported(): bool {....raises: [], tags: [RootEffect], forbids: [].}

Returns true if a terminal supports true color. Source Edit

  1. proc readPasswordFromStdin(prompt = "password: "): string {....raises: [IOError],
  2. tags: [ReadIOEffect, WriteIOEffect], forbids: [].}

Reads a password from stdin without printing it. Source Edit

  1. proc readPasswordFromStdin(prompt: string; password: var string): bool {.
  2. ...tags: [ReadIOEffect, WriteIOEffect], raises: [IOError], forbids: [].}

Reads a password from stdin without printing it. password must not be nil! Returns false if the end of the file has been reached, true otherwise. Source Edit

  1. proc resetAttributes() {.noconv, ...raises: [], tags: [RootEffect], forbids: [].}

Resets all attributes on stdout. It is advisable to register this as a quit proc with exitprocs.addExitProc(resetAttributes). Source Edit

  1. proc resetAttributes(f: File) {....raises: [], tags: [RootEffect], forbids: [].}

Resets all attributes. Source Edit

  1. proc setBackgroundColor(f: File; bg: BackgroundColor; bright = false) {.
  2. ...raises: [], tags: [RootEffect], forbids: [].}

Sets the terminal’s background color. Source Edit

  1. proc setBackgroundColor(f: File; color: Color) {....raises: [IOError],
  2. tags: [RootEffect, WriteIOEffect], forbids: [].}

Sets the terminal’s background true color. Source Edit

  1. proc setCursorPos(f: File; x, y: int) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Sets the terminal’s cursor to the (x,y) position. (0,0) is the upper left of the screen. Source Edit

  1. proc setCursorXPos(f: File; x: int) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Sets the terminal’s cursor to the x position. The y position is not changed. Source Edit

  1. proc setCursorYPos(f: File; y: int) {....raises: [OSError], tags: [RootEffect],
  2. forbids: [].}

Sets the terminal’s cursor to the y position. The x position is not changed. .. warning:: This is not supported on UNIX! Source Edit

  1. proc setForegroundColor(f: File; color: Color) {....raises: [IOError],
  2. tags: [RootEffect, WriteIOEffect], forbids: [].}

Sets the terminal’s foreground true color. Source Edit

  1. proc setForegroundColor(f: File; fg: ForegroundColor; bright = false) {.
  2. ...raises: [], tags: [RootEffect], forbids: [].}

Sets the terminal’s foreground color. Source Edit

  1. proc setStyle(f: File; style: set[Style]) {....raises: [], tags: [RootEffect],
  2. forbids: [].}

Sets the terminal style. Source Edit

  1. proc showCursor(f: File) {....raises: [OSError], tags: [RootEffect], forbids: [].}

Shows the cursor. Source Edit

  1. proc terminalHeight(): int {....raises: [], tags: [], forbids: [].}

Returns the terminal height in rows. Source Edit

  1. proc terminalHeightIoctl(handles: openArray[Handle]): int {....raises: [],
  2. tags: [], forbids: [].}

Source Edit

  1. proc terminalSize(): tuple[w, h: int] {....raises: [], tags: [], forbids: [].}

Returns the terminal width and height as a tuple. Internally calls terminalWidth and terminalHeight, so the same assumptions apply. Source Edit

  1. proc terminalWidth(): int {....raises: [], tags: [], forbids: [].}

Returns the terminal width in columns. Source Edit

  1. proc terminalWidthIoctl(handles: openArray[Handle]): int {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc writeStyled(txt: string; style: set[Style] = {styleBright}) {.
  2. ...raises: [IOError], tags: [RootEffect, WriteIOEffect], forbids: [].}

Writes the text txt in a given style to stdout. Source Edit

Macros

  1. 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

  1. stdout.styledWrite(fgRed, "red text ")
  2. stdout.styledWrite(fgGreen, "green text")

Source Edit

Templates

  1. template ansiBackgroundColorCode(color: static[Color]): string

Source Edit

  1. template ansiForegroundColorCode(color: static[Color]): string

Source Edit

  1. template ansiForegroundColorCode(fg: static[ForegroundColor];
  2. bright: static[bool] = false): string

Source Edit

  1. template ansiStyleCode(style: static[Style]): string

Source Edit

  1. template ansiStyleCode(style: Style): string

Source Edit

  1. template cursorBackward(count = 1)

Source Edit

  1. template cursorDown(count = 1)

Source Edit

  1. template cursorForward(count = 1)

Source Edit

  1. template cursorUp(count = 1)

Source Edit

  1. template eraseLine()

Source Edit

  1. template eraseScreen()

Source Edit

  1. template hideCursor()

Source Edit

  1. template setBackgroundColor(bg: BackgroundColor; bright = false)

Source Edit

  1. template setBackgroundColor(color: Color)

Source Edit

  1. template setCursorPos(x, y: int)

Source Edit

  1. template setCursorXPos(x: int)

Source Edit

  1. template setCursorYPos(x: int)

Source Edit

  1. template setForegroundColor(color: Color)

Source Edit

  1. template setForegroundColor(fg: ForegroundColor; bright = false)

Source Edit

  1. template setStyle(style: set[Style])

Source Edit

  1. template showCursor()

Source Edit

  1. template styledEcho(args: varargs[untyped])

Echoes styles arguments to stdout using styledWriteLine. Source Edit

  1. template styledWriteLine(f: File; args: varargs[untyped])

Calls styledWrite and appends a newline at the end.

Example:

  1. proc error(msg: string) =
  2. styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)

Source Edit