5

This question Node.js prompt '>' can not show in eshell solve the problem for the node repl, but that solution don't work when you call node from npm.

By example if I make

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
^[[1G^[[0Jname: (nodo1) ^[[15G

Or if you have a package.json with a "scripts" : { "start" : "node" }

$ npm start
npm WARN package.json nodo1@0.0.1 No README.md file found!
> node

^[[1G^[[0J> ^[[3G

I know this one could be solved using "start" : "env NODE_NO_READLINE=1 node", but write this everywhere isn't a find solution. And maybe other user of the package don't use emacs and need set the env var in other way.

I have try with a alias for npm setting NODE_NO_READLINE=1 but the same result

alias npm='env NODE_NO_READLINE=1 npm'
Community
  • 1
  • 1
Roberto Huelga
  • 324
  • 3
  • 10

1 Answers1

5

This is the comint mode filtering the special characters many shell front-ends use to colorize the text. You are probably familiar with messages like "using a dumb terminal or Emacs terminal" if you ever interacted with a shell program through Emacs. Some can detect that Emacs or dumb terminal is used and will not send characters which those can't interpret, but Node doesn't do it. Regardless, you could use something like this:

(add-to-list
         'comint-preoutput-filter-functions
         (lambda (output)
           (replace-regexp-in-string "\\[[0-9]+[GK]" "" output)))

somewhere in your .emacs.

Stackoverflow won't copy the control characters properly, the very first character in the regexp (right after the quote and before the slash) is the ^[ character. You can input it in Emacs by doing C-q 0 3 3 anything you type next will cause the control character to be inserted in the current buffer.

  • 1
    Your solution works on shell but not in the eshell. The eshell use eshell-output-filter-functions to filter so using a similar approach (add-to-list 'eshell-output-filter-functions '(lambda () (save-excursion (replace-regexp "\\[[0-9]+[GKJ]" "" nil eshell-last-output-start eshell-last-output-end)))) works but is slow. What I'm thinking is how to tell npm to use the NODE_NO_READLINE=1 – Roberto Huelga Nov 02 '12 at 18:40
  • I get: > console.log('is this cutting stuff out in a weird way?') console.lo('is this cuttin stuff out in a weird way?') This stops the special characters, but I no longer have g. What does the comint special character format look like to make a regex? – Mittenchops Apr 09 '16 at 18:33
  • 1
    regex had to be "\033\\[[0-9]+[GK]" for mw to work based upon http://stackoverflow.com/questions/13862471/using-node-js-with-js-comint-in-emacs – pellekrogholt May 02 '16 at 11:40
  • 1
    modifying the replacement expression to be "^H." may be a bit more to some peoples liking.. i.e - show ellipsis; of course - it should be Ctrl-H followed by a period. – RoyM Mar 01 '17 at 20:54