https://github.com/jchip/chalker
Set ansi colors in strings using <> markers and chalk
https://github.com/jchip/chalker
ansi chalk colors terminal xml
Last synced: about 1 month ago
JSON representation
Set ansi colors in strings using <> markers and chalk
- Host: GitHub
- URL: https://github.com/jchip/chalker
- Owner: jchip
- License: other
- Created: 2019-01-03T23:13:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-04-05T20:28:07.000Z (about 6 years ago)
- Last Synced: 2024-04-27T01:41:02.890Z (about 2 years ago)
- Topics: ansi, chalk, colors, terminal, xml
- Language: JavaScript
- Homepage:
- Size: 286 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
[![Dependency Status][daviddm-image]][daviddm-url] [![devDependency Status][daviddm-dev-image]][daviddm-dev-url]
# chalker
Set ansi colors in strings using `<>` markers and [chalk] or [ansi-colors].
# Usage
```js
const chalker = require("chalker");
console.log(chalker("Red on Green Text>"));
// with template string tagging
console.log(chalker`hello world`);
```
A typical use case is to easily manage colors for logs that could go to console or log servers.
```js
const msg = `some error occurred...`;
// log to console for visual with colors
if (!production) console.log(chalker(msg));
// log to log server with colors removed
logger.log(chalker.remove(msg));
```
# Install
```
npm i --save chalker chalk
```
or:
```
npm i --save chalker ansi-colors
```
`chalker` expects either [chalk] `>= 4` or [ansi-colors] `>= 4` as a peer dependency.
# Demo
![demo][demo]
# Marker Details
- Color markers has the `red text` format. You can use any valid methods [chalk] supports.
- For example, `blue bold text` will colorize `blue bold text` with `chalk.blue.bold`.
- Closing marker can be simply `>`
- The following HTML entities escapes are supported:
| Entity | Character | Entity | Character |
| -------- | --------- | -------- | ------------------ |
| `<` | < | `>` | > |
| `&` | & | ` ` | non-breaking space |
| `'` | ' | `©` | © |
| `"` | " | `®` | ® |
- HTML escape using code points also works:
- Hex - `hhhh;` where `hhhh` is the Hex code point.
- Decimal - `nnnn;` where `nnnn` is the Decimal code point.
- ie: `` makes 👩
#### Advanced Chalk Colors
Some advanced color markers use `chalker` compatibility wrappers when Chalk does not provide
the corresponding API directly.
[Chalk advanced colors] can be applied with:
| [chalk] API | chalker marker | [chalk] API | chalker marker |
| --------------- | --------------------------------------------- | ----------------- | ---------------------------------------------------- |
| `chalk.rgb` | `<(255, 10, 20)>`, `` | `chalk.bgRgb` | ``, `` |
| `chalk.hex` | `<#FF0000>`, `` | `chalk.bgHex` | ``, `` |
| `chalk.keyword` | ``, `<(orange)>`, `` | `chalk.bgKeyword` | ``, ``, `` |
| `chalk.hsl` | `` | `chalk.bgHsl` | `` |
| `chalk.hsv` | `` | `chalk.bgHsv` | `` |
| `chalk.hwb` | `` | `chalk.bgHwb` | `` |
##### More details
- a marker is tried with `chalk.keyword` if:
- it's not detected as hex value
- it doesn't contain params enclosed in `()`
- it's not found as a basic color that `chalk` supports
- for example, this is a chalk color keyword: ``
- If it's prefixed with `"bg-"` then it's tried using `chalk.bgKeyword`
- ie: ``
- All markers can be comined with `.` in any order as long as they work with [chalk]
- ie: `<#FF0000.bg#0000FF.bg-orange.keyword(red)>`
# APIs
### `chalker`
```js
chalker(str, [chalkInstance]);
```
- `str` - String with chalker color markers
- `chalkInstance` - Optional custom instance of [chalk].
- ie: created from `new chalk.constructor({level: 2})`
**Returns:** A string with terminal/ansi color codes
> If `chalk.supportsColor` is `false`, then it will simply remove the `<>` markers and decode HTML entities only.
### `chalker.CHALK`
```js
chalker.CHALK = require("ansi-colors");
```
Set the default colors library. By default, `chalker` loads `chalk` first and falls back to
[ansi-colors].
### `chalker.remove`
```js
chalker.remove(str, keepHtml);
```
- `str` - String with chalker color markers
- `keepHtml` - If `true`, then don't decode HTML entity escapes.
Simply remove all chalker markers and return the plain text string, with HTML escapes decoded.
**Returns**: A plain text string without chalker color markers
### `chalker.decodeHtml`
```js
chalker.decodeHtml(str);
```
- `str` - String to decode HTML entities
**Returns**: String with HTML entities escapes decoded
# License
Copyright (c) 2019-present, Joel Chen
Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
---
[demo]: ./images/demo.png
[ansi-colors]: https://www.npmjs.com/package/ansi-colors
[chalk]: https://www.npmjs.com/package/chalk
[chalk advanced colors]: https://github.com/chalk/chalk#256-and-truecolor-color-support
[travis-image]: https://travis-ci.org/jchip/chalker.svg?branch=master
[travis-url]: https://travis-ci.org/jchip/chalker
[npm-image]: https://badge.fury.io/js/chalker.svg
[npm-url]: https://npmjs.org/package/chalker
[daviddm-image]: https://david-dm.org/jchip/chalker/status.svg
[daviddm-url]: https://david-dm.org/jchip/chalker
[daviddm-dev-image]: https://david-dm.org/jchip/chalker/dev-status.svg
[daviddm-dev-url]: https://david-dm.org/jchip/chalker?type=dev