{"id":13591635,"url":"https://github.com/xpl/ansicolor","last_synced_at":"2025-04-12T19:46:40.519Z","repository":{"id":49305597,"uuid":"65186387","full_name":"xpl/ansicolor","owner":"xpl","description":"A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.","archived":false,"fork":false,"pushed_at":"2024-03-03T23:55:48.000Z","size":939,"stargazers_count":121,"open_issues_count":5,"forks_count":16,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-03T22:11:09.552Z","etag":null,"topics":["ansi","ansi-colors","ansi-escape-codes","ansi-escape-sequences","chrome","chrome-devtools","console","console-log","cross-platform","javascipt","javascript","javascript-library","npm-package","parsing","terminal","tty"],"latest_commit_sha":null,"homepage":"http://npmjs.com/package/ansicolor","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xpl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-08T08:21:36.000Z","updated_at":"2025-02-03T04:58:35.000Z","dependencies_parsed_at":"2023-11-13T19:59:13.055Z","dependency_job_id":"2f97b6d3-32c3-423c-944d-e232b7176ebb","html_url":"https://github.com/xpl/ansicolor","commit_stats":{"total_commits":265,"total_committers":11,"mean_commits":24.09090909090909,"dds":"0.19622641509433958","last_synced_commit":"84a32ae92402350a73facaf037e58fc197063085"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fansicolor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fansicolor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fansicolor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fansicolor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xpl","download_url":"https://codeload.github.com/xpl/ansicolor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625497,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansi","ansi-colors","ansi-escape-codes","ansi-escape-sequences","chrome","chrome-devtools","console","console-log","cross-platform","javascipt","javascript","javascript-library","npm-package","parsing","terminal","tty"],"created_at":"2024-08-01T16:01:00.060Z","updated_at":"2025-04-12T19:46:40.498Z","avatar_url":"https://github.com/xpl.png","language":"JavaScript","readme":"# ansicolor\n\n[![npm](https://img.shields.io/npm/v/ansicolor.svg)](https://npmjs.com/package/ansicolor)\n\nA JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.\n\n```bash\nnpm install ansicolor\n```\n\n## What For\n\n- String coloring with ANSI escape codes\n- Solves the [style hierarchy problem](#why-another-one) (when other similar tools fail)\n- Parsing/removing ANSI style data from strings\n- Converting ANSI styles to CSS or a Chrome DevTools-compatible output\n- A middleware for your [platform-agnostic logging system](https://github.com/xpl/ololog)\n\n## Why Another One?\n\nOther tools lack consistency, failing to solve a simple hierarchy problem:\n\n```javascript\nrequire ('colors') // a popular color utility\n\nconsole.log (('foo'.cyan + 'bar').red)\n```\n\n![pic](http://cdn.jpg.wtf/futurico/85/9b/1470626860-859b24350e22df74fd7497e9dc0d8d42.png)\n\nWTF?! The `bar` word above should be rendered in red, but it's not! That sucks. It's because ANSI codes are linear, not hierarchical (as with XML/HTML). A special kind of magic is needed to make this work. **Ansicolor** does that magic for you:\n\n```javascript\nrequire ('ansicolor').nice // .nice for unsafe String extensions\n\nconsole.log (('foo'.cyan + 'bar').red)\n```\n\n![pic](http://cdn.jpg.wtf/futurico/3c/61/1470626989-3c61b64d0690b0b413be367841650426.png)\n\nNice!\n\n## Crash Course\n\nImporting (as methods):\n\n```javascript\nimport { green, inverse, bgLightCyan, underline, dim } from 'ansicolor'\n```\n```javascript\nconst { green, inverse, bgLightCyan, underline, dim } = require ('ansicolor')\n```\n\nUsage:\n\n```javascript\nconsole.log ('foo' + green (inverse (bgLightCyan ('bar')) + 'baz') + 'qux')\n```\n```javascript\nconsole.log (underline.bright.green ('foo' + dim.red.bgLightCyan ('bar'))) // method chaining\n```\n\nImporting (as object):\n\n```javascript\nimport { ansicolor, ParsedSpan } from 'ansicolor' // along with type definitions\n```\n```javascript\nimport ansicolor from 'ansicolor'\n```\n\n### Nice Mode (not recommended)\n\n```javascript\nconst ansi = require ('ansicolor').nice\n```\n\nThe `('ansicolor').nice` export defines styling APIs on the `String` prototype directly. It uses an ad-hoc DSL (sort of) for infix-style string coloring. The `nice` is convenient, but not safe, avoid using it in public modules, as it alters global objects, and that might cause potential hard-to-debug compatibility issues.\n\n```javascript\nconsole.log ('foo'.red.bright + 'bar'.bgYellow.underline.dim)\n```\n\n### Supported Styles\n\n```javascript\n'foreground colors'\n    .red.green.yellow.blue.magenta.cyan.white.darkGray.black\n```\n```javascript\n'light foreground colors'\n    .lightRed.lightGreen.lightYellow.lightBlue.lightMagenta.lightCyan.lightGray\n```\n```javascript\n'background colors'\n    .bgRed.bgGreen.bgYellow.bgBlue.bgMagenta.bgCyan.bgWhite.bgDarkGray.bgBlack\n```\n```javascript\n'light background colors'\n    .bgLightRed.bgLightGreen.bgLightYellow.bgLightBlue.bgLightMagenta.bgLightCyan.bgLightGray\n```\n```javascript\n'styles'\n    .bright.dim.italic.underline.inverse // your platform should support italic\n```\n\nYou also can obtain all those style names (for reflection purposes):\n\n```javascript\nconst { names } = require ('ansicolor')\n\nnames // ['red', 'green', ...\n```\n\n## Removing ANSI Styles From Strings\n\n```javascript\nconst { strip } = require ('ansicolor')\n\nstrip ('\\u001b[0m\\u001b[4m\\u001b[42m\\u001b[31mfoo\\u001b[39m\\u001b[49m\\u001b[24mfoo\\u001b[0m')) // 'foofoo'\n```\n\n## Checking If Strings Contain ANSI Codes\n\n```javascript\nconst { isEscaped, green } = require ('ansicolor')\n\nisEscaped ('text')         // false\nisEscaped (green ('text')) // true\n```\n\n\n## Converting to CSS/HTML\n\nInspection of ANSI styles in arbitrary strings is essential when implementing platform-agnostic logging — that piece of code is available under command line interface and in a browser as well. Here's an example of how you would parse a colored string into an array-like structure. That structure can be traversed later to build HTML/JSON/XML or any other markup/syntax.\n\n```javascript\nconst { parse } = require ('ansicolor')\n\nconst parsed = parse ('foo'.bgLightRed.bright.italic + 'bar'.red.dim)\n```\n\nThe `ansi.parse ()` method will return a pseudo-array of styled spans, you can iterate over it with a `for ... of` loop and convert it to an array with the *spread operator* (`...`). Also, there's the `.spans` property for obtaining the already-spread array directly:\n\n```javascript\nassert.deepEqual (parsed.spans /* or [...parsed] */,\n\n    [ { css: 'font-weight: bold;font-style: italic;background:rgba(255,51,0,1);',\n        italic: true,\n        bold: true,\n        color: { bright: true },\n        bgColor: { name: 'lightRed' },\n        text: 'foo' },\n\n      { css: 'color:rgba(204,0,0,0.5);',\n        color: { name: 'red', dim: true },\n        text: 'bar' } ])\n```\n\n### Custom Color Themes\n\nYou can change default RGB values (won't work in terminals, affects only the ANSI→CSS transformation part):\n\n```javascript\nconst ansi = require ('ansicolor')\n\nansi.rgb = {\n\n    black:        [0,     0,   0],    \n    darkGray:     [100, 100, 100],\n    lightGray:    [200, 200, 200],\n    white:        [255, 255, 255],\n\n    red:          [204,   0,   0],\n    lightRed:     [255,  51,   0],\n    \n    green:        [0,   204,   0],\n    lightGreen:   [51,  204,  51],\n    \n    yellow:       [204, 102,   0],\n    lightYellow:  [255, 153,  51],\n    \n    blue:         [0,     0, 255],\n    lightBlue:    [26,  140, 255],\n    \n    magenta:      [204,   0, 204],\n    lightMagenta: [255,   0, 255],\n    \n    cyan:         [0,   153, 255],\n    lightCyan:    [0,   204, 255],\n}\n```\n\n## Chrome DevTools Compatibility\n\nWeb browsers usually implement their own proprietary CSS-based color formats for `console.log` and most of them fail to display standard ANSI colors. _Ansicolor_ offers you a helper method to convert ANSI-styled strings to browser-compatible argument lists acceptable by Chrome's `console.log`:\n\n```javascript\nconst { bgGreen, red, parse } = require ('ansicolor')\n\nconst string = 'foo' + bgGreen (red.underline.bright.inverse ('bar') + 'baz')\nconst parsed = parse (string)\n\nconsole.log (...parsed.asChromeConsoleLogArguments) // prints with colors in Chrome!\n```\n\nHere's what the format looks like:\n\n```javascript\nparsed.asChromeConsoleLogArguments // [ \"%cfoo%cbar%cbaz\",\n                                   //   \"\",\n                                   //   \"font-weight: bold;text-decoration: underline;background:rgba(255,51,0,1);color:rgba(0,204,0,1);\",\n                                   //   \"background:rgba(0,204,0,1);\"\n                                   // ]\n```\n\nPlay with this feature online: [demo page](https://xpl.github.io/ololog/). Open the DevTools console and type expressions in the input box to see colored console output.\n\nHappy logging!\n\n## Projects That Use `ansicolor`\n\n- [**Ololog!**](https://github.com/xpl/ololog) — a better `console.log` for the log-driven debugging junkies\n- [**CCXT**](https://github.com/ccxt/ccxt) — a cryptocurrency trading API with 130+ exchanges\n- [**Grafana**](https://github.com/grafana/grafana) — beautiful monitoring \u0026 metric analytics \u0026 dashboards\n\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpl%2Fansicolor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxpl%2Fansicolor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpl%2Fansicolor/lists"}