1
1
defmodule Mix.Shell.Process do
2
2
@ 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.
5
4
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:
8
11
9
12
{:mix_shell, :info, ["hello"]}
10
13
11
14
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
+
16
31
"""
17
32
18
33
@ behaviour Mix.Shell
19
34
20
35
@ doc """
21
36
Flushes all `:mix_shell` and `:mix_shell_input` messages from the current process.
37
+
22
38
If a callback is given, it is invoked for each received message.
23
39
24
40
## Examples
@@ -83,12 +99,23 @@ defmodule Mix.Shell.Process do
83
99
84
100
@ doc """
85
101
Forwards the message to the current process.
102
+
86
103
It also checks the inbox for an input message matching:
87
104
88
105
{:mix_shell_input, :prompt, value}
89
106
90
107
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
+
92
119
"""
93
120
def prompt ( message ) do
94
121
print_app
@@ -103,12 +130,20 @@ defmodule Mix.Shell.Process do
103
130
104
131
@ doc """
105
132
Forwards the message to the current process.
133
+
106
134
It also checks the inbox for an input message matching:
107
135
108
136
{:mix_shell_input, :yes?, value}
109
137
110
138
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
+
112
147
"""
113
148
def yes? ( message ) do
114
149
print_app
0 commit comments