[erlang-questions] Pretty print list of strings in tabular way
Frank Muller
frank.muller.erl@REDACTED
Tue Oct 24 16:05:07 CEST 2017
Awesome, thanks guys for feedbacks/code
/Frank
> I was bored, and thought it was nice problem, below is some code if you
> still need a solution.
>
> /Dan
>
> On Mon, Oct 23, 2017 at 10:23 AM Frank Muller <frank.muller.erl@REDACTED>
> wrote:
>
>> Hey guys,
>>
>> Is there any library which helps pretty print list of “list of strings”
>> into a nice table?
>>
>> I’ve found this module prettyptr shipped with the VM. But I couldn’t find
>> any example on how to use it.
>>
>> Any idea is very welcome.
>>
>> Thanks
>> /Frank
>>
> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> -compile([export_all, nowarn_export_all]).
>
> def_opts() ->
> #{pad=>$\s, dir=>trailing, c_sep=>" | ", r_sep => "-"}.
>
> table([Map|_] = LofMaps0) when is_map(Map) ->
> table(lists:sort(maps:keys(Map)), LofMaps0, def_opts()).
>
> table(Keys, Rows) ->
> table(Keys, Rows, def_opts()).
>
> table(Keys0, Rows0, Opts) ->
> KeyStrs = [element(2, to_string(Key)) || Key <- Keys0],
> Rows = [to_strings(Keys0, V) || V <- Rows0],
> Ws = ws(Rows, [{undefined, string:length(Key)} || Key <- KeyStrs]),
> Col = fun({_,Str}, {Type, Width}) ->
> Dir = case Type of
> number -> leading;
> _ -> maps:get(dir, Opts, trailing)
> end,
> string:pad(Str, Width, Dir)
> end,
> Row = fun(Row) ->
> R0 = [Col(Str, W) || {Str,W} <- lists:zip(Row, Ws)],
> lists:join(maps:get(c_sep, Opts, " | "), R0)
> end,
> Header0 = [string:pad(Str, W, both) ||
> {Str,{_,W}} <- lists:zip(KeyStrs, Ws)],
> Header = lists:join(maps:get(c_sep, Opts, " | "), Header0),
> Delim = lists:duplicate(string:length(Header), maps:get(r_sep, Opts,
> "-")),
> [Header, $\n, Delim, $\n,
> lists:join($\n, [Row(R) || R <- Rows]), $\n,
> Delim, $\n,$\n].
>
> ws([Row|Rs], Ws) ->
> ws(Rs, ws_1(Row, Ws));
> ws([], Ws) -> Ws.
>
> ws_1([{T0,V}|Vs], [{Type,Max}|Ms]) ->
> [{type(T0,Type),max(string:length(V),Max)}|ws_1(Vs,Ms)];
> ws_1([], []) -> [].
>
> type(T, undefined) -> T;
> type(T, T) -> T;
> type(_, _) -> string.
>
> to_strings(Keys, Map) when is_map(Map) ->
> [to_string(maps:get(Key, Map, undefined)) || Key <- Keys];
> to_strings(_Keys, List) when is_list(List) ->
> [to_string(Entry) || Entry <- List];
> to_strings(_Keys, Tuple) when is_tuple(Tuple) ->
> [to_string(Entry) || Entry <- tuple_to_list(Tuple)].
>
> to_string(Int) when is_integer(Int) -> {number, integer_to_list(Int)};
> to_string(Float) when is_float(Float) -> {number,
> io_lib:format("~.4f",[Float])};
> to_string(Str) when is_list(Str) -> {string, Str};
> to_string(Bin) when is_binary(Bin) -> {string, Bin};
> to_string(T) -> {string, io_lib:format("~tp", [T])}.
>
> test() ->
> Maps = [#{k=>K, v=> "string " ++ integer_to_list(K),
> pi=>math:pi()*K, xfoo=>{term, "another"}} ||
> K <- lists:seq(8,12) ++ lists:seq(95, 110)],
> MapTab = a:table(Maps),
> io:put_chars(MapTab),
>
> Tuples = [{K, "string:" ++ integer_to_list(K), math:pi()*K, "another"}
> ||
> K <- lists:seq(9,13) ++ lists:seq(90, 100)],
> TupleTab = a:table([key,val,pi, str], Tuples),
> io:put_chars(TupleTab),
>
> Lists = [{K, "string " ++ integer_to_list(K), math:pi()*K, {"another",
> K*K}} ||
> K <- lists:seq(9,13) ++ lists:seq(995, 1004)],
> Opts = #{pad=>$\s, dir=>both, c_sep=>" ", r_sep => " "},
> ListsTab = a:table([key,val,"pi multiplied", term], Lists, Opts),
> io:put_chars(ListsTab),
> ok.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171024/d3b0888d/attachment.htm>
More information about the erlang-questions
mailing list