Skip to content

Commit 55d0f56

Browse files
author
José Valim
committed
Improve docs and code regarding ansi formatting
1 parent 8bfe324 commit 55d0f56

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

lib/ex_unit/mix.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ defmodule ExUnit.Mixfile do
1111
[registered: [ExUnit.Server],
1212
mod: {ExUnit, []},
1313
env: [
14+
# Calculated on demand
15+
# max_cases: :erlang.system_info(:schedulers_online),
16+
# color: IO.ANSI.terminal?,
17+
# seed: rand(),
18+
1419
autorun: true,
1520
trace: false,
1621
formatters: [ExUnit.CLIFormatter],

lib/iex/lib/iex.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,10 @@ defmodule IEx do
445445

446446
@doc false
447447
def default_colors do
448-
[# Used by default on evaluation cycle
448+
[# Enabled should be commented as it is calculated if missing
449+
# enabled: true,
450+
451+
# Used by default on evaluation cycle
449452
eval_interrupt: "yellow",
450453
eval_result: "yellow",
451454
eval_error: "red",

lib/iex/lib/iex/introspection.ex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ defmodule IEx.Introspection do
1515
if function_exported?(module, :__info__, 1) do
1616
case module.__info__(:moduledoc) do
1717
{_, binary} when is_binary(binary) ->
18-
if IO.ANSI.terminal? do
19-
options = docs_options()
20-
IO.ANSI.Docs.print_heading(inspect(module), options)
21-
IO.ANSI.Docs.print(binary, options)
18+
if opts = ansi_docs() do
19+
IO.ANSI.Docs.print_heading(inspect(module), opts)
20+
IO.ANSI.Docs.print(binary, opts)
2221
else
2322
IO.puts "* #{inspect(module)}\n"
2423
IO.puts binary
@@ -159,10 +158,9 @@ defmodule IEx.Introspection do
159158
heading = "#{kind} #{fun}(#{args})"
160159
doc = doc || ""
161160

162-
if IO.ANSI.terminal? do
163-
options = docs_options()
164-
IO.ANSI.Docs.print_heading(heading, options)
165-
IO.ANSI.Docs.print(doc, options)
161+
if opts = ansi_docs() do
162+
IO.ANSI.Docs.print_heading(heading, opts)
163+
IO.ANSI.Docs.print(doc, opts)
166164
else
167165
IO.puts "* #{heading}\n"
168166
IO.puts doc
@@ -177,8 +175,11 @@ defmodule IEx.Introspection do
177175
atom_to_binary(var)
178176
end
179177

180-
defp docs_options() do
181-
[width: IEx.width] ++ Application.get_env(:iex, :colors)
178+
defp ansi_docs() do
179+
opts = Application.get_env(:iex, :colors)
180+
if opts[:enabled] do
181+
[width: IEx.width] ++ opts
182+
end
182183
end
183184

184185
@doc """

lib/iex/test/iex/interaction_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ defmodule IEx.InteractionTest do
113113
# Test that ANSI escapes in the docs are left alone
114114
opts = [colors: [enabled: true]]
115115
assert capture_iex("h IEx.InteractionTest.ansi_escapes", opts)
116-
== "* def ansi_escapes()\n\nHello, I have %{red}ANSI%{reset} escapes."
116+
=~ ~r"Hello, I have %\{red}ANSI%\{reset} escapes"
117117

118118
# Test that ANSI escapes in iex output are left alone
119119
opts = [colors: [enabled: true, eval_result: "red", eval_info: "red"]]

lib/mix/lib/mix/tasks/help.ex

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,34 @@ defmodule Mix.Tasks.Help do
44
@shortdoc "Print help information for tasks"
55

66
@moduledoc """
7-
If given a task name, prints the documentation for that task.
8-
If no task name is given, prints the short form documentation
9-
for all tasks.
7+
Lists all tasks or prints the documentation for a given task.
108
119
## Arguments
1210
1311
mix help - prints all tasks and their shortdoc
1412
mix help TASK - prints full docs for the given task
1513
14+
## Colors
15+
16+
When possible, `mix help` is going to use coloring for formatting
17+
guides. The formatting can be customized by configuring the Mix
18+
application either inside your project (in `config/config.exs`) or
19+
by using the local config (in `~/.mix/config.exs`).
20+
21+
For example, to disable, one may:
22+
23+
[mix: [colors: [enabled: false]]]
24+
25+
The available color options are:
26+
27+
* `:enabled` - show ANSI formatting (defaults to IO.ANSI.terminal?)
28+
* `:doc_code` — the attributes for code blocks (cyan, bright)
29+
* `:doc_inline_code` - inline code (cyan)
30+
* `:doc_headings` - h1 and h2 (yellow, bright)
31+
* `:doc_title` — the overall heading for the output (reverse,yellow,bright)
32+
* `:doc_bold` - (bright)
33+
* `:doc_underline` - (underline)
34+
1635
"""
1736

1837
def run([]) do
@@ -42,11 +61,12 @@ defmodule Mix.Tasks.Help do
4261
def run([task]) do
4362
module = Mix.Task.get!(task)
4463
doc = Mix.Task.moduledoc(module) || "There is no documentation for this task"
64+
opts = Application.get_env(:mix, :colors)
4565

46-
if IO.ANSI.terminal? do
47-
options = [width: width] ++ Application.get_env(:mix, :colors)
48-
IO.ANSI.Docs.print_heading("mix help #{task}", options)
49-
IO.ANSI.Docs.print(doc, options)
66+
if ansi_docs?(opts) do
67+
opts = [width: width] ++ opts
68+
IO.ANSI.Docs.print_heading("mix help #{task}", opts)
69+
IO.ANSI.Docs.print(doc, opts)
5070
else
5171
IO.puts "# mix help #{task}\n"
5272
IO.puts doc
@@ -59,6 +79,14 @@ defmodule Mix.Tasks.Help do
5979
raise Mix.Error, message: "Unexpected arguments, expected `mix help` or `mix help TASK`"
6080
end
6181

82+
defp ansi_docs?(opts) do
83+
if Keyword.has_key?(opts, :enabled) do
84+
opts[:enabled]
85+
else
86+
IO.ANSI.terminal?
87+
end
88+
end
89+
6290
defp width() do
6391
case :io.columns(:standard_input) do
6492
{:ok, width} -> min(width, 80)

0 commit comments

Comments
 (0)