美文网首页
让你的控制台更加优秀 - colors模块

让你的控制台更加优秀 - colors模块

作者: 小杺 | 来源:发表于2018-05-31 15:01 被阅读15次

前言

Console的各种方法默认没有颜色,这样的话,在信息量大的时候真不好找.幸好已经有人提供了Node.Js的颜色模块 Colors.

基本使用方法

const colors = require( "colors")

//使用 colors 模块
console.log('红色'.red);
console.log('绿色'.green);
console.log('下划线加白色'.underline.white)
console.log('反转颜色'.inverse);
console.log('彩虹色'.rainbow);
console.log("乱七八糟".zalgo);

自定义名称方法

const colors = require( "colors")

//使用 colors 模块
colors.setTheme({
  silly: 'rainbow',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
  info: 'green',
  data: 'grey',
  help: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});
console.log("this is an error".error);
console.log("this is a warning".warn);

编码写法

%s是后面的字符串插入的位置,\x1b[0m重置了控制台的颜色,这样在此之后才不会将代码编程选定的颜色。\x1b[36m被称作escape sequence他将拦截你的字符串并将其转换成指定的颜色,escape sequence处理颜色和样式的模式被称做ANSI escape code,这个颜色模式已经被标准化,所以可以运行在不同的操作系统上。

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');  //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow);  //yellow

更多的颜色参考如下

Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"

关于node.js的colors模块 - stackoverflow

相关文章

  • 让你的控制台更加优秀 - colors模块

    前言 Console的各种方法默认没有颜色,这样的话,在信息量大的时候真不好找.幸好已经有人提供了Node.Js的...

  • 2018-10-11

    1、简化控制台WDS的输出devServer:{colors: true,modules: false,child...

  • 复利

    让优秀成为一种习惯,好的习惯让你更加优秀。

  • 学习让你更加优秀

    爱是什么,爱就是心里一种美好的感觉,美好感觉就是爱,痛苦的感觉就是害,昨天学习分享

  • 《圈子让你更加优秀》

    两年前我接触到:“选择高质量人群,远离低质量人群”。那时候我朋友圈精进成长的人,会删除朋友圈很多人,这些人是低质量...

  • 刻意练习:让你更加优秀,更加卓越

    世界冠军为什么会成为世界冠军,答案就是他们不断地刻意练习。首先,刻意练习意味着要摆脱舒适区,集中精力做自己不擅长的...

  • 落选,是让你更加优秀!

    最近参加优秀评选,从候选惊喜,再到拉票紧张心惊胆跳,再到落选失落,再到释然!我想这经历也算是很好考验! 人不可能在...

  • 如何让你的孩子更加优秀

    最近中国诗词大会大热,赛场上很多选手的表现是不是让你眼前为之一亮,在欣赏他们的同时,你有没有思考一些问题,你有没有...

  • NodeJS

    使用 colors 模块 console.log('红色'.red); console.log('绿色'.gree...

  • 让自己更加优秀

    没有时间,就要挤出来,任务很多,也要见缝插针地想一想,自己的梦想还在那里等着,如果我不努力的话,我的梦想它可怎么活...

网友评论

      本文标题:让你的控制台更加优秀 - colors模块

      本文链接:https://www.haomeiwen.com/subject/qvuqsftx.html