未加星标

Styling Output from Command-line Node.js Scripts with Chalk

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

The command-line is powerful, but oftentimes it’s bland and boring. Dark text on a light background, light text on a dark background. It can be seriously bleak. Fortunately it doesn’t have to be a monochromatic wasteland. With the addition of chalk to your project, you can easily style and colorize script output, providing not just eye candy, but an improved user experience as well.

chalk is a library that provides a simple and easy to use interface for applying ANSI colors and styles to your command-line output. The package is exceptionally popular with, at the time of this writing, ~4.2 million weekly downloads and it is one of the most depended upon packages over on npm .

What’s even more impressive is that the library’s maintainer, Sindre Sorhus, is a full-time open source contributor and currently maintains over 1100 projects!

Getting Started

Before we can start cutting through the drab, we need to add chalk to our project:

# via yarn $ yarn add chalk # via npm $ npm install chalk --save

And within our script, we will need to require it:

const chalk = require('chalk'); Usage

Using chalk is pretty painless, it works by chaining together colors and styles, finally passing in the string you’d like styled.

chalk is quite powerful, and in true UNIX philosophy, it does one thing well. Because of this, chalk by itself will only style your text, not output it. No big deal though, we can use the familiar console.log() to handle the output of the beautifully styled text.

Colorizing

With chalk we are able to colorize both the background and foreground of the output:

console.log(chalk.bgRed.green('Too early for Christmas?'));

Not ready for Christmas? That’s okay because we have a full palette of colors at our disposal:

black and bgBlack red and bgRed green and bgGreen yellow and bgYellow blue and bgBlue magenta and bgMagenta cyan and bgCyan white and bgWhite gray and bgBlackBright redBright and bgRedBright greenBright and bgGreenBright yellowBright and bgYellowBright blueBright and bgBlueBright magentaBright and bgMagentaBright cyanBright and bgCyanBright whiteBright and bgWhiteBright Styling

Styling works in the same way as colorizing output, just add it to the chain:

console.log( chalk.green.bgYellow.underline.bold('Alligators Rock!') );

There’s more to it than just underlining and emphasizing text:

bold underline dim inverse reset hidden visible

And a couple of additional options that aren’t widely supported and should probably be avoided until support does improve:

italic strikethrough Advanced Colorizing

You may have noticed that there were only 16 color pairs listed above, but most modern terminals are able to support 256 colors!

Instead of providing 256 different helper options, chalk provides support using the familiar hexadecimal color codes by way of the hex() and bgHex() options:

console.log( chalk.hex('#008f68').bgHex('#efbb35')('Alligators Rock!') );

You’re not limited to just hexadecimal either, chalk supports a wide variety of different color models:

hex and bgHex - chalk.hex('#4aae9b') rgb and bgRgb - chalk.rgb(74, 174, 155) hsl and bgHsl - chalk.hsl(169, 40, 49) hsv and bgHsv - chalk.hsv(47, 57, 68) hwb and bgHwb - chalk.hwb(169, 29, 32) keyword and bgKeyword - Accepts HTML/CSS color strings! Nested Styles

All of these colors and styles are well and good, but what if we wanted to mix things up a bit? Perhaps we want to change the color and bold a single word in a string that’s already being styled by chalk .

Because the chalk methods can accept a comma separated list of arguments, similar to console.log() we can apply styles inside of styles:

console.log( chalk.orange( 'I love', chalk.green.bold('Alligators'), ', don\'t you?' ) ); Bonus: Color Detection

Under the hood, chalk handles detecting whether or not colors are supported in the current terminal. As a convenience, this value is exposed, allowing you to create your own conditional logic around whether or not colors are supported:

if (!chalk.supportsColor) { throw new Error('Your terminal cannot handle this'); }

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Styling Output from Command-line Node.js Scripts with Chalk
本站链接:https://www.codesec.net/view/611885.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(13)