terminal

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
different styles for text output   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
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
TerminalCmd = enum
  resetStyle,                 ## reset attributes
  fgColor,                    ## set foreground's true color
  bgColor                     ## set background's true color
commands that can be expressed as arguments   Source Edit

プロシージャ

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: [].}
Returns the terminal width and height as a tuple. Internally calls terminalWidth and terminalHeight, so the same assumptions apply.   ソース 編集
proc hideCursor(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Hides the cursor.   ソース 編集
proc showCursor(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Shows the cursor.   ソース 編集
proc setCursorPos(f: File; x, y: int) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Sets the terminal's cursor to the (x,y) position. (0,0) is the upper left of the screen.   ソース 編集
proc setCursorXPos(f: File; x: int) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Sets the terminal's cursor to the x position. The y position is not changed.   ソース 編集
proc setCursorYPos(f: File; y: int) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Sets the terminal's cursor to the y position. The x position is not changed. Warning: This is not supported on UNIX!  ソース 編集
proc cursorUp(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Moves the cursor up by count rows.   ソース 編集
proc cursorDown(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Moves the cursor down by count rows.   ソース 編集
proc cursorForward(f: File; count = 1) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Moves the cursor forward by count columns.   ソース 編集
proc cursorBackward(f: File; count = 1) {...}{.raises: [Exception, OSError],
                                    tags: [RootEffect].}
Moves the cursor backward by count columns.   ソース 編集
proc eraseLine(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Erases the entire current line.   ソース 編集
proc eraseScreen(f: File) {...}{.raises: [Exception, OSError], tags: [RootEffect].}
Erases the screen with the background colour and moves the cursor to home.   ソース 編集
proc resetAttributes(f: File) {...}{.raises: [Exception], tags: [RootEffect].}
Resets all attributes.   ソース 編集
proc ansiStyleCode(style: int): string {...}{.raises: [ValueError], tags: [].}
  ソース 編集
proc setStyle(f: File; style: set[Style]) {...}{.raises: [Exception], tags: [RootEffect].}
Sets the terminal style.   ソース 編集
proc writeStyled(txt: string; style: set[Style] = {styleBright}) {...}{.
    raises: [Exception, IOError], tags: [RootEffect, WriteIOEffect].}
Writes the text txt in a given style to stdout.   ソース 編集
proc setForegroundColor(f: File; fg: ForegroundColor; bright = false) {...}{.
    raises: [Exception], tags: [RootEffect].}
Sets the terminal's foreground color.   ソース 編集
proc setBackgroundColor(f: File; bg: BackgroundColor; bright = false) {...}{.
    raises: [Exception], tags: [RootEffect].}
Sets the terminal's background color.   ソース 編集
proc ansiForegroundColorCode(fg: ForegroundColor; bright = false): string {...}{.
    raises: [ValueError], tags: [].}
  ソース 編集
proc ansiForegroundColorCode(color: Color): string {...}{.raises: [ValueError], tags: [].}
  ソース 編集
proc ansiBackgroundColorCode(color: Color): string {...}{.raises: [ValueError], tags: [].}
  ソース 編集
proc setForegroundColor(f: File; color: Color) {...}{.
    raises: [Exception, IOError, ValueError], tags: [RootEffect, WriteIOEffect].}
Sets the terminal's foreground true color.   ソース 編集
proc setBackgroundColor(f: File; color: Color) {...}{.
    raises: [Exception, IOError, ValueError], tags: [RootEffect, WriteIOEffect].}
Sets the terminal's background true color.   ソース 編集
proc isatty(f: File): bool {...}{.raises: [], tags: [].}
Returns true if f is associated with a terminal device.   ソース 編集
proc getch(): char {...}{.raises: [], tags: [].}
Read a single character from the terminal, blocking until it is entered. The character is not printed to the terminal.   ソース 編集
proc readPasswordFromStdin(prompt: string; password: var TaintedString): bool {...}{.
    tags: [ReadIOEffect, WriteIOEffect], raises: [IOError].}
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
proc readPasswordFromStdin(prompt = "password: "): TaintedString {...}{.raises: [IOError],
    tags: [ReadIOEffect, WriteIOEffect].}
Reads a password from stdin without printing it.   ソース 編集
proc resetAttributes() {...}{.noconv, raises: [Exception], tags: [RootEffect].}
Resets all attributes on stdout. It is advisable to register this as a quit proc with system.addQuitProc(resetAttributes).   ソース 編集
proc isTrueColorSupported(): bool {...}{.raises: [Exception], tags: [RootEffect].}
Returns true if a terminal supports true color.   ソース 編集
proc enableTrueColors() {...}{.raises: [Exception], tags: [RootEffect, ReadEnvEffect].}
Enable true color.   ソース 編集
proc disableTrueColors() {...}{.raises: [Exception], tags: [RootEffect, ReadEnvEffect].}
Disable true color.   ソース 編集

マクロ

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.

用例:

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.

用例:

proc error(msg: string) =
  styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
  ソース 編集
template styledEcho(args: varargs[untyped])
Echoes styles arguments to stdout using 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)
  ソース 編集