Skip to content

Commit

Permalink
Merge pull request #6 from zoldar/unified-api
Browse files Browse the repository at this point in the history
Unify public API - render_table and print_table
  • Loading branch information
aerosol committed Nov 3, 2015
2 parents 9336908 + d8f7f31 commit ca3f8b7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 51 deletions.
60 changes: 38 additions & 22 deletions lib/tabula.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,43 @@ defmodule Tabula do
end
def print_table(rows, override_opts) do
unquote(__MODULE__).print_table(
rows, Keyword.merge(unquote(opts), override_opts)
) end
rows, Keyword.merge(unquote(opts), override_opts))
end
def render_table(rows) do
unquote(__MODULE__).render_table(rows, unquote(opts))
end
def render_table(rows, override_opts) do
unquote(__MODULE__).render_table(
rows, Keyword.merge(unquote(opts), override_opts))
end
end
end

def print_table([first|_]=rows, opts \\ []) do
cols = case opts[:only] do
nil ->
first |> Map.keys
cols when is_list(cols) ->
cols
end
render_table(rows, cols, opts)
def print_table(rows, opts \\ []) do
render_table(rows, opts)
|> IO.puts
end

def render_table(rows, [_|_]=cols, opts) do
def render_table(rows, opts \\ []) do
cols = extract_cols(rows, opts)
_render_table(rows, cols, opts)
|> :erlang.list_to_binary
end

def max_widths(cols, rows) do
max_index = rows
|> length
|> strlen
cols
|> map(fn k ->
max([
strlen(k), max_index
| map(rows, &(Map.get(&1, k) |> strlen))
])
end)
end

defp _render_table(rows, [_|_]=cols, opts) do
widths = max_widths(cols, rows)
formatters = widths |> formatters(opts)
spacers = widths |> spacers(opts)
Expand All @@ -68,17 +88,13 @@ defmodule Tabula do

end

def max_widths(cols, rows) do
max_index = rows
|> length
|> strlen
cols
|> map(fn k ->
max([
strlen(k), max_index
| map(rows, &(Map.get(&1, k) |> strlen))
])
end)
defp extract_cols([first|_]=_rows, opts) do
case opts[:only] do
nil ->
first |> Map.keys
cols when is_list(cols) ->
cols
end
end

defp render_row(cells, style_element, formatters, opts) do
Expand Down
58 changes: 29 additions & 29 deletions test/tabula_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,56 @@ defmodule TabulaTest do
end

test "Columns can be provided by the user or discovered automatically" do
auto = fn ->
Tabula.print_table @rows
end
man = fn ->
Tabula.print_table @rows, only: Enum.sort(@cols)
end
assert capture_io(auto) == capture_io(man)
auto = Tabula.render_table @rows
man = Tabula.render_table @rows, only: Enum.sort(@cols)
assert auto == man
end

test "Special column '#' can be provided to enumerate rows" do
table = fn ->
Tabula.print_table(@rows, only: ["#"|@cols])
end
table = Tabula.render_table(@rows, only: ["#"|@cols])
expect = """
# | name | age | city
----+---------+-----+---------
1 | Adam | 32 | Warsaw
2 | Yolanda | 28 | New York
"""
assert capture_io(table) == expect
assert table == expect
end

test "Github Markdown style can be applied" do
test "Print function outputs valid table to stdout" do
table = fn ->
Tabula.print_table(@rows, only: Enum.sort(@cols), style: :github_md)
Tabula.print_table @rows
end
expect = """
age | city | name
--- | -------- | -------
----+----------+--------
32 | Warsaw | Adam
28 | New York | Yolanda
"""
assert capture_io(table) == expect
end

test "Github Markdown style can be applied" do
table = Tabula.render_table(@rows, only: Enum.sort(@cols), style: :github_md)
expect = """
age | city | name
--- | -------- | -------
32 | Warsaw | Adam
28 | New York | Yolanda
"""
assert table == expect
end

test "Columns not found are represented by `nil`" do
table = fn ->
Tabula.print_table(@rows, only: ["phone", "email"])
end
table = Tabula.render_table(@rows, only: ["phone", "email"])
expect = """
phone | email
------+------
nil | nil
nil | nil
"""
assert capture_io(table) == expect
assert table == expect
end

test "Crash when there are no columns provided" do
Expand All @@ -74,6 +75,8 @@ defmodule TabulaTest do
end
assert Foo.__info__(:functions) |> Enum.member?({:print_table, 1})
assert Foo.__info__(:functions) |> Enum.member?({:print_table, 2})
assert Foo.__info__(:functions) |> Enum.member?({:render_table, 1})
assert Foo.__info__(:functions) |> Enum.member?({:render_table, 2})
end

test "Options will be merged" do
Expand All @@ -83,40 +86,37 @@ defmodule TabulaTest do
@data [%{"hello" => "world", "cruel" => true}]

def t1 do
@data |> print_table
@data |> render_table
end
def t2 do
@data |> print_table(only: ["cruel"])
@data |> render_table(only: ["cruel"])
end
def t3 do
@data |> print_table(only: ["#", "cruel"], style: :org_mode)
@data |> render_table(only: ["#", "cruel"], style: :org_mode)
end
end

e1 = """
cruel | hello
----- | -----
true | world
"""

e2 = """
cruel
-----
true
"""

e3 = """
# | cruel
----+------
1 | true
"""

assert capture_io(&Bar.t1/0) == e1
assert capture_io(&Bar.t2/0) == e2
assert capture_io(&Bar.t3/0) == e3
assert Bar.t1 == e1
assert Bar.t2 == e2
assert Bar.t3 == e3
end

test "Long auto-indices will be aligned properly" do
Expand Down

0 comments on commit ca3f8b7

Please sign in to comment.