Modules@robinpath/color
color

@robinpath/color

0.1.1Node.jsPublic

Terminal ANSI color utilities: red, green, blue, bold, underline, RGB, and more.

Color

Terminal ANSI color utilities: red, green, blue, bold, underline, RGB, and more.

The color module provides ANSI escape code wrappers for terminal text styling. It supports foreground colors, background colors, text decorations (bold, dim, italic, underline, strikethrough), custom RGB colors, and stripping ANSI codes from text. Use it to add visual formatting to CLI output in RobinPath scripts.

Package: @robinpath/color | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the color module when you need to:

  • Highlight errors and warnings -- color error messages red, warnings yellow
  • Format CLI output -- add bold, underline, or italic styling to terminal text
  • Color-code log levels -- green for success, red for errors, gray for debug
  • Use custom colors -- apply any RGB color to text with color.rgb
  • Strip formatting -- remove ANSI codes from colored text for plain output

Quick Reference

FunctionDescriptionReturns
redWrap text in redstring
greenWrap text in greenstring
blueWrap text in bluestring
yellowWrap text in yellowstring
cyanWrap text in cyanstring
magentaWrap text in magentastring
whiteWrap text in whitestring
grayWrap text in graystring
boldWrap text in boldstring
dimWrap text in dimstring
italicWrap text in italicstring
underlineWrap text with underlinestring
strikethroughWrap text with strikethroughstring
bgRedWrap text with red backgroundstring
bgGreenWrap text with green backgroundstring
bgBlueWrap text with blue backgroundstring
stripStrip all ANSI escape codes from textstring
rgbWrap text with custom RGB foreground colorstring

Functions

red

Wrap text in red foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.red "error"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

green

Wrap text in green foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.green "success"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

blue

Wrap text in blue foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.blue "info"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

yellow

Wrap text in yellow foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.yellow "warning"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

cyan

Wrap text in cyan foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.cyan "info"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

magenta

Wrap text in magenta foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.magenta "special"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

white

Wrap text in white foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.white "text"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

gray

Wrap text in gray foreground color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.gray "muted"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

bold

Wrap text in bold style.

Module: color | Returns: string -- ANSI-styled string

set $msg as color.bold "important"
print $msg
ParameterTypeRequiredDescription
textstringYesText to style

dim

Wrap text in dim style.

Module: color | Returns: string -- ANSI-styled string

set $msg as color.dim "subtle"
print $msg
ParameterTypeRequiredDescription
textstringYesText to style

italic

Wrap text in italic style.

Module: color | Returns: string -- ANSI-styled string

set $msg as color.italic "emphasis"
print $msg
ParameterTypeRequiredDescription
textstringYesText to style

underline

Wrap text with underline decoration.

Module: color | Returns: string -- ANSI-styled string

set $msg as color.underline "link"
print $msg
ParameterTypeRequiredDescription
textstringYesText to style

strikethrough

Wrap text with strikethrough decoration.

Module: color | Returns: string -- ANSI-styled string

set $msg as color.strikethrough "deleted"
print $msg
ParameterTypeRequiredDescription
textstringYesText to style

bgRed

Wrap text with red background color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.bgRed "alert"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

bgGreen

Wrap text with green background color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.bgGreen "pass"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

bgBlue

Wrap text with blue background color.

Module: color | Returns: string -- ANSI-colored string

set $msg as color.bgBlue "info"
print $msg
ParameterTypeRequiredDescription
textstringYesText to colorize

strip

Strip all ANSI escape codes from text, returning plain unformatted text.

Module: color | Returns: string -- Plain text without ANSI codes

set $colored as color.red "error message"
set $plain as color.strip $colored
print $plain
ParameterTypeRequiredDescription
textstringYesText with ANSI codes to strip

rgb

Wrap text with a custom RGB foreground color (24-bit true color).

Module: color | Returns: string -- RGB-colored string

set $msg as color.rgb "custom colored text" 255 128 0
print $msg
ParameterTypeRequiredDescription
textstringYesText to color
rnumberYesRed component (0-255)
gnumberYesGreen component (0-255)
bnumberYesBlue component (0-255)

Error Handling

Color functions do not throw errors. If called with no arguments, they return an empty colored string. The strip function safely handles text with no ANSI codes.

set $result as color.red "safe to call"
print $result

Recipes

1. Color-coded log output

Print messages with different colors based on severity level.

@desc "Output result"
do
  print color.red "[ERROR] Database connection failed"
  print color.yellow "[WARN] Cache miss for key: user_123"
  print color.green "[OK] Server started on port 3000"
  print color.gray "[DEBUG] Request payload: {id: 1}"
enddo

2. Styled headers and sections

Use bold and underline for section headers in CLI output.

@desc "Output result"
do
  print color.bold color.underline "=== Build Report ==="
  print color.green "Tests: 42 passed"
  print color.yellow "Warnings: 3"
  print color.red "Errors: 0"
  print color.dim "Generated at 2024-06-20 14:30"
enddo

3. Custom brand colors with RGB

Apply exact brand colors using RGB values.

@desc "Rgb"
do
  set $brand as color.rgb "Anthropic" 191 144 0
  set $accent as color.rgb "Claude" 100 180 255
  print $brand
  print $accent
enddo

4. Highlight errors in a list

Loop through items and color-code based on status.

@desc "Validate result"
do
  set $items as [{"name": "API", "status": "ok"}, {"name": "DB", "status": "error"}, {"name": "Cache", "status": "warning"}]
  each $item in $items
    if $item.status == "ok"
      print color.green $item.name + ": " + $item.status
    end
    if $item.status == "error"
      print color.red $item.name + ": " + $item.status
    end
    if $item.status == "warning"
      print color.yellow $item.name + ": " + $item.status
    end
  end
enddo

5. Background highlights for alerts

Use background colors for critical alerts.

print color.bgRed color.bold " CRITICAL: Service down "
print color.bgGreen " RESOLVED: Service restored "

6. Strip colors for file output

Remove ANSI codes before writing to a file or sending to an API.

@desc "Green and strip"
do
  set $colored as color.green "Success: 42 tests passed"
  print $colored
  set $plain as color.strip $colored
  # $plain is now "Success: 42 tests passed" without ANSI codes
enddo

Related Modules

  • log -- Structured logging that can use color for formatted output
  • string -- String manipulation utilities for text processing
  • debug -- Debug output utilities complemented by color formatting
  • template -- Template rendering that can include colored placeholders

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/color

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.0 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities