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)andbackground(s, n)use the 256-colour palette;rgbandbgRgbuse true colour. - Styles:
bold,dim,italic,underline,inverse,strike, or any SGR code withstyle(s, "code"). - Screen:
clear(),clearLine(),moveTo(row, col),hideCursor(),showCursor(), andwidth()/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—swrapped in the ANSI escapecode(e.g. "1;31"), reset after it.static fn bold(s: string) -> string—sin bold.static fn dim(s: string) -> string—sdimmed.static fn italic(s: string) -> string—sin italics.static fn underline(s: string) -> string—sunderlined.static fn red(s: string) -> string—sin red.static fn green(s: string) -> string—sin green.static fn yellow(s: string) -> string—sin yellow.static fn blue(s: string) -> string—sin blue.static fn magenta(s: string) -> string—sin magenta.static fn cyan(s: string) -> string—sin cyan.static fn gray(s: string) -> string—sin gray.static fn white(s: string) -> string—sin white.static fn inverse(s: string) -> string—swith foreground and background swapped.static fn strike(s: string) -> string—sstruck 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—son 256-colour backgroundn(0-255).static fn rgb(s: string, r: int, g: int, b: int) -> string—sin a 24-bit colour.static fn bgRgb(s: string, r: int, g: int, b: int) -> string—son 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), afterprompt.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").