Skip to content

Commit 8b203a8

Browse files
author
José Valim
committed
Use IO.ANSI.Docs to format mix help
1 parent de26484 commit 8b203a8

File tree

10 files changed

+96
-81
lines changed

10 files changed

+96
-81
lines changed

lib/eex/mix.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ defmodule EEx.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[app: :eex, version: System.version, build_per_environment: false]
5+
[app: :eex,
6+
version: System.version,
7+
build_per_environment: false]
68
end
79
end

lib/elixir/lib/io/ansi/docs.ex

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,42 @@ defmodule IO.ANSI.Docs do
33

44
@bullets [?*, ?-, ?+]
55

6-
@default_options [enabled: true,
7-
doc_code: "cyan,bright",
8-
doc_inline_code: "cyan",
9-
doc_headings: "yellow,bright",
10-
doc_title: "reverse,yellow,bright",
11-
doc_bold: "bright",
12-
doc_underline: "underline",
13-
width: 80]
6+
@doc """
7+
The default options used by this module.
8+
9+
The supported values are:
10+
11+
* `:enabled` - toggles coloring on and off (true)
12+
* `:doc_code` - code blocks (cyan, bright)
13+
* `:doc_inline_code` - inline code (cyan)
14+
* `:doc_headings` - h1 and h2 headings (yellow, bright)
15+
* `:doc_title` - top level heading (reverse, yellow, bright)
16+
* `:doc_bold` - bold text (bright)
17+
* `:doc_underline` - underlined text (underline)
18+
* `:width` - the width to format the text (80)
19+
20+
Values for the color settings are strings with
21+
comma-separated ANSI values.
22+
"""
23+
def default_options do
24+
[enabled: true,
25+
doc_code: "cyan,bright",
26+
doc_inline_code: "cyan",
27+
doc_headings: "yellow,bright",
28+
doc_title: "reverse,yellow,bright",
29+
doc_bold: "bright",
30+
doc_underline: "underline",
31+
width: 80]
32+
end
1433

1534
@doc """
1635
Prints the head of the documentation (i.e. the function signature).
1736
18-
See `print/3` for docs on the supported options.
37+
See `default_options/0` for docs on the supported options.
1938
"""
2039
def print_heading(heading, options \\ []) do
2140
IO.puts IO.ANSI.reset
22-
options = Keyword.merge(@default_options, options)
41+
options = Keyword.merge(default_options, options)
2342
width = options[:width]
2443
padding = div(width + String.length(heading), 2)
2544
heading = heading |> String.rjust(padding) |> String.ljust(width)
@@ -29,25 +48,11 @@ defmodule IO.ANSI.Docs do
2948
@doc """
3049
Prints the documentation body.
3150
32-
In addition to the priting string, takes a truth value for whether to use ANSI
33-
escape codes, and a keyword list for the printing color settings. Supported
34-
options are:
35-
36-
* `:enabled` - toggles coloring on and off (true)
37-
* `:doc_code` - code blocks (cyan, bright)
38-
* `:doc_inline_code` - inline code (cyan)
39-
* `:doc_headings` - h1 and h2 headings (yellow, bright)
40-
* `:doc_title` - top level heading (reverse, yellow, bright)
41-
* `:doc_bold` - bold text (bright)
42-
* `:doc_underline` - underlined text (underline)
43-
* `:width` - the width to format the text
44-
45-
Values for the color settings are strings with comma-separated
46-
ansi values.
47-
51+
In addition to the priting string, takes a set of options
52+
defined in `default_options/1`.
4853
"""
4954
def print(doc, options \\ []) do
50-
options = Keyword.merge(@default_options, options)
55+
options = Keyword.merge(default_options, options)
5156
doc
5257
|> String.split(["\r\n","\n"], trim: false)
5358
|> Enum.map(&String.rstrip/1)

lib/elixir/mix.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ defmodule Elixir.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[ app: :elixir,
6-
version: System.version,
7-
build_per_environment: false,
8-
escript_embed_elixir: false,
9-
escript_main_module: :elixir,
10-
escript_emu_args: "%%! -noshell\n" ]
5+
[app: :elixir,
6+
version: System.version,
7+
build_per_environment: false,
8+
escript_embed_elixir: false,
9+
escript_main_module: :elixir,
10+
escript_emu_args: "%%! -noshell\n"]
1111
end
1212
end

lib/elixir/test/elixir/io/ansi/docs_test.exs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@ defmodule IO.ANSI.DocsTest do
44
use ExUnit.Case, async: true
55
import ExUnit.CaptureIO
66

7-
@colors [ enabled: true,
8-
doc_code: "cyan,bright",
9-
doc_inline_code: "cyan",
10-
doc_bold: "bright",
11-
doc_underline: "underline",
12-
doc_headings: "yellow,bright",
13-
doc_title: "reverse,yellow,bright" ]
14-
157
def format_heading(str) do
16-
capture_io(fn -> IO.ANSI.Docs.print_heading(str, @colors) end) |> String.strip
8+
capture_io(fn -> IO.ANSI.Docs.print_heading(str, []) end) |> String.strip
179
end
1810

1911
def format(str) do
20-
capture_io(fn -> IO.ANSI.Docs.print(str, @colors) end) |> String.strip
12+
capture_io(fn -> IO.ANSI.Docs.print(str, []) end) |> String.strip
2113
end
2214

2315
test "heading is formatted" do

lib/ex_unit/mix.exs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ defmodule ExUnit.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[app: :ex_unit, version: System.version, build_per_environment: false]
5+
[app: :ex_unit,
6+
version: System.version,
7+
build_per_environment: false]
68
end
79

810
def application do
9-
[ registered: [ExUnit.Server],
10-
mod: {ExUnit, []},
11-
env: [
12-
autorun: true,
13-
trace: false,
14-
formatters: [ExUnit.CLIFormatter],
15-
include: [],
16-
exclude: [] ] ]
11+
[registered: [ExUnit.Server],
12+
mod: {ExUnit, []},
13+
env: [
14+
autorun: true,
15+
trace: false,
16+
formatters: [ExUnit.CLIFormatter],
17+
include: [],
18+
exclude: []]]
1719
end
1820
end

lib/iex/mix.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ defmodule IEx.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[app: :iex, version: System.version, build_per_environment: false]
5+
[app: :iex,
6+
version: System.version,
7+
build_per_environment: false]
68
end
79

810
def application do

lib/mix/lib/mix/project.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ defmodule Mix.Project do
138138
def config_files do
139139
[Mix.Dep.Lock.manifest] ++
140140
case Mix.ProjectStack.peek do
141-
{name, config, file} ->
141+
{_name, config, file} ->
142142
configs = config[:config_path] || "config/config.exs"
143143
|> Path.dirname
144144
|> Path.join("*.exs")

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,44 @@ defmodule Mix.Tasks.Help do
4141

4242
def run([task]) do
4343
module = Mix.Task.get!(task)
44-
shell = Mix.shell
45-
shell.info "%{bright}# mix help #{task}\n"
44+
doc = Mix.Task.moduledoc(module) || "There is no documentation for this task"
4645

47-
if doc = Mix.Task.moduledoc(module) do
48-
shell.info doc
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)
4950
else
50-
shell.info "There is no documentation for this task"
51+
IO.puts "# mix help #{task}\n"
52+
IO.puts doc
5153
end
5254

53-
shell.info "Location: #{where_is_file(module)}"
55+
IO.puts "Location: #{where_is_file(module)}"
5456
end
5557

5658
def run(_) do
5759
raise Mix.Error, message: "Unexpected arguments, expected `mix help` or `mix help TASK`"
5860
end
5961

62+
defp width() do
63+
case :io.columns(:standard_input) do
64+
{:ok, width} -> min(width, 80)
65+
{:error, _} -> 80
66+
end
67+
end
68+
6069
defp format_task(task, max, doc) do
6170
String.ljust(task, max) <> " # " <> doc
6271
end
6372

6473
defp where_is_file(module) do
6574
case :code.where_is_file(atom_to_list(module) ++ '.beam') do
66-
:non_existing -> "not available"
67-
location -> Path.expand(Path.dirname(location))
75+
:non_existing ->
76+
"not available"
77+
location ->
78+
location
79+
|> Path.dirname
80+
|> Path.expand
81+
|> Path.relative_to_cwd
6882
end
6983
end
7084

lib/mix/mix.exs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ defmodule Mix.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[ app: :mix,
6-
build_per_environment: false,
7-
version: System.version,
8-
escript_main_module: Mix.CLI ]
5+
[app: :mix,
6+
build_per_environment: false,
7+
version: System.version,
8+
escript_main_module: Mix.CLI]
99
end
1010

1111
def application do
12-
[ registered: [Mix.TasksServer, Mix.ProjectStack],
13-
mod: {Mix, []},
14-
env: [shell: Mix.Shell.IO,
15-
env: :dev,
16-
scm: [Mix.SCM.Git, Mix.SCM.Path]] ]
12+
[registered: [Mix.TasksServer, Mix.ProjectStack],
13+
mod: {Mix, []},
14+
env: [shell: Mix.Shell.IO,
15+
env: :dev,
16+
scm: [Mix.SCM.Git, Mix.SCM.Path],
17+
colors: []]]
1718
end
1819
end

lib/mix/test/mix/tasks/help_test.exs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Code.require_file "../../test_helper.exs", __DIR__
33
defmodule Mix.Tasks.HelpTest do
44
use MixTest.Case
55

6+
import ExUnit.CaptureIO
7+
68
test "help lists all tasks" do
79
in_fixture "no_mixfile", fn ->
810
Mix.Tasks.Help.run []
@@ -24,18 +26,13 @@ defmodule Mix.Tasks.HelpTest do
2426

2527
test "help TASK" do
2628
in_fixture "no_mixfile", fn ->
27-
Mix.Tasks.Help.run ["compile"]
29+
output =
30+
capture_io fn ->
31+
Mix.Tasks.Help.run ["compile"]
32+
end
2833

29-
{_, _, [output]} =
30-
assert_received {:mix_shell, :info, [_]}
3134
assert output =~ "# mix help compile"
32-
33-
{_, _, [output]} =
34-
assert_received {:mix_shell, :info, [_]}
3535
assert output =~ "## Command line options"
36-
37-
{_, _, [output]} =
38-
assert_received {:mix_shell, :info, [_]}
3936
assert output =~ ~r/^Location:/m
4037
end
4138
end

0 commit comments

Comments
 (0)