styled-string-builder
Version:
String Styler class based on a builder design pattern
87 lines (86 loc) • 4.08 kB
TypeScript
import { styles } from "./constants";
/**
* @description Applies a basic ANSI color code to text.
* @summary This function takes a string, an ANSI color code number, and an optional background flag.
* It returns the text wrapped in the appropriate ANSI escape codes for either foreground or background coloring.
* This function is used for basic 16-color ANSI formatting.
*
* @param {string} text - The text to be colored.
* @param {number} n - The ANSI color code number.
* @param {boolean} [bg=false] - If true, applies the color to the background instead of the foreground.
* @return {string} The text wrapped in ANSI color codes.
*
* @function colorizeANSI
* @memberOf module:@StyledString
*/
export declare function colorizeANSI(text: string, n: number, bg?: boolean): string;
/**
* @description Applies a 256-color ANSI code to text.
* @summary This function takes a string and a color number (0-255) and returns the text
* wrapped in ANSI escape codes for either foreground or background coloring.
*
* @param {string} text - The text to be colored.
* @param {number} n - The color number (0-255).
* @param {boolean} [bg=false] - If true, applies the color to the background instead of the foreground.
* @return {string} The text wrapped in ANSI color codes.
*
* @function colorize256
* @memberOf module:@StyledString
*/
export declare function colorize256(text: string, n: number, bg?: boolean): string;
/**
* @description Applies an RGB color ANSI code to text.
* @summary This function takes a string and RGB color values (0-255 for each component)
* and returns the text wrapped in ANSI escape codes for either foreground or background coloring.
*
* @param {string} text - The text to be colored.
* @param {number} r - The red component of the color (0-255).
* @param {number} g - The green component of the color (0-255).
* @param {number} b - The blue component of the color (0-255).
* @param {boolean} [bg=false] - If true, applies the color to the background instead of the foreground.
* @return {string} The text wrapped in ANSI color codes.
*
* @function colorizeRGB
* @memberOf module:StyledString
*/
export declare function colorizeRGB(text: string, r: number, g: number, b: number, bg?: boolean): string;
/**
* @description Applies an ANSI style code to text.
* @summary This function takes a string and a style code (either a number or a key from the styles object)
* and returns the text wrapped in the appropriate ANSI escape codes for that style.
*
* @param {string} text - The text to be styled.
* @param {number | string} n - The style code or style name.
* @return {string} The text wrapped in ANSI style codes.
*
* @function applyStyle
* @memberOf module:StyledString
*/
export declare function applyStyle(text: string, n: number | keyof typeof styles): string;
/**
* @description Removes all ANSI formatting codes from text.
* @summary This function takes a string that may contain ANSI escape codes for formatting
* and returns a new string with all such codes removed, leaving only the plain text content.
* It uses a regular expression to match and remove ANSI escape sequences.
*
* @param {string} text - The text potentially containing ANSI formatting codes.
* @return {string} The input text with all ANSI formatting codes removed.
*
* @function clear
* @memberOf module:StyledString
*/
export declare function clear(text: string): string;
/**
* @description Applies raw ANSI escape codes to text.
* @summary This function takes a string and a raw ANSI escape code, and returns the text
* wrapped in the provided raw ANSI code and the reset code. This allows for applying custom
* or complex ANSI formatting that may not be covered by other utility functions.
*
* @param {string} text - The text to be formatted.
* @param {string} raw - The raw ANSI escape code to be applied.
* @return {string} The text wrapped in the raw ANSI code and the reset code.
*
* @function raw
* @memberOf module:StyledString
*/
export declare function raw(text: string, raw: string): string;