npm.io
0.0.1 • Published 4 years ago

chalk-log-plus

Licence
GNU-3
Version
0.0.1
Deps
1
Size
155 kB
Vulns
0
Weekly
0

ChalkLog

A friendly terminal log output library based on chalk encapsulation

Github Stars Chalk Version license Author

中文

Why to develop ChalkLog?

Because when I added the log output code of the express project, I found that if I directly used chat, there would be many repeated fragments in the code, and it was too complex. So I encapsulated this simple library on the basis of chalk.

How to use?

Install chalk (Version less 5)

Chalk no longer supports the import of require() in nodejs after version 5, so you choose to use version 4 chalk. I will consider adapting chalks after version 5 in the future

npm install --save chalk@4
Require ChalkLog
  1. First download chalklog and put it into your nodejs project

  2. Then call chalklog

const log = require("./chalk-log")
Code supported

log is a class, including the following functions

  • add
  • output

Initializing class

const clog = new log();

When initializing, you can consider adding prefixes. Chalklog has the following prefix presets (preset_class)

  • info
  • error
  • success
  • connect

For example

// Case insensitive
const clog = new log("info");

If these prefix presets can't satisfy you, you can also define the chalk style yourself

// The following codes are equivalent to "chalk.rgb(0,0,0).bold"

// 1
const chalk = require("chalk");
const clog = new log(chalk.rgb(0,0,0).bold,"text");

// 2
const clog = new log("rgb(0,0,0).bold","text");

// 3
const clog = new log([ "rgb(0,0,0)","bold" ],"text");

The add() function can append output

If you only need to output content without font style matching, you only need to pass in one parameter: output content

clog.add("infomations")

Otherwise, you need to pass in the following two parameters

The first parameter can be the preset color name. Chalklog provides the following color presets (preset_color)

  • host
  • error

As in initialization, if the preset color cannot meet your needs, you can also define it yourself in the same way as in initialization

The second parameter is the output content

Finally, the content is output only when output() is called

clog.output();

After using output() to output, you can still use add() to continue appending content and output again with output()

Abbreviation supported

this code

const clog = new log("rgb(0,0,0).bold","text");
clog.add("infomations")
clog.output();

It can be abbreviated as

new log("rgb(0,0,0).bold","text").add("infomations").output();

It should be noted that if you use the shorthand method, you cannot use add() to continue adding content or output() to output after using output()

Writing attention

  1. You don't need to pay too much attention to the writing method when passing in the custom style of non chalk function (for example, when bold is required, "bold" and "bold()" are both OK)

I am still learning nodejs. If the program reports errors, please feed back in issues. Thank you for using!

Keywords