Grav v1.14.0

Term

import "std/io" (included in import "std") · namespace std.io

Terminal styling with ANSI escape codes (colours, bold, underline), cursor control, and the terminal's size. Each style function wraps a string in the codes that switch the style on and back off, so styles can be nested and mixed with plain text.

grav
import "std"

fn main() {
    print(Term.bold("Build report"))
    print("${Term.green("ok")}   parser")
    print("${Term.red("FAIL")} codegen")
    print(Term.style("custom", "4;35"))
    print(Term.rgb("orange", 255, 165, 0))
}
  • Colours: red, green, yellow, blue, magenta, cyan, gray, white; color(s, n) and background(s, n) use the 256-colour palette; rgb and bgRgb use true colour.
  • Styles: bold, dim, italic, underline, inverse, strike, or any SGR code with style(s, "code").
  • Screen: clear(), clearLine(), moveTo(row, col), hideCursor(), showCursor(), and width() / height() of the terminal.

The codes are written whether or not the output is a terminal, so leave styling out when writing to files or pipes.

API

class Term

print(Term.green("ok") + " " + Term.bold("done")) Term.clear() Styles are plain escape codes; check OS.isTerminal() (lib/os.grav) before colouring output that may be piped to a file.

Static members

  • static fn style(s: string, code: string) -> string — s wrapped in the ANSI escape code (e.g. "1;31"), reset after it.
  • static fn bold(s: string) -> string — s in bold.
  • static fn dim(s: string) -> string — s dimmed.
  • static fn italic(s: string) -> string — s in italics.
  • static fn underline(s: string) -> string — s underlined.
  • static fn red(s: string) -> string — s in red.
  • static fn green(s: string) -> string — s in green.
  • static fn yellow(s: string) -> string — s in yellow.
  • static fn blue(s: string) -> string — s in blue.
  • static fn magenta(s: string) -> string — s in magenta.
  • static fn cyan(s: string) -> string — s in cyan.
  • static fn gray(s: string) -> string — s in gray.
  • static fn white(s: string) -> string — s in white.
  • static fn inverse(s: string) -> string — s with foreground and background swapped.
  • static fn strike(s: string) -> string — s struck through.
  • static fn color(s: string, n: int) -> string — 256-colour foreground/background (0-255) and 24-bit true colour.
  • static fn background(s: string, n: int) -> string — s on 256-colour background n (0-255).
  • static fn rgb(s: string, r: int, g: int, b: int) -> string — s in a 24-bit colour.
  • static fn bgRgb(s: string, r: int, g: int, b: int) -> string — s on a 24-bit background colour.
  • static fn width() -> int — Terminal size in columns/rows (80 x 24 when not attached to a terminal).
  • static fn height() -> int — The terminal's height in rows.
  • static fn clear() — Clears the screen and moves the cursor to the top left.
  • static fn clearLine() — Clears the current line.
  • static fn moveTo(row: int, col: int) — Moves the cursor to row, col (1-based).
  • static fn hideCursor() — Hides the cursor.
  • static fn showCursor() — Shows the cursor.
  • static fn readHidden(prompt: string = "") -> string — A line typed without showing it (a password), after prompt.
  • static fn isInteractive() -> bool — Whether standard input is a terminal (someone can answer a question).
  • static fn supportsColor() -> bool — Whether standard output is a terminal that takes colours (NO_COLOR unset, TERM not "dumb").

Edit this page on GitHub