1:- module(onepointfour_basics_dict_pp_topmost,
    2          [
    3          pp_if_shallow_enough/3   % pp_if_shallow_enough(Dict,SettingsDict,LinesOut)
    4          ]).    5
    6:- use_module(library(yall)).            % yall meta predicates (slow)
    7:- use_module(library(apply)).           % meta-call predicates (slow)
    8:- use_module(library(apply_macros)).    % rewrite to avoid meta-calling (transforms the above into fast code)
    9
   10:- use_module(library('onepointfour_basics/checks.pl')).   11:- use_module(library('onepointfour_basics/dict_settings.pl')).   12:- use_module(library('onepointfour_basics/stringy_overwrite.pl')).   13:- use_module(library('onepointfour_basics/meta_helpers.pl')).   14:- use_module(library('onepointfour_basics/stringy_and_charylist_type.pl')).   15:- use_module(library('onepointfour_basics/dict_pp/decision.pl')).   16:- use_module(library('onepointfour_basics/dict_pp/lineify.pl')).   17:- use_module(library('onepointfour_basics/dict_pp/helpers.pl')).   18:- use_module(library('onepointfour_basics/dict_pp/string_stuff.pl')).   19
   20/*  MIT License Follows (https://opensource.org/licenses/MIT)
   21
   22    Copyright 2021 David Tonhofer <ronerycoder@gluino.name>
   23
   24    Permission is hereby granted, free of charge, to any person obtaining
   25    a copy of this software and associated documentation files
   26    (the "Software"), to deal in the Software without restriction,
   27    including without limitation the rights to use, copy, modify, merge,
   28    publish, distribute, sublicense, and/or sell copies of the Software,
   29    and to permit persons to whom the Software is furnished to do so,
   30    subject to the following conditions:
   31
   32    The above copyright notice and this permission notice shall be
   33    included in all copies or substantial portions of the Software.
   34
   35    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   36    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   37    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   38    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   39    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   40    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   41    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   42*/

Recursively called predicate for dict prettyprinting

This module exports the predicate which is called from dict_pp/3 and eventually from itself if dict prettyprinting needs to prettyprint subdicts.

The homepage for this module is at

https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_dict_pp.md

*/

 pp_if_shallow_enough(+Dict, +SettingsDict, -LinesOut)
