Current section
Files
Jump to
Current section
Files
src/gtabler.erl
-module(gtabler).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([print_table/3]).
-export_type([table_config/0]).
-type table_config() :: {table_config,
binary(),
binary(),
fun((binary()) -> binary()),
fun((binary()) -> binary())}.
-file("C:\\Users\\erick\\Documents\\projects\\gtabler\\src\\gtabler.gleam", 34).
-spec calculate_column_widths(list(list(binary()))) -> list(integer()).
calculate_column_widths(Rows) ->
Rows_width = gleam@list:map(
Rows,
fun(Row) -> gleam@list:map(Row, fun gleam@string:length/1) end
),
gleam@list:fold(Rows_width, [], fun(Acc, Row@1) -> case {Acc, Row@1} of
{[], []} ->
gleam@list:repeat(0, erlang:length(Row@1));
{[], Row@2} ->
Row@2;
{Row@3, Acc@1} ->
_pipe = gleam@list:zip(Row@3, Acc@1),
gleam@list:map(
_pipe,
fun(X) ->
gleam@int:max(
erlang:element(1, X),
erlang:element(2, X)
)
end
)
end end).
-file("C:\\Users\\erick\\Documents\\projects\\gtabler\\src\\gtabler.gleam", 58).
-spec format_separator(list(integer()), binary(), binary()) -> binary().
format_separator(Widths, Border_char, Separator) ->
_pipe = Widths,
_pipe@2 = gleam@list:map(
_pipe,
fun(Width) -> _pipe@1 = gleam@list:repeat(Border_char, Width),
gleam@string:join(_pipe@1, <<""/utf8>>) end
),
gleam@string:join(_pipe@2, Separator).
-file("C:\\Users\\erick\\Documents\\projects\\gtabler\\src\\gtabler.gleam", 68).
-spec pad_column(binary(), integer()) -> binary().
pad_column(Value, Width) ->
<<Value/binary,
(begin
_pipe = gleam@list:repeat(
<<" "/utf8>>,
Width - gleam@string:length(Value)
),
gleam@string:join(_pipe, <<""/utf8>>)
end)/binary>>.
-file("C:\\Users\\erick\\Documents\\projects\\gtabler\\src\\gtabler.gleam", 47).
-spec format_row(
list(binary()),
list(integer()),
fun((binary()) -> binary()),
binary()
) -> binary().
format_row(Row, Widths, Color, Separator) ->
_pipe = gleam@list:zip(Row, Widths),
_pipe@1 = gleam@list:map(
_pipe,
fun(X) ->
Color(pad_column(erlang:element(1, X), erlang:element(2, X)))
end
),
gleam@string:join(_pipe@1, Separator).
-file("C:\\Users\\erick\\Documents\\projects\\gtabler\\src\\gtabler.gleam", 18).
-spec print_table(table_config(), list(binary()), list(list(binary()))) -> binary().
print_table(C, H, R) ->
Column_widths = calculate_column_widths([H | R]),
Header_row = format_row(
H,
Column_widths,
erlang:element(4, C),
erlang:element(2, C)
),
Separator = format_separator(
Column_widths,
erlang:element(3, C),
erlang:element(2, C)
),
Data_rows = begin
_pipe = R,
gleam@list:map(
_pipe,
fun(Row) ->
format_row(
Row,
Column_widths,
erlang:element(5, C),
erlang:element(2, C)
)
end
)
end,
<<<<<<<<Header_row/binary, "\n"/utf8>>/binary, Separator/binary>>/binary,
"\n"/utf8>>/binary,
(gleam@string:join(Data_rows, <<"\n"/utf8>>))/binary>>.