0.1.3 • Published 6 months ago
@art-suite/art-core-terminal-colors v0.1.3
@art-suite/art-core-terminal-colors
A lightweight and efficient library for adding colors and styles to your terminal/console output using ANSI escape codes. Provides a simple API for common text styling needs.
Features
- Apply various text styles: bold, dim, italic, underline, inverse, hidden, strikethrough.
- Set foreground text colors: black, red, green, yellow, blue, magenta, cyan, white, grey, and their bright counterparts.
- Set background text colors: bgBlack, bgRed, ..., bgWhiteBright.
- Utilities for working with ANSI codes:
ansiRegexto match codes,stripAnsito remove them, andansiSafeStringLengthto get visual length. - Chainable API (typically, color functions return the modified string, often allowing chaining).
- TypeScript definitions for complete type safety.
Installation
npm install @art-suite/art-core-terminal-colorsAPI Overview
All styling functions take a string as input and return a new string with the appropriate ANSI escape codes applied.
ANSI Utilities
ansiRegex: RegExp: A regular expression to match ANSI escape codes.stripAnsi(str: string): string: Removes all ANSI escape codes from a string.ansiSafeStringLength(str: string): number: Calculates the visual length of a string, ignoring ANSI escape codes.
Style Modifiers
reset(str: string): string: Resets all current styling.bold(str: string): string: Makes text bold.dim(str: string): string: Makes text dim (decreased intensity).italic(str: string): string: Makes text italic.underline(str: string): string: Underlines text.inverse(str: string): string: Inverts foreground and background colors.hidden(str: string): string: Makes text hidden (invisible).strikethrough(str: string): string: Applies strikethrough to text.
Foreground Colors
black(str: string): stringred(str: string): stringgreen(str: string): stringyellow(str: string): stringblue(str: string): stringmagenta(str: string): stringcyan(str: string): stringwhite(str: string): stringgrey(str: string): string(orgray)
Bright Foreground Colors
redBright(str: string): stringgreenBright(str: string): stringyellowBright(str: string): stringblueBright(str: string): stringmagentaBright(str: string): stringcyanBright(str: string): stringwhiteBright(str: string): string
Background Colors
bgBlack(str: string): stringbgRed(str: string): stringbgGreen(str: string): stringbgYellow(str: string): stringbgBlue(str: string): stringbgMagenta(str: string): stringbgCyan(str: string): stringbgWhite(str: string): stringbgGrey(str: string): string(orbgGray)
Bright Background Colors
bgRedBright(str: string): stringbgGreenBright(str: string): stringbgYellowBright(str: string): stringbgBlueBright(str: string): stringbgMagentaBright(str: string): stringbgCyanBright(str: string): stringbgWhiteBright(str: string): string
Examples
import {
red,
bold,
bgYellow,
italic,
stripAnsi,
ansiSafeStringLength,
} from "@art-suite/art-core-terminal-colors";
const greeting = "Hello, World!";
// Simple coloring
console.log(red(greeting));
// Chained styling (assuming functions can be chained or nested)
// Example: bold(red(greeting)) or specific chaining API if provided
console.log(bold(red("This is bold red text!")));
console.log(italic(bgYellow(blue("Blue text on yellow, italicized."))));
// ANSI Utilities
const coloredText = red(bold("Important Message"));
console.log(coloredText); // Shows colored text
console.log(stripAnsi(coloredText)); // "Important Message"
console.log(ansiSafeStringLength(coloredText)); // 17 (length of "Important Message")
console.log(coloredText.length); // Actual string length including ANSI codes (much longer)License
MIT