VOOZH about

URL: https://deepwiki.com/hypervel/prompts/9.2-ansi-colors-and-styling

⇱ ANSI Colors and Styling | hypervel/prompts | DeepWiki


Loading...
Last indexed: 7 February 2026 (2e2181)
Menu

ANSI Colors and Styling

Purpose and Scope

This document describes the Colors trait, which provides methods for applying ANSI escape sequences to terminal text. The trait enables text styling (bold, dim, italic, underline, etc.) and coloring (foreground and background colors) for CLI output. For information about the rendering pipeline that uses these colors, see The Default Renderer. For terminal output using HTML-like syntax instead of ANSI codes directly, see Termwind Integration.

Sources: src/Concerns/Colors.php1-209

Overview

The Colors trait is located at src/Concerns/Colors.php and provides a collection of methods that wrap text with ANSI escape sequences. Each method takes a string as input and returns the same string wrapped with the appropriate escape codes for terminal styling.

The trait follows a consistent pattern: each method applies a specific ANSI code at the start of the text and resets that specific attribute at the end, ensuring styles don't bleed into subsequent output.


Sources: src/Concerns/Colors.php1-209 src/Themes/Default/Renderer.php14

ANSI Escape Sequence Structure

Each method in the Colors trait follows a specific pattern of ANSI escape sequences. The general structure is:

