The input string to be formatted with style codes
The input string wrapped with CSI style escape sequences and appropriate reset codes
Basic Style Functions
import { bold, italic, underline, dim } from "@nevware21/chromacon";
// These are all CsiStyle functions under the hood
const importantText: string = bold("CRITICAL ERROR");
const emphasizedText: string = italic("Note: This is important");
const linkedText: string = underline("https://example.com");
const mutedText: string = dim("(optional parameter)");
console.log(importantText); // "\x1b[1mCRITICAL ERROR\x1b[22m"
console.log(emphasizedText); // "\x1b[3mNote: This is important\x1b[23m"
console.log(linkedText); // "\x1b[4mhttps://example.com\x1b[24m"
console.log(mutedText); // "\x1b[2m(optional parameter)\x1b[22m"
Function Composition and Layering
import { bold, italic, underline, strikethrough } from "@nevware21/chromacon";
// CsiStyle functions can be composed for rich formatting
const formatTitle = (text: string) => bold(underline(text));
const formatDeprecated = (text: string) => strikethrough(italic(text));
const formatEmphasis = (text: string) => bold(italic(text));
console.log(formatTitle("Chapter 1: Introduction"));
// Output: "\x1b[1m\x1b[4mChapter 1: Introduction\x1b[24m\x1b[22m"
console.log(formatDeprecated("old_function()"));
// Output: "\x1b[9m\x1b[3mold_function()\x1b[23m\x1b[29m"
console.log(formatEmphasis("VERY IMPORTANT"));
// Output: "\x1b[1m\x1b[3mVERY IMPORTANT\x1b[23m\x1b[22m"
Custom Style Function Implementation
// Hypothetical custom implementation following CsiStyle interface
const customBold: CsiStyle = (value: string): string => {
const startCode = '\x1b[1m'; // Bold on
const resetCode = '\x1b[22m'; // Normal intensity
return startCode + value + resetCode;
};
const customItalic: CsiStyle = (value: string): string => {
const startCode = '\x1b[3m'; // Italic on
const resetCode = '\x1b[23m'; // Not italic
return startCode + value + resetCode;
};
console.log(customBold("Custom bold text"));
// Output: "\x1b[1mCustom bold text\x1b[22m"
Style Maps for Semantic Formatting
import { bold, italic, underline, dim, strikethrough } from "@nevware21/chromacon";
const textStyles: Record<string, CsiStyle> = {
heading: bold,
emphasis: italic,
link: underline,
muted: dim,
deprecated: strikethrough
};
function formatSemantic(style: keyof typeof textStyles, text: string): string {
const styleFn = textStyles[style];
return styleFn(text);
}
console.log(formatSemantic('heading', 'Table of Contents'));
console.log(formatSemantic('emphasis', 'important note'));
console.log(formatSemantic('link', 'Click here'));
console.log(formatSemantic('deprecated', 'legacyMethod()'));
Pipeline Processing with Styles
import { bold, italic, underline } from "@nevware21/chromacon";
// Chain of style transformations
const styleProcessors: CsiStyle[] = [bold, italic, underline];
function applyStylePipeline(text: string, styles: CsiStyle[]): string {
return styles.reduce((styledText, styleFn) => styleFn(styledText), text);
}
const result = applyStylePipeline("Formatted Text", [bold, underline]);
console.log(result);
// Output: "\x1b[1m\x1b[4mFormatted Text\x1b[24m\x1b[22m"
Conditional Style Application
import { bold, dim, italic, isColorSupported } from "@nevware21/chromacon";
interface StyleConfig {
error: CsiStyle;
warning: CsiStyle;
info: CsiStyle;
}
function createStyleConfig(): StyleConfig {
if (!isColorSupported()) {
// Return identity functions when styles not supported
const noStyle: CsiStyle = (text: string) => text;
return { error: noStyle, warning: noStyle, info: noStyle };
}
return {
error: bold,
warning: italic,
info: dim
};
}
const styles = createStyleConfig();
console.log(styles.error("Error: File not found"));
console.log(styles.warning("Warning: Deprecated API"));
console.log(styles.info("Info: Operation completed"));
Documentation and Code Formatting
import { bold, italic, underline, dim } from "@nevware21/chromacon";
// Style functions for code documentation
const formatKeyword = bold; // Language keywords
const formatType = italic; // Type annotations
const formatFunction = underline; // Function names
const formatComment = dim; // Comments
function formatCodeExample(keyword: string, type: string, funcName: string, comment: string): string {
return `${formatKeyword(keyword)} ${formatType(type)} ${formatFunction(funcName)} ${formatComment(comment)}`;
}
console.log(formatCodeExample('const', 'string', 'getName()', '// Gets user name'));
// Output with appropriate styling for each element
A function type that applies CSI (Control Sequence Introducer) style codes to text strings.
CsiStylerepresents the core functional interface for text style formatting in terminal/console output. It takes a string as input and returns that string wrapped with appropriate CSI escape sequences for style formatting such as bold, italic, underline, and other text appearance modifications. This is the base type that ChromaStyle extends to provide additional string capabilities.Unlike CsiColor which handles color formatting,
CsiStylefocuses exclusively on text styling that affects appearance properties like weight, emphasis, decoration, and intensity.CSI style codes are part of the ANSI escape sequence standard and provide cross-platform text formatting capabilities in modern terminals and console applications.