Skip to content

Commit

Permalink
More efficient pretty printing on the rest of the line.
Browse files Browse the repository at this point in the history
  • Loading branch information
rvirding committed Aug 30, 2009
1 parent 350445f commit 4a44ac1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Binary file modified ebin/lfe_io_pretty.beam
Binary file not shown.
5 changes: 5 additions & 0 deletions src/ChangeLog
@@ -1,3 +1,8 @@
2009-08-30 Robert Virding <rv@stanislaw.local>

* lfe_io_pretty.erl (print1_list_max, print1_tail_max): Bit more
efficient pretty printing on the rest of the line.

2009-08-13 Robert Virding <rv@stanislaw.local>

* lfe_comp.erl (list_warnings, list_errors): Use lfe_io format.
Expand Down
1 change: 1 addition & 0 deletions src/lfe_io.erl
Expand Up @@ -30,6 +30,7 @@
%% The io functions have been split into the following modules:
%% lfe_io - basic read and write functions
%% lfe_io_pretty - sexpr prettyprinter
%% lfe_io_format - formatted output

-module(lfe_io).

Expand Down
49 changes: 34 additions & 15 deletions src/lfe_io_pretty.erl
Expand Up @@ -31,14 +31,14 @@

-export([print1/1,print1/2,print1/3,print1/4]).

%% -compile(export_all).
-compile(export_all).

-import(lists, [flatlength/1]).
-import(lists, [reverse/1,flatlength/1]).

%% print1(Sexpr) -> [char()].
%% print1(Sexpr, Depth) -> [char()].
%% print1(Sexpr, Depth, Indentation, LineLength) -> [char()].
%% An relatively simple pretty print function, but with some
%% A relatively simple pretty print function, but with some
%% customisation.

print1(S) -> print1(S, -1, 0, 80).
Expand All @@ -60,12 +60,11 @@ print1(['unquote-splicing',E], D, I, L) -> [",@",print1(E, D, I+2, L)];
print1([Car|Cdr]=List, D, I, L) ->
%% Handle printable lists specially.
case io_lib:printable_list(List) of
true -> lfe_io:print1_string(List, 34); %"
true -> lfe_io:print1_string(List, $"); %"
false ->
Print = lfe_io:print1(List, D), %Raw printing
case flatlength(Print) of
Len when Len + I < L -> Print;
_ ->
case print1_list_max(List, D, I+2, L) of
{yes,Print} -> ["(",Print,")"];
no ->
%% Customise printing of lists.
case indent_type(Car) of
none ->
Expand All @@ -85,12 +84,10 @@ print1([Car|Cdr]=List, D, I, L) ->
print1([], _, _, _) -> "()";
print1({}, _, _, _) -> "#()";
print1(Tup, D, I, L) when is_tuple(Tup) ->
Print = lfe_io:print1(Tup, D),
case flatlength(Print) of
Len when Len + I < L -> Print;
_ ->
Es = tuple_to_list(Tup),
["#(",print1_list(Es, D-1, I+2, L),")"]
Es = tuple_to_list(Tup),
case print1_list_max(Es, D, I+3, L) of
{yes,Print} -> ["#(",Print,")"];
no -> ["#(",print1_list(Es, D-1, I+2, L),")"]
end;
print1(Bit, _, _, _) when is_bitstring(Bit) ->
["#B(",lfe_io:print1_bits(Bit, 30),$)]; %First 30 bytes
Expand All @@ -107,6 +104,28 @@ split(N, [H|T]) ->
{H1,T1} = split(N-1, T),
{[H|H1],T1}.

%% print1_list_max(List, Depth, Indentation, LineLength) -> {yes,Chars} | no.
%% Maybe print a list on one line, but abort if it goes past
%% LineLength.

print1_list_max([Car|Cdr], D, I, L) ->
Cs = print1(Car, D, 0, 99999), %Never break the line
print1_tail_max(Cdr, D, I + flatlength(Cs), L, [Cs]);
print1_list_max([], _, _, _) -> {yes,[]}.

%% print1_tail_max(Tail, Depth, Indentation, LineLength) -> {yes,Chars} | no.
%% Maybe print the tail of a list on one line, but abort if it goes
%% past LineLength. We know about dotted pairs.

print1_tail_max(_, _, I, L, _) when I >= L -> no; %No more room
print1_tail_max([], _, _, _, Acc) -> {yes,reverse(Acc)};
print1_tail_max([Car|Cdr], D, I, L, Acc) ->
Cs = print1(Car, D, 0, 99999), %Never break the line
print1_tail_max(Cdr, D, I + flatlength(Cs) + 1, L, [Cs," "|Acc]);
print1_tail_max(S, D, I, L, Acc) ->
Cs = print1(S, D, 0, 99999), %Never break the line
print1_tail_max([], D, I + flatlength(Cs) + 3, L, [Cs," . "|Acc]).

%% print1_list(List, Depth, Indentation, LineLength)
%% Print a list, one element per line. No leading/trailing ().

Expand All @@ -115,7 +134,7 @@ print1_list([Car|Cdr], D, I, L) ->
print1_list([], _, _, _) -> [].

%% print1_tail(Tail, Depth, Indentation, LineLength)
%% Print the tail of a list. We know about dotted pairs.
%% Print the tail of a list. We know about dotted pairs.

print1_tail([], _, _, _) -> "";
print1_tail(_, 1, _, _) -> " ...";
Expand Down

0 comments on commit 4a44ac1

Please sign in to comment.