An open API service indexing awesome lists of open source software.

https://github.com/dmnsgn/console-ansi

Easy console coloring and prefixing via Proxy object with ANSI strings.
https://github.com/dmnsgn/console-ansi

ansi ansi-colors chalk colors console prefix proxy symbol theme

Last synced: 5 months ago
JSON representation

Easy console coloring and prefixing via Proxy object with ANSI strings.

Awesome Lists containing this project

README

          

# console-ansi

[![npm version](https://img.shields.io/npm/v/console-ansi)](https://www.npmjs.com/package/console-ansi)
[![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)](https://www.npmjs.com/package/console-ansi)
[![npm minzipped size](https://img.shields.io/bundlephobia/minzip/console-ansi)](https://bundlephobia.com/package/console-ansi)
[![dependencies](https://img.shields.io/librariesio/release/npm/console-ansi)](https://github.com/dmnsgn/console-ansi/blob/main/package.json)
[![types](https://img.shields.io/npm/types/console-ansi)](https://github.com/microsoft/TypeScript)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-fa6673.svg)](https://conventionalcommits.org)
[![styled with prettier](https://img.shields.io/badge/styled_with-Prettier-f8bc45.svg?logo=prettier)](https://github.com/prettier/prettier)
[![linted with eslint](https://img.shields.io/badge/linted_with-ES_Lint-4B32C3.svg?logo=eslint)](https://github.com/eslint/eslint)
[![license](https://img.shields.io/github/license/dmnsgn/console-ansi)](https://github.com/dmnsgn/console-ansi/blob/main/LICENSE.md)

Easy console coloring and prefixing via Proxy object with ANSI strings. Works in Node.js and in browsers that support ANSI strings.

[![paypal](https://img.shields.io/badge/donate-paypal-informational?logo=paypal)](https://paypal.me/dmnsgn)
[![coinbase](https://img.shields.io/badge/donate-coinbase-informational?logo=coinbase)](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3)
[![twitter](https://img.shields.io/twitter/follow/dmnsgn?style=social)](https://twitter.com/dmnsgn)

![](https://raw.githubusercontent.com/dmnsgn/console-ansi/main/screenshot.jpg)

## Installation

```bash
npm install console-ansi
```

## Usage

```js
import console, { styles } from "console-ansi";

// Set prefix
console.prefix = "[test]";

// Call the console object methods as usual
console.time("Test time");
console.log("Test log");
console.info("Test info");
console.warn("Test warn");
console.error("Test error");
console.timeEnd("Test time");

// Customise color
console.theme.log = styles.blue;
console.log("Test log blue");

// Remove symbols
console.symbol = {};
```

## API

## Modules


console-ansi


## Typedefs



ConsoleAnsi : object



ConsoleAnsiThemeAttributeArray : Array.<number>


Array for ANSI definition [start, end].




ConsoleAnsiTheme : Object.<string, ConsoleAnsiThemeAttributeArray> | Object.<string, Array.<ConsoleAnsiThemeAttributeArray>>


Theme object consisting of ANSI styles or Array of ANSI styles.




ConsoleAnsiLevel : string


Current log level. Can be any console method but levels property only defines log(1)/info(2)/warn(3)/error(4).




ConsoleAnsiLevels : Object.<ConsoleAnsiLevel, number>


Levels object consisting of console method as keys and numbered priority.




ConsoleAnsiSymbol : Object.<string, string>


Map of unicode symbols to be prepended to certain console methods.



## console-ansi

- [console-ansi](#module_console-ansi)
- [.styles](#module_console-ansi.styles) : [ConsoleAnsiTheme](#ConsoleAnsiTheme)
- [.default](#module_console-ansi.default) : [ConsoleAnsi](#ConsoleAnsi)
- [.getConsole(options)](#module_console-ansi.getConsole) ⇒ [ConsoleAnsi](#ConsoleAnsi)

### console-ansi.styles : [ConsoleAnsiTheme](#ConsoleAnsiTheme)

Basic ANSI escape codes map

**Kind**: static property of [console-ansi](#module_console-ansi)
**See**

- [Wikipedia ANSI]()
- [Node.js util](https://nodejs.org/api/util.html#util_customizing_util_inspect_colors)

### console-ansi.default : [ConsoleAnsi](#ConsoleAnsi)

Export a Proxy object to automatically style the console with ANSI strings.

**Kind**: static property of [console-ansi](#module_console-ansi)

### console-ansi.getConsole(options) ⇒ [ConsoleAnsi](#ConsoleAnsi)

Get an instance of the Proxy-ed console. Useful if you need different prefixes for instance.

**Kind**: static method of [console-ansi](#module_console-ansi)

| Param | Type |
| ------- | ---------------------------------------- |
| options | [ConsoleAnsi](#ConsoleAnsi) |

## ConsoleAnsi : object

**Kind**: global typedef
**Properties**

| Name | Type | Default | Description |
| --------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| [prefix] | string | "\"\"" | A string to prepend to every log. |
| [theme] | [ConsoleAnsiTheme](#ConsoleAnsiTheme) | | Color definition associated to console methods. |
| [level] | [ConsoleAnsiLevel](#ConsoleAnsiLevel) | "log" | A minimum log level value. See ConsoleAnsiLevels. |
| [levels] | [ConsoleAnsiLevels](#ConsoleAnsiLevels) | { error: 5, warn: 4, info: 3, log: 2 } | Numbered priority associated to console methods to match above for level property. |
| [symbol] | [ConsoleAnsiSymbol](#ConsoleAnsiSymbol) | { log: "✔", info: "ℹ", warn: "⚠", error: "✖" } | Unicode symbols to prepend to defined console methods. |
| [noColor] | boolean | false | Disable color ansi sequence. |

## ConsoleAnsiThemeAttributeArray : Array.<number>

Array for ANSI definition [start, end].

**Kind**: global typedef

## ConsoleAnsiTheme : Object.<string, ConsoleAnsiThemeAttributeArray> \| Object.<string, Array.<ConsoleAnsiThemeAttributeArray>>

Theme object consisting of ANSI styles or Array of ANSI styles.

**Kind**: global typedef

## ConsoleAnsiLevel : string

Current log level. Can be any console method but levels property only defines log(1)/info(2)/warn(3)/error(4).

**Kind**: global typedef

## ConsoleAnsiLevels : Object.<ConsoleAnsiLevel, number>

Levels object consisting of console method as keys and numbered priority.

**Kind**: global typedef

## ConsoleAnsiSymbol : Object.<string, string>

Map of unicode symbols to be prepended to certain console methods.

**Kind**: global typedef

## License

MIT. See [license file](https://github.com/dmnsgn/console-ansi/blob/main/LICENSE.md).