首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Mix.Shell.Process

使用当前进程邮箱进行通信的混合shell。

该模块提供了一个混合shell实现,它使用当前进程邮箱进行通信,而不是IO。

举个例子,当Mix.shell.info("hello")调用时,将向调用进程发送以下消息:

{:mix_shell, :info, ["hello"]}

这在测试中主要是有用的,它允许我们断言是否接收到给定的消息,而不是对某些捕获的IO执行检查。由于我们需要保证在测试之间保持干净,所以也有一个flush/1负责冲洗所有:mix_shell进程收件箱中的相关消息。

实例

Mix.shell.info "hello"
receive do {:mix_shell, :info, [msg]} -> msg end
#=> "hello"

send self(), {:mix_shell_input, :prompt, "Pretty cool"}
Mix.shell.prompt?("How cool was that?!")
#=> "Pretty cool"

功能

cmd(command, opts \ [])

执行给定的命令并将其消息转发给当前进程。

error(message)

将错误转发到当前进程。

flush(callback \ fn x -> x end)

冲淡一切:mix_shell:mix_shell_input来自当前进程的消息

info(message)

将消息转发到当前进程。

print_app()

如果尚未打印当前应用程序,则打印该应用程序。

prompt(message)

将消息转发到当前进程。

yes?(message)

将消息转发到当前进程。

cmd(command, opts \ [])

执行给定的命令并将其消息转发给当前进程。

error(message)

将错误转发到当前进程。

flush(callback \ fn x -> x end)

冲淡一切:mix_shell:mix_shell_input来自当前进程的消息。

如果给出回调,则对接收到的每条消息调用回调。

实例

flush &IO.inspect(&1)

info(message)

将消息转发到当前进程。

print_app()

如果尚未打印当前应用程序,则打印该应用程序。

prompt(message)

将消息转发到当前进程。

它还检查收件箱中的输入消息匹配:

{:mix_shell_input, :prompt, value}

如果不存在,它将中止,因为没有提供shell进程输入。value一定是一根绳子。

实例

以下将以"Meg"到提示符"What's your name?"*

# The response is sent before calling prompt/1 so that prompt/1 can read it
send self(), {:mix_shell_input, :prompt, "Meg"}
Mix.shell.prompt("What's your name?")

yes?(message)

将消息转发到当前进程。

它还检查收件箱中的输入消息匹配:

{:mix_shell_input, :yes?, value}

如果其中一个不存在,它将会中止,因为没有给出shell处理输入。value必须是truefalse

# Send the response to self() first so that yes?/1 will be able to read it
send self(), {:mix_shell_input, :yes?, true}
Mix.shell.yes?("Are you sure you want to continue?")

扫码关注腾讯云开发者

领取腾讯云代金券