Pretty print list of strings in tabular way

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Pretty print list of strings in tabular way

Frank Muller
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

bengt
Greetings,

Where did you find a module called prettyptr? Searching for it I found nothing.
I have used the module erl_prettypr for code, but I do not think that is what you want.
Would you mind explaining slightly more about what you are trying to achieve?

bengt

On Mon, Oct 23, 2017 at 10:23 AM, Frank Muller <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Frank Muller
erlang/19.1/lib/syntax_tools-2.1/src/prettypr.erl



Greetings,

Where did you find a module called prettyptr? Searching for it I found nothing.
I have used the module erl_prettypr for code, but I do not think that is what you want.
Would you mind explaining slightly more about what you are trying to achieve?

bengt

On Mon, Oct 23, 2017 at 10:23 AM, Frank Muller <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Frank Muller
What I would like to achieve is something similar to Scribe but in pure Erlang:


/Frank


erlang/19.1/lib/syntax_tools-2.1/src/prettypr.erl



Greetings,

Where did you find a module called prettyptr? Searching for it I found nothing.
I have used the module erl_prettypr for code, but I do not think that is what you want.
Would you mind explaining slightly more about what you are trying to achieve?

bengt

On Mon, Oct 23, 2017 at 10:23 AM, Frank Muller <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Richard Carlsson-3
The erl_prettypr.erl module uses the prettypr.erl module to do the formatting, so you can look at that as an example.

Generating nice-looking tables is however not something that fits well with how prettypr.erl works. It wants to be able to make local choices between vertical or horizontal alignment, and a table requires that the lines separating cells are aligned both horizontally and vertically across the whole table. Maybe it would be straightforward to add such grid alignment as an extra capability to prettypr.erl though. Otherwise, you could use it to format individual pieces of text to fit into the cells of the table once you have chosen the cell sizes.

        /Richard

2017-10-23 10:42 GMT+02:00 Frank Muller <[hidden email]>:
What I would like to achieve is something similar to Scribe but in pure Erlang:


/Frank


erlang/19.1/lib/syntax_tools-2.1/src/prettypr.erl



Greetings,

Where did you find a module called prettyptr? Searching for it I found nothing.
I have used the module erl_prettypr for code, but I do not think that is what you want.
Would you mind explaining slightly more about what you are trying to achieve?

bengt

On Mon, Oct 23, 2017 at 10:23 AM, Frank Muller <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Pierre Fenoll-2
How about using the elixir lib you linked?
@benoitc has a rebar3 provider that can compile elixir libs for you. 

On Mon 23 Oct 2017 at 11:09, Richard Carlsson <[hidden email]> wrote:
The erl_prettypr.erl module uses the prettypr.erl module to do the formatting, so you can look at that as an example.

Generating nice-looking tables is however not something that fits well with how prettypr.erl works. It wants to be able to make local choices between vertical or horizontal alignment, and a table requires that the lines separating cells are aligned both horizontally and vertically across the whole table. Maybe it would be straightforward to add such grid alignment as an extra capability to prettypr.erl though. Otherwise, you could use it to format individual pieces of text to fit into the cells of the table once you have chosen the cell sizes.

        /Richard

2017-10-23 10:42 GMT+02:00 Frank Muller <[hidden email]>:
What I would like to achieve is something similar to Scribe but in pure Erlang:


/Frank


erlang/19.1/lib/syntax_tools-2.1/src/prettypr.erl



Greetings,

Where did you find a module called prettyptr? Searching for it I found nothing.
I have used the module erl_prettypr for code, but I do not think that is what you want.
Would you mind explaining slightly more about what you are trying to achieve?

bengt

On Mon, Oct 23, 2017 at 10:23 AM, Frank Muller <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
--

Cheers,
-- 
Pierre Fenoll


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Adam Rutkowski-3
On Mon, Oct 23, 2017, at 12:11, Pierre Fenoll wrote:
How about using the elixir lib you linked?
@benoitc has a rebar3 provider that can compile elixir libs for you. 

That lib looks pretty comprehensive!

If the rebar3 plugin option is not entirely satisfying, I'd recommend looking at this [1] implementation in Clojure, for a very basic  and elegant approach that should be easily transferrable. I was looking at it when writing yet another Elixir version here [2] -- it's not as advanced as the library you linked though, but perhaps a good point to get started.




/A.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Richard A. O'Keefe-2
In reply to this post by Frank Muller
I would normally handle tabular output by delegating to
another program.  Two simple ones (in Python and AWK) are
described in http://www.cs.otago.ac.nz/staffpriv/ok/tale.pdf.

These days I'd probably write a <table> to a temporary file
and tell a web browser to open it.  Or possibly get RStudio
to read it as a data frame and view it in the data viewer.

Much depends on
  - the number of rows in the table
  - the number of columns in the table
  - the size of the fields (are they tolerably short, or
    are they paragraphs)
  - whether the fields contain tabs, line break separators,
    bidirectional text, or other odd stuff
  - whether the table is to be read by people or programs
  - whether the table is large enough that a human reader
    might need machine help to search it
  - or do things like highlighting the current row/column
  - or sort on selected columns
  ...
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty print list of strings in tabular way

Frank Muller
In reply to this post by Frank Muller
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 <[hidden email]> 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
[hidden email]
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.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions