I have implemented portable(true), max_text(+Length)andtruncated(-Bool)`. Some remarks:
- I renamed
text_max(+Length)tomax_text(+Length)because all Prolog options and flags aremax_*. Will be renamed to whatever the consensus will be. - It now prints prefix…suffix, where the suffix length is min((Max-AtomLen-EllipsisLen)//2, 3) and the prefix length takes the remaining characters. These are some remarks
- If the streams allows for it, we use Unicode U-2026 (…), otherwise ASCII
... - If Length > 0 but < length(ellipsis) we take length(ellipsis). So,
max_text(1)always just prints the ellipsis. - For non-quoted text we are now done.
- For quoted text,
- The quotes are not included in
Length. - The total print length may exceed
Lengthdue to escape sequences. I.e., we compute the prefix and suffix lengths according to the above and then print using escape sequences where required. - If an atom needs no quotes, but its length exceeds
Length, it will be quoted.
- The quotes are not included in
- If the streams allows for it, we use Unicode U-2026 (…), otherwise ASCII
truncated(-Bool) is set to true if either or both max_depth or max_text are exceeded.
Here are some tests:
:- begin_tests(max_text).
test(string, Out == "\"hello\"") :-
max_text(5, "hello", Out, [quoted(true)]).
test(string, Out == "\"hel…rld\"") :-
max_text(7, "hello world", Out, [quoted(true), truncated(Bool)]),
assertion(Bool == true).
test(string, Out == "\"…\"") :-
max_text(1, "hello world", Out, [quoted(true)]).
test(string, Out == "\"h…\"") :-
max_text(2, "hello world", Out, [quoted(true)]).
test(string, Out == "\"h…d\"") :-
max_text(3, "hello world", Out, [quoted(true)]).
test(string, Out == "\"hel…ιος\"") :-
max_text(7, "hello υφήλιος", Out, [quoted(true)]).
test(atom, Out == "'hel…ιος'") :-
max_text(7, 'hello υφήλιος', Out, [quoted(true)]).
test(atom, Out == "hello_υφήλιος") :-
max_text(20, hello_υφήλιος, Out, [quoted(true)]).
test(atom, Out == "'hel…ιος'") :-
max_text(7, hello_υφήλιος, Out, [quoted(true)]).
test(atom, Out == "'\\nhe…ιος'") :-
max_text(7, '\nhello_υφήλιος', Out, [quoted(true)]).
test(atom, Out == "hel…ιος") :-
max_text(7, hello_υφήλιος, Out, [quoted(false)]).
max_text(Max, Term, String, Options) :-
with_output_to(string(String),
write_term(Term, [max_text(Max)|Options])).
:- end_tests(max_text).