项目作者: squirrel-forge

项目描述 :
Node output/console styler
高级语言: JavaScript
项目地址: git://github.com/squirrel-forge/node-cfx.git
创建时间: 2021-06-16T16:42:37Z
项目社区:https://github.com/squirrel-forge/node-cfx

开源协议:MIT License

下载


@squirrel-forge/node-cfx

Node stdout/console styler, replace the basic console methods and allow for coloured output.

Install

  1. npm i @squirrel-forge/node-cfx

Example output

<a href=@squirrel-forge/node-cfx example text output">

Usage

Getting and using a default instance.

  1. const { cfx } = require( '@squirrel-forge/node-cfx' );
  2. cfx.log( '[ul][fgreen]underlined green text[re]' );

Default styled output.

  1. cfx.success( 'success' );
  2. cfx.error( 'error' );
  3. cfx.warn( 'warning' );
  4. cfx.info( 'info' );

Objects will not be styled, but outputted in plain, so you may mix arguments like with the console object.

  1. cfx.success( 'success', { some : 'object' }, 'another message' );

Get a styled string:

  1. const str = cfx.setStyle( '[ul][fgreen]underlined green text[re]' ); // \x1b[4m\x1b[32munderlined green text\x1b[0m

To enable automatic prefixing with the current timestamp set the prependTime property:

  1. cfx.prependTime = true;

To change the timestamp style, use the timestampPrefix and timestampSuffix properties:

  1. cfx.timestampPrefix = '[fwhite][[re][th]';
  2. cfx.timestampSuffix = '[re][fwhite]][re] ';

Setting a custom timestamp format, see time-stamp for details.

  1. cfx.timestampFormat = 'YYYY-MM-DD HH:mm:ss';

Styling options

Control and text style

Code Control Code Text
[re] Reset [bo] Bold
[rv] Reverse [th] Thin
- - [ul] Underline
- - [bl] Blink
- - [hd] Hidden

Text and background colors

Text Background Color
[fblack] [bblack] Black
[fred] [bred] Red
[fgreen] [bgreen] Green
[fyellow] [byellow] Yellow
[fblue] [bblue] Blue
[fmagenta] [bmagenta] Magenta
[fcyan] [bcyan] Cyan
[fwhite] [bwhite] White

Overriding default styles

Default styles and reset.

  1. cfx.style = {
  2. error : '[bred][fwhite] ',
  3. warn : '[byellow][fblack] ',
  4. info : '[bblack][fcyan] ',
  5. success : '[bgreen][fblack] ',
  6. };
  7. this.styleReset = ' [re]';

Default reset, used when no other reset is passed to the internal _write method, useful when making your own style methods.

  1. cfx.defaultReset = ' [re]';

Creating a custom instance

  1. const { ASCIIREF, OutputStyler } = require( '@squirrel-forge/node-cfx' );
  2. const myCfx = new OutputStyler( ASCIIREF, console );

Extending the styler

  1. const { ASCIIREF, OutputStyler } = require( '@squirrel-forge/node-cfx' );
  2. // Extended styler class
  3. class MyOutputStyler extends OutputStyler {
  4. constructor() {
  5. super( ASCIIREF, console );
  6. // Extend the constructor or set default options
  7. }
  8. // Add override or extend any methods
  9. }

Issues and docs

If you encounter any issues, please report here.


Check the sourcecode on github for detailed comments.