如何在Emacs term-mode下防止npm的彩色输出看起来很丑?

22
在使用 M-x term 中的 npm 时,它会生成类似于这样的彩色消息(即使加上 -q 参数): 反色 来自 what-cursor-position 的信息。
There are text properties here:
font-lock-face       (:foreground "red3" :background "black" :inverse-video nil)
fontified            t

这看起来很丑,在其他主题中也很难阅读,是否可以动态更改颜色?例如,更改匹配npm httpnpm ERR!的文本颜色。

谢谢。


2
你尝试过使用 npm config set color false 吗? - Aaron Miller
我不知道!看起来比让emacs改变终端颜色更实用 :) - Rangi Lin
是的,我也想到了同样的事情 - 有很多Emacs库,其Lisp代码对于相对新手来说是可访问的并且容易定制,但term-mode不属于这些库之一。 - Aaron Miller
为了避免这个问题无人回答,我将之前的评论提升为答案,并且编辑了标题以更好地反映问题本身的意义。 - Aaron Miller
3个回答

36

您可以使用以下命令在npm中禁用颜色:

npm config set color false

这并没有直接回答你的问题,因为它不是在term模式下覆盖ANSI颜色的方法,但它可以解决你的问题,因为npm输出将不再难看且难以阅读。


32
你也可以将命令运行为 npm --no-color <arguments>,这是我在脚本中所做的。 - Guss
1
这个答案对我不起作用。npm 版本为 6.9.0。 - sureshvv
@sureshvv 在这里使用的是 npm 7.4.0(Debian buster-backports)。 - ceving
在Windows上,无论是config还是--no-color都对我没有用。 - Slion

3

我在 davidchambers/dotfiles#1 中创建了一个针对 npm 的包装器。以下是完整的代码:

__strip_background_colors() {
  local output="$(sed $'s:\x1B\[4[0-9]m::g')"
  [[ -n $output ]] && printf %s%s "$output" "$1"
}

npm() {
  # Strip the visually offensive background colours from npm's output,
  # leaving the foreground colours intact.
  NPM_CONFIG_COLOR=always "$(which npm)" "$@" \
    1> >(__strip_background_colors $'\n' >&1) \
    2> >(__strip_background_colors '' >&2)
}

它可以去除让人不舒服的背景颜色,同时保留非常好看的前景颜色。 :)

0
自2018年以来:
配置:当设置了$NO_COLOR时,默认禁用颜色(#19929)。
[链接](https://github.com/npm/cli/commit/ecfbb16dc705f28aa61b3223bdbf9e47230a0fa4)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接