diff options
author | Hans Bolinder <[email protected]> | 2018-04-17 14:55:35 +0200 |
---|---|---|
committer | Hans Bolinder <[email protected]> | 2018-04-25 12:30:06 +0200 |
commit | 513e6c069c31da33d435d16d811211eee7e16399 (patch) | |
tree | 569917dc0034701cd18b8dcab2795830b9a645a1 /lib/stdlib/src | |
parent | d3f0d1785e5847031f21484ca99c503c6ef704b8 (diff) | |
download | otp-513e6c069c31da33d435d16d811211eee7e16399.tar.gz otp-513e6c069c31da33d435d16d811211eee7e16399.tar.bz2 otp-513e6c069c31da33d435d16d811211eee7e16399.zip |
stdlib: Modify ~w/~W when number of characters is limited
A bug fix: limited maps end with "...", not "...=>...".
A modification: wW separate pairs with " => ", not "=>".
When the output is limited on number of characters, the term is
balanced by wW the same way as is done with pP (see commit bc38638).
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r-- | lib/stdlib/src/io_lib.erl | 22 | ||||
-rw-r--r-- | lib/stdlib/src/io_lib_pretty.erl | 7 |
2 files changed, 22 insertions, 7 deletions
diff --git a/lib/stdlib/src/io_lib.erl b/lib/stdlib/src/io_lib.erl index 17b6e9967c..7a520f17d9 100644 --- a/lib/stdlib/src/io_lib.erl +++ b/lib/stdlib/src/io_lib.erl @@ -65,6 +65,7 @@ -export([print/1,print/4,indentation/2]). -export([write/1,write/2,write/3,nl/0,format_prompt/1,format_prompt/2]). +-export([write_binary/2]). -export([write_atom/1,write_string/1,write_string/2,write_latin1_string/1, write_latin1_string/2, write_char/1, write_latin1_char/1]). @@ -259,7 +260,8 @@ add_modifier(_, C) -> -spec write(Term) -> chars() when Term :: term(). -write(Term) -> write(Term, -1). +write(Term) -> + write1(Term, -1, latin1). -spec write(term(), depth(), boolean()) -> chars(). @@ -281,9 +283,19 @@ write(Term, D, false) -> write(Term, Options) when is_list(Options) -> Depth = get_option(depth, Options, -1), Encoding = get_option(encoding, Options, epp:default_encoding()), - write1(Term, Depth, Encoding); + CharsLimit = get_option(chars_limit, Options, -1), + RecDefFun = no_fun, + case Depth of + 0 -> "..."; + _ when CharsLimit < 0 -> + write1(Term, Depth, Encoding); + _ when CharsLimit >= 0 -> + If = io_lib_pretty:intermediate + (Term, Depth, CharsLimit, RecDefFun, Encoding, _Str=false), + io_lib_pretty:write(If) + end; write(Term, Depth) -> - write1(Term, Depth, latin1). + write(Term, [{depth, Depth}, {encoding, latin1}]). write1(_Term, 0, _E) -> "..."; write1(Term, _D, _E) when is_integer(Term) -> integer_to_list(Term); @@ -339,14 +351,14 @@ write_ref(Ref) -> write_map(Map, D, E) when is_integer(D) -> [$#,${,write_map_body(maps:to_list(Map), D, E),$}]. -write_map_body(_, 0, _E) -> "..."; +write_map_body(_, 1, _E) -> "..."; write_map_body([], _, _E) -> []; write_map_body([{K,V}], D, E) -> write_map_assoc(K, V, D, E); write_map_body([{K,V}|KVs], D, E) -> [write_map_assoc(K, V, D, E),$, | write_map_body(KVs, D-1, E)]. write_map_assoc(K, V, D, E) -> - [write1(K, D - 1, E),"=>",write1(V, D-1, E)]. + [write1(K, D - 1, E)," => ",write1(V, D-1, E)]. write_binary(B, D) when is_integer(D) -> [$<,$<,write_binary_body(B, D),$>,$>]. diff --git a/lib/stdlib/src/io_lib_pretty.erl b/lib/stdlib/src/io_lib_pretty.erl index 785c602ac0..9b0000ded0 100644 --- a/lib/stdlib/src/io_lib_pretty.erl +++ b/lib/stdlib/src/io_lib_pretty.erl @@ -26,6 +26,9 @@ -export([print/1,print/2,print/3,print/4,print/5,print/6]). +%% To be used by io_lib only. +-export([intermediate/6, write/1]). + %%% %%% Exported functions %%% @@ -573,10 +576,10 @@ print_length(<<_/bitstring>> = Bin, D, T, RF, Enc, Str) -> end, {[$<,$<,S|"/utf8...>>"], 12 + length(S), 3, More}; false when byte_size(Bin) < D -> - S = io_lib:write(Bin, D), + S = io_lib:write_binary(Bin, D), {{bin, S}, iolist_size(S), 0, no_more}; false -> - S = io_lib:write(Bin, D), + S = io_lib:write_binary(Bin, D), More = fun(T1, Dd) -> ?FUNCTION_NAME(Bin, D+Dd, T1, RF, Enc, Str) end, |