diff --git a/lib/formatter.ex b/lib/formatter.ex index 734e932..e317388 100644 --- a/lib/formatter.ex +++ b/lib/formatter.ex @@ -37,8 +37,13 @@ defmodule Scribe.Formatter.Line do cell_value = case opts[:colorize] do - false -> value |> cell(width) - _ -> value |> cell(width) |> colorize(style.color(value)) + false -> + value |> cell(width, opts[:alignment]) + + _ -> + value + |> cell(width, opts[:alignment]) + |> colorize(style.color(value)) end acc <> cell_value <> b.right_edge @@ -47,10 +52,25 @@ defmodule Scribe.Formatter.Line do left_edge <> line <> "\n" end - def cell(x, width) do + def cell(x, width, alignment \\ :left) do len = min(String.length(" #{inspect(x)} "), width) - padding = String.duplicate(" ", width - len) - truncate(" #{inspect(x)}#{padding}", width - 2) <> " " + + case alignment do + :center -> + padding = String.duplicate(" ", div(width - len, 2)) + remaining = String.duplicate(" ", rem(width - len, 2)) + + truncate(" #{padding}#{inspect(x)}#{padding}#{remaining}", width - 2) <> + " " + + :right -> + padding = String.duplicate(" ", width - len) + truncate(" #{padding}#{inspect(x)}", width - 2) <> " " + + _ -> + padding = String.duplicate(" ", width - len) + truncate(" #{inspect(x)}#{padding}", width - 2) <> " " + end end def cell_value(x, padding, max_width) when padding >= 0 do diff --git a/lib/scribe.ex b/lib/scribe.ex index 10fd027..016624a 100644 --- a/lib/scribe.ex +++ b/lib/scribe.ex @@ -13,6 +13,7 @@ defmodule Scribe do @typedoc ~S""" Options for configuring table output. + - `:alignment` - Define text alignment in cells. Defaults to `:left`. - `:colorize` - When `false`, disables colored output. Defaults to `true` - `:data` - Defines table headers - `:device` - Where to print (defaults to STDOUT) @@ -20,6 +21,7 @@ defmodule Scribe do - `:width` - Defines table width. Defaults to `:infinite` """ @type format_opts :: [ + alignment: atom, colorize: boolean, data: [...], style: module,