How do I stop npm's colored output from looking ugly in Emacs term-mode?
Asked Answered
C

3

22

When using npm in M-x term, it generate color message like this (even with -q) :

inverse color

Information from what-cursor-position

There are text properties here:
font-lock-face       (:foreground "red3" :background "black" :inverse-video nil)
fontified            t

It is ugly, and also hard to read in other themes, is it possible to change the color on the fly ? For example, change color of text that match npm http, npm ERR!

Thanks.

Containerize answered 20/12, 2013 at 15:24 Comment(4)
Have you tried npm config set color false?Reactionary
I didn't know that! Looks like it is more practical than make emacs change term color :)Containerize
Yeah, I figured the same -- there are lots of Emacs libraries whose Lisp code is accessible to the relative newcomer and straightforward to customize, but term-mode is not one of those libraries.Reactionary
So that this question doesn't hang around unanswered, I've promoted my earlier comment to an answer, and also edited the title to better reflect the sense of the question itself.Reactionary
R
36

You can disable colors in npm with the command:

npm config set color false

This doesn't exactly answer your question, in that it's not a way to override ANSI colors in term-mode, but it will solve your problem, in that the npm output will no longer be ugly and hard to read.

Reactionary answered 20/12, 2013 at 19:25 Comment(4)
You can also run your command as npm --no-color <arguments>, which is what I do in my scripts.Rejuvenate
this answer is not working for me. npm version 6.9.0Acerbic
@Acerbic Works here for npm 7.4.0 (Debian buster-backports)Dickerson
Neither config nor --no-color worked for me on Windows.Shortening
A
3

I created a wrapper for npm in davidchambers/dotfiles#1. Here's the code in full:

__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)
}

It removes the offensive background colours while preserving the rather nice foreground colours. :)

Ahmedahmedabad answered 26/4, 2014 at 6:52 Comment(0)
C
0

Since 2018:

https://github.com/npm/cli/commit/ecfbb16dc705f28aa61b3223bdbf9e47230a0fa4 config: disable color by default when $NO_COLOR is set (#19929).

Chez answered 15/11, 2023 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.