npm在emacs eshell下不能正确显示提示符

5
这个问题 Node.js prompt '>' can not show in eshell 解决了使用 node repl 时的问题,但当你从 npm 中调用 node 时,这个解决方法不起作用。
例如,如果我运行以下命令:
$ 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

如果您有一个包含 "scripts" : { "start" : "node" } 的 package.json 文件。
$ npm start
npm WARN package.json nodo1@0.0.1 No README.md file found!
> node

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

我知道可以通过 "start": "env NODE_NO_READLINE=1 node" 来解决这个问题,但在每个地方都写这句话并不是一个好的解决方案。而且,也许其他使用该软件包的用户并不使用emacs,并且需要以其他方式设置环境变量。

我已经尝试了设置npm alias为NODE_NO_READLINE=1,但是结果仍然相同。

alias npm='env NODE_NO_READLINE=1 npm'
1个回答

5

这是comint模式过滤特殊字符的功能,许多shell前端使用该功能来着色文本。如果您曾经通过Emacs与shell程序交互,可能会熟悉类似“使用愚蠢终端或Emacs终端”的消息。有些程序可以检测到使用了Emacs或愚蠢终端,并不发送这些不能解释的字符,但Node则不支持此功能。不过,您可以像这样使用:

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

在你的 .emacs 文件中的某个地方。

Stackoverflow 无法正确复制控制字符,正则表达式中第一个字符(在引号和斜杠之后)是 ^[ 字符。你可以通过在 Emacs 中输入 C-q 0 3 3 来插入当前缓冲区中的控制字符。


1
你的解决方案在shell上运行正常,但在eshell上却不行。eshell使用eshell-output-filter-functions进行过滤,因此使用类似的方法(add-to-list 'eshell-output-filter-functions '(lambda () (save-excursion (replace-regexp "\[[0-9]+[GKJ]" "" nil eshell-last-output-start eshell-last-output-end))))可以解决问题,但速度较慢。我正在考虑如何告诉npm使用NODE_NO_READLINE=1。 - Roberto Huelga
我得到:
console.log('is this cutting stuff out in a weird way?') console.lo('is this cuttin stuff out in a weird way?')
这样做可以阻止特殊字符,但我不再拥有 g。如何使用 comint 特殊字符格式来创建正则表达式?
- Mittenchops
1
正则表达式必须是 "\033\[[0-9]+[GK]" 才能使得多窗口(mw)基于 https://dev59.com/P2Yr5IYBdhLWcg3wNHqh 在 Emacs 中使用 Node.js 与 JS Comint。 - pellekrogholt
1
将替换表达式修改为"^H."可能更适合某些人的喜好...即显示省略号;当然,应该是Ctrl-H加上一个句点。 - RoyM

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