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