Skip to content

Commit b7bbe13

Browse files
author
José Valim
committed
Remove non ansi handling from IO.ANSI.Docs
1 parent 4cbe174 commit b7bbe13

File tree

4 files changed

+52
-68
lines changed

4 files changed

+52
-68
lines changed

lib/elixir/lib/float.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Float do
3333
parse_unsign(binary)
3434
end
3535

36-
defp parse_unsign("-" <> binary), do: :error
36+
defp parse_unsign("-" <> _), do: :error
3737
defp parse_unsign(binary) when is_binary(binary) do
3838
case Integer.parse binary do
3939
:error -> :error

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

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,12 @@ defmodule IO.ANSI.Docs do
1111
doc_bold: "bright",
1212
doc_underline: "underline" ]
1313

14-
@shared_print_doc """
15-
In addition to the priting string, takes a truth value for whether to use ANSI
16-
escape codes, and a keyword list for the printing color settings. Supported
17-
keys for the color settings are:
18-
19-
* `:enabled` - toggles coloring on and off (true)
20-
* `:doc_code` - code blocks (cyan, bright)
21-
* `:doc_inline_code` - inline code (cyan)
22-
* `:doc_headings` - h1 and h2 headings (yellow, bright)
23-
* `:doc_title` - top level heading (reverse, yellow, bright)
24-
* `:doc_bold` - bold text (bright)
25-
* `:doc_underline` - underlined text (underline)
26-
27-
Values for the color settings are strings with comma-separated attributes.
28-
Supported attributes are:
29-
30-
* Colors: `black red green yellow blue magenta cyan white`
31-
* Intensity: `normal bright`
32-
* Decoration: `underline reverse`
33-
"""
34-
3514
@doc """
3615
Prints the head of the documentation (i.e. the function signature).
3716
38-
#{@shared_print_doc}
17+
See `print/3` for docs on the supported options.
3918
"""
40-
def print_heading(string, use_ansi // IO.ANSI.terminal?, colors // @default_colors) do
41-
if use_ansi do
42-
write_doc_heading(string, colors)
43-
else
44-
IO.puts "* #{string}\n"
45-
end
46-
dont_display_result
47-
end
48-
49-
defp write_doc_heading(heading, colors) do
19+
def print_heading(heading, colors // @default_colors) do
5020
IO.puts IO.ANSI.reset
5121
width = column_width()
5222
padding = div(width + String.length(heading), 2)
@@ -57,22 +27,32 @@ defmodule IO.ANSI.Docs do
5727
@doc """
5828
Prints the documentation body.
5929
60-
#{@shared_print_doc}
30+
In addition to the priting string, takes a truth value for whether to use ANSI
31+
escape codes, and a keyword list for the printing color settings. Supported
32+
keys for the color settings are:
33+
34+
* `:enabled` - toggles coloring on and off (true)
35+
* `:doc_code` - code blocks (cyan, bright)
36+
* `:doc_inline_code` - inline code (cyan)
37+
* `:doc_headings` - h1 and h2 headings (yellow, bright)
38+
* `:doc_title` - top level heading (reverse, yellow, bright)
39+
* `:doc_bold` - bold text (bright)
40+
* `:doc_underline` - underlined text (underline)
41+
42+
Values for the color settings are strings with comma-separated attributes.
43+
Supported attributes are:
44+
45+
* Colors: `black red green yellow blue magenta cyan white`
46+
* Intensity: `normal bright`
47+
* Decoration: `underline reverse`
6148
"""
62-
def print(doc, use_ansi // IO.ANSI.terminal?, colors // @default_colors) do
63-
if use_ansi do
64-
doc
65-
|> String.split(["\r\n","\n"], trim: false)
66-
|> Enum.map(&String.rstrip/1)
67-
|> process("", colors)
68-
else
69-
IO.puts doc
70-
end
71-
dont_display_result
49+
def print(doc, colors // @default_colors) do
50+
doc
51+
|> String.split(["\r\n","\n"], trim: false)
52+
|> Enum.map(&String.rstrip/1)
53+
|> process("", colors)
7254
end
7355

74-
defp dont_display_result, do: :"do not show this result in output"
75-
7656
defp process([], _indent, _colors), do: nil
7757

7858
defp process(["# " <> heading | rest], _indent, colors) do

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,16 @@ defmodule IO.ANSI.DocsTest do
1212
doc_headings: "yellow,bright",
1313
doc_title: "reverse,yellow,bright" ]
1414

15-
def format_heading(str, use_ansi // true) do
16-
capture_io(fn -> IO.ANSI.Docs.print_heading(str, use_ansi, @colors) end)
17-
|> String.strip
15+
def format_heading(str) do
16+
capture_io(fn -> IO.ANSI.Docs.print_heading(str, @colors) end) |> String.strip
1817
end
1918

20-
def format(str, use_ansi // true) do
21-
capture_io(fn -> IO.ANSI.Docs.print(str, use_ansi, @colors) end)
22-
|> String.strip
19+
def format(str) do
20+
capture_io(fn -> IO.ANSI.Docs.print(str, @colors) end) |> String.strip
2321
end
2422

25-
test "non-ansi heading just uses an asterisk" do
26-
result = format_heading("wibble", false)
27-
assert result == "* wibble"
28-
end
29-
30-
test "ansi heading is formatted" do
31-
result = format_heading("wibble", true)
23+
test "heading is formatted" do
24+
result = format_heading("wibble")
3225
assert String.starts_with?(result, "\e[0m\n\e[7m\e[33m\e[1m")
3326
assert String.ends_with?(result, "\e[0m\n\e[0m")
3427
assert String.contains?(result, " wibble ")

lib/iex/lib/iex/introspection.ex

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ defmodule IEx.Introspection do
1212
if function_exported?(module, :__info__, 1) do
1313
case module.__info__(:moduledoc) do
1414
{ _, binary } when is_binary(binary) ->
15-
use_ansi = IO.ANSI.terminal?
16-
colors = IEx.Options.get(:colors)
17-
IO.ANSI.Docs.print_heading(inspect(module), use_ansi, colors)
18-
IO.ANSI.Docs.print(binary, use_ansi, colors)
15+
if IO.ANSI.terminal? do
16+
colors = IEx.Options.get(:colors)
17+
IO.ANSI.Docs.print_heading(inspect(module), colors)
18+
IO.ANSI.Docs.print(binary, colors)
19+
else
20+
IO.puts "* #{inspect(module)}\n"
21+
IO.puts binary
22+
end
1923
{ _, _ } ->
2024
nodocs(inspect module)
2125
_ ->
@@ -159,11 +163,18 @@ defmodule IEx.Introspection do
159163
end
160164

161165
defp print_doc({ { fun, _ }, _line, kind, args, doc }) do
162-
args = Enum.map_join(args, ", ", &print_doc_arg(&1))
163-
use_ansi = IO.ANSI.terminal?
164-
colors = IEx.Options.get(:colors)
165-
IO.ANSI.Docs.print_heading("#{kind} #{fun}(#{args})", use_ansi, colors)
166-
if doc, do: IO.ANSI.Docs.print(doc, use_ansi, colors)
166+
args = Enum.map_join(args, ", ", &print_doc_arg(&1))
167+
heading = "#{kind} #{fun}(#{args})"
168+
doc = doc || ""
169+
170+
if IO.ANSI.terminal? do
171+
colors = IEx.Options.get(:colors)
172+
IO.ANSI.Docs.print_heading(heading, colors)
173+
IO.ANSI.Docs.print(doc, colors)
174+
else
175+
IO.puts "* #{heading}\n"
176+
IO.puts doc
177+
end
167178
end
168179

169180
defp print_doc_arg({ ://, _, [left, right] }) do

0 commit comments

Comments
 (0)