如何给 Elixir iex 提示着色?
How to colorize Elixir iex prompt?
是否可以为 iex
prompt? Does iex
have a resource file (like .irbrc
for Ruby's irb
)? Is there a customization API that allows prompt customization (like Ruby's IRB.conf
) 添加颜色和其他效果?
对对对对!
要自定义您的提示,您需要几样东西:
- 您的主目录中有一个
.iex.exs
文件。如果此文件不存在,请创建它。它将在 iex
启动时执行。
- [可选] 调用
Application.put_env
以启用 ANSI。如果您平台上的 iex
(例如,Windows 10)未检测到 ANSI 支持,您可能需要这个。
- 调用
IEx.configure
以启用颜色并设置提示。
- ANSI escape codes to correct the cursor position. Without this, using up/down arrows to cycle through command history moves the cursor ever farther to the right.
IO.ANSI
目前不公开所有光标移动代码,但原始代码将适用于支持它们的终端。
IO.ANSI
格式化函数。
- 提示文字。
IO.ANSI.reset
关闭所有剩余的格式。
- 使用
IO.chardata_to_string
转换为字符串。
以下是终端中 iex
1.3.0 和 Windows 10 中 iTerm2 3.0.3 on OS X 10.11.5 and in Console, GitBash, and ConEmu 对我有用的方法:
# ~/.iex.exs
Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
colors: [enabled: true],
default_prompt: [
"\e[G", # ANSI CHA, move cursor to column 1
:magenta,
"%prefix", # IEx prompt variable
">", # plain string
:reset
] |> IO.ANSI.format |> IO.chardata_to_string
)
此代码运行良好,但我的提示仅在第一次交互后生效:当 iex
首次启动时,它会显示其内置提示。如果我点击 return,那么我的提示就会生效。如果有人知道如何解决这个问题,请分享。
[更新:已修改以在 Windows 上更好地工作。]
是否可以为 iex
prompt? Does iex
have a resource file (like .irbrc
for Ruby's irb
)? Is there a customization API that allows prompt customization (like Ruby's IRB.conf
) 添加颜色和其他效果?
对对对对!
要自定义您的提示,您需要几样东西:
- 您的主目录中有一个
.iex.exs
文件。如果此文件不存在,请创建它。它将在iex
启动时执行。 - [可选] 调用
Application.put_env
以启用 ANSI。如果您平台上的iex
(例如,Windows 10)未检测到 ANSI 支持,您可能需要这个。 - 调用
IEx.configure
以启用颜色并设置提示。 - ANSI escape codes to correct the cursor position. Without this, using up/down arrows to cycle through command history moves the cursor ever farther to the right.
IO.ANSI
目前不公开所有光标移动代码,但原始代码将适用于支持它们的终端。 IO.ANSI
格式化函数。- 提示文字。
IO.ANSI.reset
关闭所有剩余的格式。- 使用
IO.chardata_to_string
转换为字符串。
以下是终端中 iex
1.3.0 和 Windows 10 中 iTerm2 3.0.3 on OS X 10.11.5 and in Console, GitBash, and ConEmu 对我有用的方法:
# ~/.iex.exs
Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
colors: [enabled: true],
default_prompt: [
"\e[G", # ANSI CHA, move cursor to column 1
:magenta,
"%prefix", # IEx prompt variable
">", # plain string
:reset
] |> IO.ANSI.format |> IO.chardata_to_string
)
此代码运行良好,但我的提示仅在第一次交互后生效:当 iex
首次启动时,它会显示其内置提示。如果我点击 return,那么我的提示就会生效。如果有人知道如何解决这个问题,请分享。
[更新:已修改以在 Windows 上更好地工作。]