首先,可以使用npm安装
npm install colors.ts
或者git克隆它
如果你克隆或者下载,那么就需要build,如下
npm install
npm run build
npm run test
如何安装?
如果使用npm安装,请跳过
拷贝lib到你的工程路径下, 或者任意你喜欢的地方。
如何使用?
如果通过npm安装,你可以如下引用它
import Colors = require('colors.ts');
如果通过克隆或者下载,可以用如下方式引用:
import {Colors} from './lib/colors';
或者
require('./lib/colors');
接着,就可以使用啦。
import Colors = require('colors.ts');Colors.enable();console.log("this is a red string".red);Colors.colors("red", "this is a red string too!");
主题
你可以使用自定义主题
Colors.theme({error:"red"})console.log("this is a error".error);Colors.theme({error:"bgRed"})console.log("this is a error".error);
主题相关属性包括,verbose, info, debug, error四个常用的,以及custom0~custom9十个自定义位置。
灰度和256色
import Colors = require('colors.ts');Colors.enable();// range at [0, 25]console.log("gray".gray(20));console.log("gray".gray_bg(20));// range at [0, 255]console.log("256 colors".color_at_256(194));console.log("256 colors".color_bg_at_256(194));
网页安全色
import Colors = require('colors.ts');Colors.enable();console.log("web safe colors".colors("#336699"));console.log("web safe colors".colors("b#996633"));
着色方案
import Colors = require('colors.ts');Colors.enable();let ts = `class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; }}let greeter = new Greeter("world");let button = document.createElement('button');button.textContent = "Say Hello";button.onclick = function() { alert(greeter.greet());}document.body.appendChild(button);`;console.log(ts.paint([
{key:["let", "new", "alert", "class"], colors:"red"},
{key:"this", colors:"lightgreen"},
{key:"return", colors:"green"},
{key:"document", colors:"green"},
{key:new RegExp("string|number|function", "g"), colors:["#6600FF", "bold"]},
{key:new RegExp("\\{|\\}", "g"), colors:"bold"},
{key:new RegExp("\\(|\\)", "g"), colors:"bold"},
{key:[/"[^"]*"/g, /'[^']*'/g], colors:["lightyellow", "underline"]},
]))
API
Colors.colors(color: string | string[], value: string): string
返回颜色化的value
Colors.enable(value: boolean = true): void
是否启用颜色
Colors.theme(theme: { [key: string]: string | string[] }): void
设置主题
Colors.paint(paint: { key: string | string[] | RegExp | RegExp[], colors: string | string[] }[], value: string): string
着色