Skip to content

Terminal Formatting

Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Terminal colors, styles, and geometry

std::terminal adapts formatting to the current output capabilities. color sets foreground color; bk_color sets the background. Both join their text arguments with spaces and return a String.

Colors accept hex Strings such as "#31d8e0", comma-separated RGB Strings such as "49,216,224", or [49, 216, 224]. True-color terminals receive RGB escapes; 256-color and basic-color terminals receive a nearest palette color. Monochrome output receives plain text.

import std::terminal::{ color, bold }
echo (color "#31d8e0" "harbor" "open")
echo (bold "boarding")
harbor open
boarding

The displayed output above is from redirected, non-TTY output. bold, dim, italic, underline, reverse, and strikethrough remain available on a monochrome TTY but produce plain text when stdout is not a TTY.

Inspect the output mode

$tty_color_mode has native type TtyColorMode, with variants TtyMonochrome, TtyBasic, Tty256, and TtyTrueColor. The interpreter initializes it for the current terminal. Prefer the formatting helpers to manually choosing escape sequences for every branch.

Geometry and cursor control

  • term_size returns [rows, columns] | ().
  • cursor_position returns [row, column] | ().
  • move_cursor row column writes a movement immediately.
  • clear returns the clear-screen/home sequence.

Cursor coordinates are one based, matching terminal escape conventions. Array positions in Shelly remain zero based. Unavailable queries return (); movement and clearing do nothing when output is not a TTY.

Terminal queries can require a foreground terminal. Check the optional result before destructuring it. Use the line-editing API when Reedline should own repainting and cursor positioning during interactive input.


Related: Prompts · Line editing · Environment

Clone this wiki locally