我们如何在 Windows 上的 Iex 中清除屏幕

How Can We Clear the Screen in Iex on Windows

请问我们如何在 WindowsIex

清除屏幕

Iex 帮助中记录的方法不起作用:

This Whosebug Answer 也不适用于 Windows。

是的,据我所知,我们无法在 Windows 上清除它。如果有一个我们可以输出到 IO 设备以清除 Windows 上的屏幕的转义,我很想知道并将此功能也添加到 Windows。 :)

您最好的选择(如果这对您来说是一个真正的问题而不是烦恼)是使用支持 ANSI 转义序列的备用 Windows shell。参见 this S O question for why you can't simply use ANSI Escape sequences in a Windows Cmd Shell. One command shell alternative that does support ANSI is ConEmu。在您的机器上配置 ConEmu 留作 reader.

的练习。

您可以在 Windows 上的 iex 中直接使用 ANSI 代码和支持它们的控制台(例如 ConEmu 或 Windows 10 控制台。)

这将清除 iex 中的屏幕:

iex> IO.write "\e[H\e[J"; IEx.dont_display_result

解释:

  • IO.write 不带换行符输出到控制台
  • \e[ 是 ANSI CSI codes
  • 的前缀
  • H 是 CSI CUP – Cursor Position 代码,没有参数,默认移动光标到第 1 行,第 1 列
  • J 是 CSI ED – Erase Display 没有参数的代码,默认情况下从当前光标位置清除屏幕
  • IEx.dont_display_result防止IO.write:ok结果在清屏后显示

您还可以使用 IO.ANSI 而不是原始转义码来清除屏幕:

iex> IO.write [IO.ANSI.home, IO.ANSI.clear]; IEx.dont_display_result

clear/1 基本上就是 implemented

我发现这是可能的,因为 Windows 10 中的本机终端支持 ANSI 颜色和转义序列。唯一的事情是在 iex shell.

中启用它

根据 https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/io/ansi.ex#L41,此选项是可配置的。作为快速解决方案,只需在 iex 会话中输入以下代码:

Application.put_env(:elixir, :ansi_enabled, true)

为了使其永久化,您可以在~/.iex.exs 文件(https://hexdocs.pm/iex/IEx.html#module-configuring-the-shell) 中配置iex shell。只需将以下内容粘贴到文件中:

IEx.configure [colors: [enabled: true]]

将以下内容添加到您的主目录中的 ~/.iex.exs -- 如果该文件不存在,请创建它并添加以下内容。

Application.put_env(:elixir, :ansi_enabled, true)

编辑:请注意,您需要一个支持此功能的术语,ConEmu、Cmder 等。