【问题标题】:How to log colored messages with grunt.log如何使用 grunt.log 记录彩色消息
【发布时间】:2014-10-16 18:07:50
【问题描述】:

我想在运行特定任务时使用 Grunt 记录警告、黄色消息。

目前我使用grunt.log.subhead输出粗体字,但并没有引起太多注意。

grunt.log.subhead 'You may want to use --sourceMaps option'

【问题讨论】:

  • 最好使用github.com/Marak/colors.js之类的东西
  • Grunt 实际上是 使用colors 并且通过查看下面的两个很好的答案,在提出问题时也是如此。用于格式化文本的有趣语法是颜色的“超级漂亮方式”,它修改 String.prototype 以添加属性,例如 .green.bold

标签: node.js gruntjs


【解决方案1】:

你可以试试这个:

grunt.log.writeln('You may want to use --sourceMaps option'['yellow'].bold);

日志的 grunt API 文档没有提到如何做到这一点。我在这里的一组幻灯片中找到了它: http://slides.com/joshschumacher/grunt-logging#/

【讨论】:

  • 您也可以使用:grunt.log.write('Path: '.cyan);grunt.log.writeln(path);
  • 另外,如果您要连接字符串(即在字符串中使用变量),请先将所有内容括在括号中:grunt.log.writeln( ( 'concatenated ' + variable + ' and string need parenthesis' ).yellow )
【解决方案2】:

从@jon 提到的幻灯片中,有效的颜色是:

var colors = ['white', 'black', 'grey', 'blue', 'cyan', 'green', 'magenta', 'red', 'yellow', 'rainbow'];

colors.forEach(function (color) {
  grunt.log.writeln('testing'[color]);
  grunt.log.writeln('testing bold'[color].bold);
});

【讨论】:

  • 是否可以从 grunt 的对象中获取颜色数组?
猜你喜欢
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
相关资源
最近更新 更多