Skip to content

Commit 7b0cbb1

Browse files
author
José Valim
committed
Speed up and calculate effective length after rendering
1 parent 3dc0e87 commit 7b0cbb1

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ defmodule IO.ANSI.Docs do
241241
end
242242

243243
defp table_lines(lines, options) do
244-
lines = Enum.map(lines, &split_into_columns/1)
245-
count = lines |> Enum.map(&length/1) |> Enum.max
244+
lines = Enum.map(lines, &split_into_columns(&1, options))
245+
count = Enum.map(lines, &length/1) |> Enum.max
246246
lines = Enum.map(lines, &pad_to_number_of_columns(&1, count))
247247

248248
widths = for line <- lines, do:
249-
(for col <- line, do: effective_length(col))
249+
(for {_col, length} <- line, do: length)
250250

251251
col_widths = Enum.reduce(widths,
252252
List.duplicate(0, count),
@@ -255,24 +255,33 @@ defmodule IO.ANSI.Docs do
255255
render_table(lines, col_widths, options)
256256
end
257257

258-
defp split_into_columns(line) do
258+
defp split_into_columns(line, options) do
259259
line
260260
|> String.strip(?|)
261261
|> String.strip()
262262
|> String.split(~r/\s\|\s/)
263+
|> Enum.map(&render_column(&1, options))
263264
end
264265

265-
defp pad_to_number_of_columns(cols, col_count),
266-
do: cols ++ List.duplicate("", col_count - length(cols))
266+
defp render_column(col, options) do
267+
col = col
268+
|> String.replace(~r/\\ \|/x, "|")
269+
|> handle_links
270+
|> handle_inline(nil, [], [], options)
267271

268-
defp max_column_widths(cols, widths) do
269-
Enum.zip(cols, widths) |> Enum.map(fn {a,b} -> max(a,b) end)
270-
end
272+
len = col
273+
|> String.replace(~r/\e\[\d+m/, "")
274+
|> String.length
271275

272-
defp effective_length(text) do
273-
String.length(Regex.replace(~r/((^|\b)[`*_]+)|([`*_]+\b)/, text, ""))
276+
{col, len}
274277
end
275278

279+
defp pad_to_number_of_columns(cols, col_count),
280+
do: cols ++ List.duplicate("", col_count - length(cols))
281+
282+
defp max_column_widths(cols, widths),
283+
do: Enum.zip(cols, widths) |> Enum.map(fn {a,b} -> max(a,b) end)
284+
276285
# If second line is heading separator, use the heading style on the first
277286
defp render_table([first, second | rest], widths, options) do
278287
combined = Enum.zip(first, widths)
@@ -291,20 +300,17 @@ defmodule IO.ANSI.Docs do
291300
render_table(rest, widths, options)
292301
end
293302

294-
defp render_table([], _, _), do: nil
303+
defp render_table([], _, _),
304+
do: nil
295305

296306
defp table_header?(row), do:
297-
Enum.all?(row, fn col -> col =~ ~r/^:?-+:?$/ end)
307+
Enum.all?(row, fn {col, _} -> col =~ ~r/^:?-+:?$/ end)
298308

299309
defp draw_table_row(cols_and_widths, options, heading \\ false) do
300-
columns = for { col, width } <- cols_and_widths do
301-
padding = width - effective_length(col)
302-
col = Regex.replace(~r/\\ \|/x, col, "|") # escaped bars
303-
text = col
304-
|> handle_links
305-
|> handle_inline(nil, [], [], options)
306-
text <> String.duplicate(" ", padding)
307-
end |> Enum.join(" | ")
310+
columns =
311+
Enum.map_join(cols_and_widths, " | ", fn {{col, length}, width} ->
312+
col <> String.duplicate(" ", width - length)
313+
end)
308314

309315
if heading do
310316
write(:doc_table_heading, columns, options)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,4 @@ defmodule IO.ANSI.DocsTest do
241241
result = format("`a` | _b_\nc | d")
242242
assert result == "\e[36ma\e[0m | \e[4mb\e[0m\nc | d\n\e[0m"
243243
end
244-
245244
end

0 commit comments

Comments
 (0)