由于眼睛的问题,我不得不将控制台背景色改为白色,但字体是灰色的,它使消息无法阅读。我怎样才能改变呢?
当前回答
我创建了自己的模块StyleMe。我这样做,我可以用很少的打字做很多事情。例子:
var StyleMe = require('styleme');
StyleMe.extend() // extend the string prototype
console.log("gre{Hello} blu{world}!".styleMe()) // Logs hello world! with 'hello' being green, and 'world' being blue with '!' being normal.
它也可以被嵌套:
console.log("This is normal red{this is red blu{this is blue} back to red}".styleMe())
或者,如果你不想扩展字符串原型,你可以选择其他3个选项:
console.log(styleme.red("a string"))
console.log("Hello, this is yellow text".yellow().end())
console.log(styleme.style("some text","red,bbl"))
其他回答
日志/索引.js
const colors = {
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",
};
module.exports = () => {
Object.keys(colors).forEach(key => {
console['log' + key] = (strg) => {
if(typeof strg === 'object') strg = JSON.stringify(strg, null, 4);
return console.log(colors[key]+strg+'\x1b[0m');
}
});
}
app.js
require('./logger')();
然后这样使用它:
console.logBgGreen(" grüner Hintergrund ")
paint-console
简单的可着色原木。支持检查对象和单行更新 这个包只是重新油漆控制台。
安装
npm install paint-console
使用
require('paint-console');
console.info('console.info();');
console.warn('console.warn();');
console.error('console.error();');
console.log('console.log();');
demo
var to_rgb = function (_text, _r, _g, _b) {
return "\x1b[38;2;" + _r + ";" + _g + ";" + _b + "m" + _text + "\x1b[0m";
};
此代码帮助设置前景色:\x1b[38;2;R;G;Bm
这在某些地方可能行不通
如果你想改变颜色直接自己没有模块尝试
console.log('\x1b[36m', 'sometext' ,'\x1b[0m');
先\x1b[36m改变颜色为36,然后回到终端颜色0。
下面是ANSI颜色代码列表
var colorSet = {
Reset: "\x1b[0m",
Red: "\x1b[31m",
Green: "\x1b[32m",
Yellow: "\x1b[33m",
Blue: "\x1b[34m",
Magenta: "\x1b[35m"
};
var funcNames = ["info", "log", "warn", "error"];
var colors = [colorSet.Green, colorSet.Blue, colorSet.Yellow, colorSet.Red];
for (var i = 0; i < funcNames.length; i++) {
let funcName = funcNames[i];
let color = colors[i];
let oldFunc = console[funcName];
console[funcName] = function () {
var args = Array.prototype.slice.call(arguments);
if (args.length) {
args = [color + args[0]].concat(args.slice(1), colorSet.Reset);
}
oldFunc.apply(null, args);
};
}
// Test:
console.info("Info is green.");
console.log("Log is blue.");
console.warn("Warn is orange.");
console.error("Error is red.");
console.info("--------------------");
console.info("Formatting works as well. The number = %d", 123);
推荐文章
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?
- 回调函数来处理管道的完成
- Express函数中的“res”和“req”参数是什么?
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]
- Passport.js -错误:序列化用户到会话失败
- Node.js vs .Net性能
- 从电子应用程序中删除菜单栏
- 如何用node.js实现一个安全的REST API
- 如何处理Node.js中的循环依赖
- 在猫鼬,我如何排序的日期?(node . js)
- 如何在Node.js内进行远程REST调用?旋度吗?
- 如何用分发文件发布npm包?