Skip to content

Commit bedc358

Browse files
committed
Polish docs for Mix.shell and Mix.Shell.Process
I just tried to make docs more verbose and clear.
1 parent 62baed2 commit bedc358

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

lib/mix/lib/mix.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,25 @@ defmodule Mix do
210210
end
211211

212212
@doc """
213-
The shell is a wrapper for doing IO.
213+
Returns the current shell.
214214
215-
It contains conveniences for asking the user information,
216-
printing status and so forth. It is also swappable,
217-
allowing developers to use a test shell that simply sends the
218-
messages to the current process.
215+
`shell/0` can be used as a wrapper for the current shell. It contains
216+
conveniences for asking information to the user, printing things and so
217+
forth. The Mix shell is swappable (see `shell/1`), allowing developers to use
218+
a test shell that simply sends messages to the current process instead of
219+
doing IO (see `Mix.Shell.Process`).
220+
221+
By default, this returns `Mix.Shell.IO`.
219222
"""
220223
def shell do
221224
Mix.State.get(:shell, Mix.Shell.IO)
222225
end
223226

224227
@doc """
225228
Sets the current shell.
229+
230+
After calling this function, `shell` becomes the shell that is returned by
231+
`shell/0`.
226232
"""
227233
def shell(shell) do
228234
Mix.State.put(:shell, shell)

lib/mix/lib/mix/shell/process.ex

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
defmodule 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

Comments
 (0)