Node series – 005 – colors.js

Time:2021-11-4

Accesscommander.jsandInquirer.jsAfter that, it should have been connected directlycolors.jsAfter all, we are now the console output, and the console is not flashy.

But the first partInquires.jsIt’s too awesome to go directly to the 1.7w word, so this is a relatively brief one.

Reading tips: This article is short and easy to read. Next, it will make complaints about Tucao.

I. Preface

colors.jsMarak built a 4.1K star (2021-06-16) warehouse.

Accesscolors.jsIt can make your console more explosive and aesthetic.

  • Installation:npm i colors
  • Enter code:

src/index.ts

import program from 'commander';
import common from './common';
import colors from 'colors';

program
  .version('0.0.1')
  . description ('tool Library ')

program
  .command('jsliang')
  . description ('jsliang help instruction ')
  .action(() => {
    common();
  });

program
  .command('test')
  . description ('test channel ')
  .action(() => {
    const text = `
      _   _____   _       _       ___   __   _   _____  
     | | /  ___/ | |     | |     /   | |  \\ | | /  ___| 
     | | | |___  | |     | |    / /| | |   \\| | | |     
  _  | | \\___  \\ | |     | |   / /—| | | |\\   | | |  _  
 | |_| |  ___| | | |___  | |  / /  | | | | \\  | | |_| |
 \\_____/ /_____/ |_____| |_| /_/   |_| |_|  \\_| \\_____/
    `;
    console.log(colors.rainbow(text));
  });

program.parse(process.argv);

package.json

{
  "name": "jsliang",
  "version": "1.0.0",
  "Description": "Fe util, node tool library",
  "main": "index.js",
  "scripts": {
    "jsliang": "ts-node ./src/index.ts jsliang",
    "test": "ts-node ./src/index.ts test"
  },
  "keywords": [
    "jsliang",
    "Node tool library",
    "Node"
  ],
  "author": "jsliang",
  "license": "ISC",
  "devDependencies": {
    "@types/inquirer": "^7.3.1",
    "@types/node": "^15.12.2",
    "@typescript-eslint/eslint-plugin": "^4.26.1",
    "@typescript-eslint/parser": "^4.26.1",
    "eslint": "^7.28.0",
    "ts-node": "^10.0.0",
    "typescript": "^4.3.2"
  },
  "dependencies": {
    "colors": "^1.4.0",
    "commander": "^7.2.0",
    "inquirer": "^8.1.0",
    "rxjs": "^5.5.12"
  }
}
  • implementnpm run test

I found the console old and beautiful:

Node series - 005 - colors.js

In the above code, addedtestRelevant instructions (we’ll plug the test content here later. You don’t need to add it, butjsliangWill be used as an example)

As for this beautiful font, it is converted through ASCII art word converter.

  • Kalvin online tools
  • ASCII font generator

Two are recommended here at will. More small partners can dig by themselves.

II. Colors.js

Sharp tools make good work.

On it we showcolors.jsA rainbow color incolors.rainbow, then there must be other colors.

import colors from 'colors';

console.log(colors.rainbow('rainbow'));
console.log(colors.black('black'));
console.log(colors.red('red'));
console.log(colors.green('green'));
console.log(colors.yellow('yellow'));
console.log(colors.blue('blue'));
console.log(colors.magenta('magenta'));
console.log(colors.cyan('cyan'));
console.log(colors.white('white'));
console.log(colors.gray('gray'));
console.log(colors.grey('grey'));
console.log(colors.bgBlack('bgBlack'));
console.log(colors.bgRed('bgRed'));
console.log(colors.bgGreen('bgGreen'));
console.log(colors.bgYellow('bgYellow'));
console.log(colors.bgBlue('bgBlue'));
console.log(colors.bgMagenta('bgMagenta'));
console.log(colors.bgCyan('bgCyan'));
console.log(colors.bgWhite('bgWhite'));
console.log(colors.bgGrey('bgGrey'));
console.log(colors.reset('reset'));
console.log(colors.bold('bold'));
console.log(colors.dim('dim'));
console.log(colors.italic('italic'));
console.log(colors.underline('underline'));
console.log(colors.inverse('inverse'));
console.log(colors.hidden('hidden'));
console.log(colors.strikethrough('strikethrough'));
console.log(colors.rainbow('rainbow'));
console.log(colors.zebra('zebra'));
console.log(colors.america('america'));
console.log(colors.trap('trap'));
console.log(colors.random('random'));

Throw them intestIn, executenpm run test, get the fancy print in the flower:

Node series - 005 - colors.js

Third, rewrite console.log

OK, we’ve already made a magnificent whistle on it. If you print it every time, you have to quote it againcolorsThat’s a little unreasonable.

So let’s rewrite itconsole.logIn this way, as long as there is a place to use it, we will have rainbow color!

base/getType.ts

/**
 * @name getType
 *@ description get type
 *@ param {string | object} param passed in variable
 */
export const getType = (param: string): string => {
  return Object.prototype.toString.call(param).slice(8, -1);
};

base/console.ts

import colors from 'colors';
import { getType } from './getType';

//Print index
let consoleIndex = 1;

//Rewrite console.log
const log = console.log;
console.log = (...args: any) => {
  log(`\n---${consoleIndex++}---`);
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (['String', 'Number', 'Boolean'].includes(getType(arg))) {
      log(colors.rainbow(String(arg)));
    } else {
      log(arg);
    }
  }
};

//Override console.error
const error = console.error;
console.error = (...args: any) => {
  log(`\n---${consoleIndex++}---`);
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (['String', 'Number', 'Boolean'].includes(getType(arg))) {
      error(colors.red(String(arg)));
    } else {
      error(arg);
    }
  }
};

Then insrc/index.tsReference this override inconsole.tsIn this way, the global can use:

src/index.ts

import program from 'commander';
import common from './common';
import './base/console';

program
  .version('0.0.1')
  . description ('tool Library ')

program
  .command('jsliang')
  . description ('jsliang help instruction ')
  .action(() => {
    common();
  });

program
  .command('test')
  . description ('test channel ')
  .action(() => {
    console.log('There is jsliang?', true);
    Console. Error ('casually reporting an error indicates that there is a problem ');
  });

program.parse(process.argv);

Run at this timenpm run testThe printing effect is:

Node series - 005 - colors.js

In fact, the rainbow color looks too fancy, but it won’t be changed here for the time being. Partners can change the color themselves

Then, the fancy access is over. Although they are all API copy and paste engineers, it is still possible to make the decoration look better~

See you next!

IV. references

  • GitHub:Marak/colors.js
  • Kalvin online tools
  • ASCII font generator

No tossing front end, what’s the difference between salted fish and salted fish!

Jsliang’s document library is licensed by Liang Junrong under the 4.0 international license agreement of knowledge sharing signature – non-commercial use – sharing in the same way< Br / > based on https://github.com/LiangJunrong/document-library Creation of works on< Br / > use rights other than those authorized by this license agreement can be obtained from https://creativecommons.org/licenses/by-nc-sa/2.5/cn/ Obtained from.

Recommended Today

redis day2

persistence mechanism client redis[memory] —–&gt; memory data-data persistence–&gt;disk Redis officially provides two different persistence methods to store data in memory to the hard disk: Snapshot AOF (Append Only File) only appends log files   Snapshot Features:In this way, all data at a certain moment can be written to the hard disk, of course, this is […]