11defmodule Mix.Shell.Process do
22 @ moduledoc """
3- This is a Mix shell that uses the current process mailbox
4- for communication instead of IO.
3+ Mix shell that uses the current process mailbox for communication.
54
6- When a developer calls `info("hello")`, the following
7- message will be sent to the current process:
5+
6+ This module provides a Mix shell implementation that uses
7+ the current process mailbox for communication instead of IO.
8+
9+ As an example, when `Mix.shell.info("hello")` is called,
10+ the following message will be sent to the calling process:
811
912 {:mix_shell, :info, ["hello"]}
1013
1114 This is mainly useful in tests, allowing us to assert
12- if given messages were received or not. Since we need
13- to guarantee a clean slate between tests, there
14- is also a `flush/1` function responsible for flushing all
15- `:mix_shell` related messages from the process inbox.
15+ if given messages were received or not instead of performing
16+ checks on some captured IO. Since we need to guarantee a clean
17+ slate between tests, there is also a `flush/1` function
18+ responsible for flushing all `:mix_shell` related messages
19+ from the process inbox.
20+
21+ ## Examples
22+
23+ Mix.shell.info "hello"
24+ receive do {:mix_shell, :info, [msg]} -> msg end
25+ #=> "hello"
26+
27+ send self(), {:mix_shell_input, :prompt, "Pretty cool"}
28+ Mix.shell.prompt?("How cool was that?!")
29+ #=> "Pretty cool"
30+
1631 """
1732
1833 @ behaviour Mix.Shell
1934
2035 @ doc """
2136 Flushes all `:mix_shell` and `:mix_shell_input` messages from the current process.
37+
2238 If a callback is given, it is invoked for each received message.
2339
2440 ## Examples
@@ -83,12 +99,23 @@ defmodule Mix.Shell.Process do
8399
84100 @ doc """
85101 Forwards the message to the current process.
102+
86103 It also checks the inbox for an input message matching:
87104
88105 {:mix_shell_input, :prompt, value}
89106
90107 If one does not exist, it will abort since there was no shell
91- process inputs given. Value must be a string.
108+ process inputs given. `value` must be a string.
109+
110+ ## Examples
111+
112+ The following will answer with `"Meg"` to the prompt
113+ `"What's your name?"`:
114+
115+ # The response is sent before calling prompt/1 so that prompt/1 can read it
116+ send self(), {:mix_shell_input, :prompt, "Meg"}
117+ Mix.shell.prompt("What's your name?")
118+
92119 """
93120 def prompt ( message ) do
94121 print_app
@@ -103,12 +130,20 @@ defmodule Mix.Shell.Process do
103130
104131 @ doc """
105132 Forwards the message to the current process.
133+
106134 It also checks the inbox for an input message matching:
107135
108136 {:mix_shell_input, :yes?, value}
109137
110138 If one does not exist, it will abort since there was no shell
111- process inputs given. Value must be `true` or `false`.
139+ process inputs given. `value` must be `true` or `false`.
140+
141+ ## Example
142+
143+ # Send the response to self() first so that yes?/1 will be able to read it
144+ send self(), {:mix_shell_input, :yes?, true}
145+ Mix.shell.yes?("Are you sure you want to continue?")
146+
112147 """
113148 def yes? ( message ) do
114149 print_app
0 commit comments