-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
io.ex
77 lines (66 loc) · 1.63 KB
/
io.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
defmodule Mix.Shell.IO do
@moduledoc """
This is Mix's default shell.
It simply prints messages to stdio and stderr.
"""
@behaviour Mix.Shell
@doc """
Prints the currently running application if it
was not printed yet.
"""
def print_app do
if name = Mix.Shell.printable_app_name do
IO.puts "==> #{name}"
end
end
@doc """
Executes the given command and prints its output
to stdout as it comes.
"""
def cmd(command, opts \\ []) do
print_app? = Keyword.get(opts, :print_app, true)
Mix.Shell.cmd(command, opts, fn data ->
if print_app?, do: print_app()
IO.write(data)
end)
end
@doc """
Writes a message to the shell followed by new line.
"""
def info(message) do
print_app
IO.puts IO.ANSI.format message
end
@doc """
Writes an error message to the shell followed by new line.
"""
def error(message) do
print_app
IO.puts :stderr, IO.ANSI.format(red(message))
end
@doc """
Writes a message shell followed by prompting the user for
input. Input will be consumed until enter is pressed.
"""
def prompt(message) do
print_app
IO.gets message <> " "
end
@doc """
Receives a message and asks the user if they want to proceed.
The user must press enter or type anything that matches the "yes"
regex `~r/^Y(es)?$/i`.
"""
def yes?(message) do
print_app
got_yes? IO.gets(message <> " [Yn] ")
end
defp got_yes?(answer) when is_binary(answer) do
answer =~ ~r/^(Y(es)?)?$/i
end
# The io server may return :eof or :error
defp got_yes?(_), do: false
defp red(message) do
[:red, :bright, message]
end
end