If the limit of the depth-of-recursion has been reached, print the dict as a oneliner using format/3 and the ~q placeholder, otherwise proceed with dict prettyprinting. This predicate is recursively called if a (sub-)dict is encountered during prettyprinting.
   62pp_if_shallow_enough(Dict,SettingsDict,LinesOut) :-
   63   get_setting(SettingsDict,depth_limit,DepthLimit,10),  % Get value of 'depth_limit', which is >= 0
   64   get_setting(SettingsDict,depth,Depth),                % 'depth' entry must exist
   65   check_that(DepthLimit,[hard(pos0int)]),
   66   ((Depth>=DepthLimit) 
   67    -> 
   68    (dict_to_string(Dict,Line),LinesOut=[Line])          % Limit reached; print dict as oneliner!
   69    ;
   70    dict_pp_inner(Dict,SettingsDict,LinesOut)).          % Otherwise let's go on...
   71
   72dict_to_string(Dict,Line) :-
   73   format(string(Line),"~q",Dict).
   74
   75% Generate lines for the Dict, then possibly pad them with whitespace into
   76% a rectangle and possibly put an ASCII border around the rectangle
   77% depending on what SettingsDict says. Whether the dict's tag is printed
   78% depends on whether it is set and on what SettingsDict says about that.
   79
   80dict_pp_inner(Dict,SettingsDict,ResultLines) :-
   81   %
   82   % "Dict" -> ("Tag", sorted "Pairs")
   83   % Does NOT fail if Dict is empty. Pairs will just be [].
   84   %
   85   dict_pairs(Dict,Tag,Pairs),
   86   % 
   87   % Transform each pair "Key-Value" into an entry "KeyString-Compound"
   88   % where "KeyString" is a suitably formatted string representing the dict key,
   89   % and "Compound" is a compound term "mono(String)" or "poly(ListOfString)".
   90   % This may involve recursively calling dict prettyprinting on (sub-)dicts. 
   91   %
   92   pairs_to_entries(Pairs,SettingsDict,Entries),
   93   %
   94   % Generate a list of strings "Lines" from the "Pairs" according to "SettingsDict".
   95   %
   96   lineify(Entries,SettingsDict,Lines,[]), % "Lines-[]" used to be an open difference list, which we close
   97   %
   98   % How to build the output
   99   %
  100   decision_how_to_display(Tag,SettingsDict,DecisionForTag,DecisionForBorder,DecisionForPadding),
  101   assertion(check_that(DecisionForPadding,hard(boolean))),
  102   assertion(check_that(DecisionForTag,hard(boolean))),
  103   assertion(check_that(DecisionForBorder,hard(boolean))),
  104   once(dispatch(DecisionForTag,DecisionForBorder,DecisionForPadding,Lines,Tag,SettingsDict,ResultLines)).
  105
  106% dispatch(DecisionForTag,DecisionForBorder,DecisionForPadding,Lines,Tag,SettingsDict,ResultLines)
  107
  108dispatch(true  , true  , DecisionForPadding , Lines , Tag , SettingsDict , LinesOut) :- add_with_border_with_tag(Lines,Tag,DecisionForPadding,SettingsDict,LinesOut).
  109dispatch(false , true  , DecisionForPadding , Lines , _   , SettingsDict , LinesOut) :- add_with_border_without_tag(Lines,DecisionForPadding,SettingsDict,LinesOut).
  110dispatch(true  , false , true               , Lines , Tag , SettingsDict , LinesOut) :- add_with_tag_with_padding(Lines,Tag,SettingsDict,LinesOut).
  111dispatch(true  , false , false              , Lines , Tag , SettingsDict , LinesOut) :- add_with_tag_without_padding(Lines,Tag,SettingsDict,LinesOut).
  112dispatch(false , false , true               , Lines , _   , SettingsDict , LinesOut) :- add_with_padding(Lines,SettingsDict,LinesOut).
  113dispatch(false , false , false              , Lines , _   , _            , Lines).      % no tag, no border, no padding -> just transfer lines
  114
  115add_with_border_with_tag(Lines,Tag,DecisionForPadding,SettingsDict,ResultLines) :-
  116   get_padding_settings_clamped(DecisionForPadding,SettingsDict,PadTop,PadBottom,PadLeft,PadRight), % if padding is off, pad at least 0
  117   max_line_width([Tag|Lines],MaxLineWidth),
  118   make_tag_line(Tag,MaxLineWidth,PadLeft,PadRight,SettingsDict,TagLine),
  119   make_bordery_lines(MaxLineWidth,PadLeft,PadRight,BorderLine,BgLine), 
  120   ResultLines=[BorderLine|Fin1],
  121   append_tag_line(TagLine,BgLine,Fin1,Fin2),
  122   Fin2=[BorderLine|Fin3],
  123   Pos is 1+PadLeft,
  124   append_content(Lines,BgLine,Pos,PadTop,PadBottom,Fin3,Fin4),
  125   Fin4=[BorderLine],
  126   assertion(
  127      (Len is 1+PadLeft+MaxLineWidth+PadRight+1, 
  128      strings_of_given_length(ResultLines,Len))
  129   ).
  130
  131add_with_border_without_tag(Lines,DecisionForPadding,SettingsDict,ResultLines) :- 
  132   get_padding_settings_clamped(DecisionForPadding,SettingsDict,PadTop,PadBottom,PadLeft,PadRight), % if padding is off, pad at least 0
  133   max_line_width(Lines,MaxLineWidth),
  134   make_bordery_lines(MaxLineWidth,PadLeft,PadRight,BorderLine,BgLine), 
  135   ResultLines=[BorderLine|Fin1],
  136   Pos is 1+PadLeft,
  137   append_content(Lines,BgLine,Pos,PadTop,PadBottom,Fin1,Fin2),
  138   Fin2=[BorderLine],
  139   assertion(
  140      (Len is 1+PadLeft+MaxLineWidth+PadRight+1, 
  141      strings_of_given_length(ResultLines,Len))
  142   ).
  143
  144add_with_tag_with_padding(Lines,Tag,SettingsDict,ResultLines) :-
  145   get_padding_settings(SettingsDict,PadTop,PadBottom,PadLeft,PadRight),
  146   max_line_width([Tag|Lines],MaxLineWidth),
  147   make_tag_line(Tag,MaxLineWidth,PadLeft,PadRight,SettingsDict,TagLine),
  148   ResultLines=[TagLine|Fin1],
  149   make_background_line_for_padding(MaxLineWidth,PadLeft,PadRight,BgLine),
  150   append_content(Lines,BgLine,PadLeft,PadTop,PadBottom,Fin1,[]),
  151   assertion(
  152      (Len is PadLeft+MaxLineWidth+PadRight,
  153      strings_of_given_length(ResultLines,Len))
  154   ).
  155
  156add_with_tag_without_padding(Lines,Tag,SettingsDict,ResultLines) :-
  157   max_line_width([Tag|Lines],MaxLineWidth),
  158   make_tag_line(Tag,MaxLineWidth,0,0,SettingsDict,TagLine), % has whitespace on the right though
  159   ResultLines=[TagLine|Lines]. % variable whitespace on the right
  160
  161add_with_padding(Lines,SettingsDict,ResultLines) :-
  162   get_padding_settings(SettingsDict,PadTop,PadBottom,PadLeft,PadRight),
  163   max_line_width(Lines,MaxLineWidth),
  164   make_background_line_for_padding(MaxLineWidth,PadLeft,PadRight,BgLine),
  165   append_content(Lines,BgLine,PadLeft,PadTop,PadBottom,ResultLines,[]),
  166   assertion(
  167      (Len is PadLeft+MaxLineWidth+PadRight, 
  168      strings_of_given_length(ResultLines,Len))
  169   ).
  170
  171% "it is a list of strings and all the strings have the given length"
  172% If List is not a list in the first place, maplist fails (we assume it is not an open list)
  173
  174strings_of_given_length(List,Length) :-
  175   maplist({Length}/[X]>>stringy_type_with_length(X,string(Length)),List).
  176
  177% Append lines underneath the border line underneath the tag
  178
  179append_content(Lines,BgLine,Pos,PadTop,PadBottom,ResultLines,FinalFin) :-
  180   pad_around(PadTop,BgLine,ResultLines,Fin1),                          % append PadTop x BgLine
  181   maplist_onto_open_list(mpl_overwrite(BgLine,Pos),Lines,Fin1,Fin2),   % append content lines 
  182   pad_around(PadBottom,BgLine,Fin2,FinalFin).                          % append PadBottom x BgLine
  183
  184% The tag line is created by overwriting:
  185%
  186%  xxxxxxxxxxTAGxxxxxxxx   tag line
  187% |                     |  background line with left and right box borders
  188
  189append_tag_line(TagLine,BgLine,ResultLines,FinalFin) :-
  190   maplist_onto_open_list(mpl_overwrite(BgLine,1),[TagLine],ResultLines,FinalFin).
  191
  192mpl_overwrite(BgLine,Pos,LineIn,Result) :-
  193   CutLeft=true,
  194   CutRight=true,
  195   stringy_overwrite(BgLine,LineIn,Pos,CutLeft,CutRight,Result,string).
 pad_around(+PadCount, +PadLine, ?Tip, ?FinalFin)
Simple iteration for "top" or "bottom" padding around list of lines given by an open list difference list. The line inserted is PadLine.
  202pad_around(0,_,FinalFin,FinalFin) :- !.
  203
  204pad_around(Count,PadLine,[PadLine|NewFin],FinalFin) :-
  205   assertion(Count>0),
  206   CountMinus is Count-1,
  207   pad_around(CountMinus,PadLine,NewFin,FinalFin)