Align columns: Difference between revisions
Content deleted Content added
→{{header|Elixir}}: String.ljust -> pad_trailing |
|||
Line 1,592: | Line 1,592: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
{{works with|Elixir|1.3}} |
|||
The String module of Elixir doesn't have the function of the center position adjusting. |
The String module of Elixir doesn't have the function of the center position adjusting. |
||
It calls and processes the function of 'Erlang'. |
It calls and processes the function of 'Erlang'. |
||
Line 1,597: | Line 1,598: | ||
def columns(text, alignment) do |
def columns(text, alignment) do |
||
fieldsbyrow = String.split(text, "\n", trim: true) |
fieldsbyrow = String.split(text, "\n", trim: true) |
||
|> Enum.map(fn |
|> Enum.map(fn row -> String.split(row, "$", trim: true) end) |
||
maxfields = Enum.map(fieldsbyrow, fn field -> length(field) end) |> Enum.max |
maxfields = Enum.map(fieldsbyrow, fn field -> length(field) end) |> Enum.max |
||
colwidths = Enum.map(fieldsbyrow, fn field -> field ++ List.duplicate("", maxfields - length(field)) end) |
colwidths = Enum.map(fieldsbyrow, fn field -> field ++ List.duplicate("", maxfields - length(field)) end) |
||
Line 1,611: | Line 1,612: | ||
end |
end |
||
defp adjust(field, width, :Left), do: String. |
defp adjust(field, width, :Left), do: String.pad_trailing(field, width) |
||
defp adjust(field, width, :Right), do: String. |
defp adjust(field, width, :Right), do: String.pad_leading(field, width) |
||
defp adjust(field, width, _), do: :string.centre(String. |
defp adjust(field, width, _), do: :string.centre(String.to_charlist(field), width) |
||
end |
end |
||