UNPKG

@artiq/stylize

Version:

Lightweight terminal text stylizer with ANSI, RGB, HEX colors, multiple styling modes, and chainable proxy API.

202 lines (142 loc) โ€ข 4.78 kB
![npm](https://img.shields.io/npm/v/@artiq/stylize) ![License](https://img.shields.io/npm/l/@artiq/stylize) ![ESM](https://img.shields.io/badge/javascript-ESM-orange) ![Node](https://img.shields.io/badge/node-%3E=22.0.0-green) # @artiq/stylize A minimalist ANSI styling tool for text formatting โ€” made for simplicity, flexibility, and extensibility. --- ## โœจ Key Features - ๐ŸŽจ **Rich Styling Support** โ€“ ANSI modifiers, foreground/background colors, 8-bit RGB, HEX - ๐Ÿ”— **Chainable Proxy API** โ€“ Use `.bold.red.bgBlue(...)` with full IntelliSense - โš™๏ธ **Flexible Application** โ€“ Apply styles per character, word, line, or sentence - ๐Ÿงช **Runtime Safe** โ€“ Auto-validates styles and mode usage --- ## ๐Ÿ“ฆ Installation ```bash npm install @artiq/stylize ``` > This package is **ESM only**, make sure you have `"type": "module"` in your `package.json`. --- ## ๐ŸŽจ Supported Styles ### Modifiers ``` bold, dim, italic, underline, inverse, hidden, strikethrough ``` ### Foreground Colors ``` black, red, green, yellow, blue, magenta, cyan, white, gray, brightRed, brightGreen, brightYellow, brightBlue, brightMagenta, brightCyan, brightWhite ``` ### Background Colors Prefix with `bg` + capitalized color, e.g.: ``` bgRed, bgBlue, bgGreen, bgBrightCyan, bgWhite, etc. ``` > Each word will receive a random style from the `styleSets`. --- ## ๐Ÿ“˜ API Reference ### `stylize(text, styleSets, options?)` | Param | Type | Description | |-------------|--------------|-----------------------------------------------------| | `text` | `string` | The raw text to stylize | | `styleSets` | `string[][]` | Array of style groups | | `options` | `object` | Optional: `style` (mode), `randomize` (boolean) | --- ### `resolveAnsiStyles(styles: string[])` Resolve an array of styles to a single ANSI escape sequence. --- ## ๐Ÿš€ Getting Started ### Import package ```js import stylize from '@artiq/stylize'; ``` --- ### Basic Style ```js console.log(stylize( 'HELLO ARTIQ', [['bold', 'red']], { style: 'word' } )); console.log(stylize('HELLO STYLIZE', [['italic', 'green']])); ``` **Result:** ![Basic Style 1](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/basic-style1.png) ![Basic Style 2](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/basic-style2.png) --- ### Using Multiple Styles (`styleSets`) ```js const styleSets = [ ['green', 'bold'], ['blue', 'italic'], ['yellow', 'dim'], ['magenta'], ['cyan', 'underline'] ]; console.log(stylize( 'Stylize is fun and colorful!', styleSets, { style: 'word' } )); ``` **Result:** ![Multiple Stylize](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/multiple-styles.png) --- ### Available Modes | Mode | Description | |------------|---------------------------| | `char` | Apply style per character | | `word` | Apply style per word | | `line` | Apply style per line | | `sentence` | Apply style per sentence | --- ### ๐Ÿงฉ Proxy API (Chaining Style) ```js console.log( stylize.yellow.bold.underline('HELLO ARTIQ STYLIZE') ); ``` **Result:** ![Chaining Style](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/chained-style.png) --- ### Custom RGB & HEX ```js const stylesRGB = [ ['bold', 'rgb(255,0,0)'], ['underline', 'rgb(0,255,0)'], ['italic', 'rgb(0,0,255)'], ['bgRgb(255,255,0)', 'black'] ]; console.log( stylize('Hello colorful style rgb', stylesRGB, { style: 'word', randomize: false }) ); const stylesHEX = [ ['#00FFFF', 'underline'], ['#ff0000', 'strikethrough'], ['bg#00FF00'], ['bg#333333'], ['dim'] ]; console.log(stylize('Hello colorful style hex', stylesHEX, { style: 'word' })); ``` **Result:** ![RGB](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/rgb.png) ![HEX](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/hex.png) --- ### ๐Ÿ”€ Advanced: Random & Gradient Application #### Randomize Style per Segment ```js console.log(stylize( 'Random style fun', [['red'], ['blue', 'italic'], ['green', 'underline']], { style: 'word', randomize: true } )); ``` **Result:** ![Randomize Style](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/doc/random-style.png) --- ## ๐Ÿ‘ค Author **[Dimas Fajar](https://github.com/fajardison)** --- ## โš–๏ธ License This project is licensed under the **MIT License** โ€“ see the [LICENSE](https://raw.githubusercontent.com/fajardison/artiq-stylize/refs/heads/main/LICENSE) file for details.