\e[<code>m<text>\e[<reset_code>m

Where:

  • \e[ is the ANSI escape sequence introducer
  • <code>m is the SGR (Select Graphic Rendition) parameter
  • <text> is the text to be styled
  • <reset_code>m is the specific reset code for that attribute

This ensures that each style is properly scoped and doesn't affect subsequent text.


Sources: src/Concerns/Colors.php12-207

Text Style Methods

The trait provides methods for common text styling effects. Each method applies a specific visual style to the text.

Style Methods Reference

MethodANSI CodesEffectFile Reference
reset()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/0m...\\e[0mReset all styles and colors[src/Concerns/Colors.php#L12-L15" min=12 max=15 file-path="0m...\e[0m`
bold()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/1m...\\e[22mBold/bright text[src/Concerns/Colors.php#L20-L23" min=20 max=23 file-path="1m...\e[22m`
dim()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/2m...\\e[22mDimmed/faint text[src/Concerns/Colors.php#L28-L31" min=28 max=31 file-path="2m...\e[22m`
italic()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/3m...\\e[23mItalic text[src/Concerns/Colors.php#L36-L39" min=36 max=39 file-path="3m...\e[23m`
underline()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/4m...\\e[24mUnderlined text[src/Concerns/Colors.php#L44-L47" min=44 max=47 file-path="4m...\e[24m`
inverse()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/7m...\\e[27mInverted foreground/background[src/Concerns/Colors.php#L52-L55" min=52 max=55 file-path="7m...\e[27m`
hidden()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/8m...\\e[28mHidden/invisible text[src/Concerns/Colors.php#L60-L63" min=60 max=63 file-path="8m...\e[28m`
strikethrough()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/9m...\\e[29mStrikethrough text[src/Concerns/Colors.php#L68-L71" min=68 max=71 file-path="9m...\e[29m`

Sources: src/Concerns/Colors.php12-71

Foreground Color Methods

The trait provides eight standard foreground colors plus an additional gray color. Each color method wraps text with the appropriate ANSI color code.

Foreground Color Reference

MethodANSI CodesColorFile Reference
black()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/30m...\\e[39mBlack text[src/Concerns/Colors.php#L76-L79" min=76 max=79 file-path="30m...\e[39m`
red()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/31m...\\e[39mRed text[src/Concerns/Colors.php#L84-L87" min=84 max=87 file-path="31m...\e[39m`
green()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/32m...\\e[39mGreen text[src/Concerns/Colors.php#L92-L95" min=92 max=95 file-path="32m...\e[39m`
yellow()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/33m...\\e[39mYellow text[src/Concerns/Colors.php#L100-L103" min=100 max=103 file-path="33m...\e[39m`
blue()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/34m...\\e[39mBlue text[src/Concerns/Colors.php#L108-L111" min=108 max=111 file-path="34m...\e[39m`
magenta()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/35m...\\e[39mMagenta text[src/Concerns/Colors.php#L116-L119" min=116 max=119 file-path="35m...\e[39m`
cyan()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/36m...\\e[39mCyan text[src/Concerns/Colors.php#L124-L127" min=124 max=127 file-path="36m...\e[39m`
white()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/37m...\\e[39mWhite text[src/Concerns/Colors.php#L132-L135" min=132 max=135 file-path="37m...\e[39m`
gray()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/90m...\\e[39mGray text (bright black)[src/Concerns/Colors.php#L204-L207" min=204 max=207 file-path="90m...\e[39m`

The reset code \e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/39m returns the foreground color to the terminal's default.\n\nSources#LNaN-LNaN" NaN file-path="39m` returns the foreground color to the terminal's default.\n\nSources">Hii src/Concerns/Colors.php204-207

Background Color Methods

The trait provides eight background color methods, each prefixed with bg to distinguish them from foreground colors.

Background Color Reference

MethodANSI CodesEffectFile Reference
bgBlack()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/40m...\\e[49mBlack background[src/Concerns/Colors.php#L140-L143" min=140 max=143 file-path="40m...\e[49m`
bgRed()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/41m...\\e[49mRed background[src/Concerns/Colors.php#L148-L151" min=148 max=151 file-path="41m...\e[49m`
bgGreen()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/42m...\\e[49mGreen background[src/Concerns/Colors.php#L156-L159" min=156 max=159 file-path="42m...\e[49m`
bgYellow()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/43m...\\e[49mYellow background[src/Concerns/Colors.php#L164-L167" min=164 max=167 file-path="43m...\e[49m`
bgBlue()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/44m...\\e[49mBlue background[src/Concerns/Colors.php#L172-L175" min=172 max=175 file-path="44m...\e[49m`
bgMagenta()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/45m...\\e[49mMagenta background[src/Concerns/Colors.php#L180-L183" min=180 max=183 file-path="45m...\e[49m`
bgCyan()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/46m...\\e[49mCyan background[src/Concerns/Colors.php#L188-L191" min=188 max=191 file-path="46m...\e[49m`
bgWhite()\e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/47m...\\e[49mWhite background[src/Concerns/Colors.php#L196-L199" min=196 max=199 file-path="47m...\e[49m`

The reset code \e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/49m returns the background color to the terminal's default.\n\nSources#LNaN-LNaN" NaN file-path="49m` returns the background color to the terminal's default.\n\nSources">Hii

Usage in Renderer Classes

The Colors trait is used by the Renderer abstract class, which is the base class for all theme renderers. This makes color and styling methods available to all renderer implementations.


Sources: src/Themes/Default/Renderer.php7-8 src/Themes/Default/Renderer.php14 src/Themes/Default/Renderer.php52-77

Renderer Implementation Examples

The Renderer class demonstrates practical usage of the Colors trait:

Warning Messages src/Themes/Default/Renderer.php52-55


Error Messages src/Themes/Default/Renderer.php60-63


Hint Messages src/Themes/Default/Renderer.php68-77


Sources: src/Themes/Default/Renderer.php52-77

Method Composition and Nesting

The Colors trait methods can be composed together to apply multiple styles simultaneously. Since each method wraps its input with ANSI codes, they can be nested:



Sources: src/Concerns/Colors.php1-209

ANSI Code Reset Behavior

Each method in the Colors trait uses specific reset codes rather than a global reset. This allows for fine-grained control and proper nesting of styles:

Style CategorySet Code RangeReset CodePurpose
Intensity1-222Reset bold/dim
Italic323Reset italic
Underline424Reset underline
Inverse727Reset inverse
Hidden828Reset hidden
Strikethrough929Reset strikethrough
Foreground30-37, 9039Reset to default foreground
Background40-4749Reset to default background
Global00Reset all attributes

Sources: src/Concerns/Colors.php12-207

Integration with Other Traits

The Colors trait is typically used alongside other traits in the renderer system:


The hint() method in the Renderer class demonstrates this combination, where text is first truncated using the Truncation trait, then colored using the Colors trait src/Themes/Default/Renderer.php68-77

Sources: src/Themes/Default/Renderer.php7-8 src/Themes/Default/Renderer.php14-15 src/Themes/Default/Renderer.php68-77

Complete Method Signatures

All methods in the Colors trait follow the same signature pattern:


This consistent interface makes the trait methods easy to use and compose. Every method:

  • Accepts a single string parameter
  • Returns a string with ANSI codes embedded
  • Is side-effect free (pure function)
  • Can be chained or nested with other methods

Sources: src/Concerns/Colors.php1-209