1:- module(onepointfour_basics_dict_pp_topmost,
2 [
3 pp_if_shallow_enough/3 4 ]). 5
6:- use_module(library(yall)). 7:- use_module(library(apply)). 8:- use_module(library(apply_macros)). 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
62pp_if_shallow_enough(Dict,SettingsDict,LinesOut) :-
63 get_setting(SettingsDict,depth_limit,DepthLimit,10), 64 get_setting(SettingsDict,depth,Depth), 65 check_that(DepthLimit,[hard(pos0int)]),
66 ((Depth>=DepthLimit)
67 ->
68 (dict_to_string(Dict,Line),LinesOut=[Line]) 69 ;
70 dict_pp_inner(Dict,SettingsDict,LinesOut)). 71
72dict_to_string(Dict,Line) :-
73 format(string(Line),"~q",Dict).
74
79
80dict_pp_inner(Dict,SettingsDict,ResultLines) :-
81 82 83 84 85 dict_pairs(Dict,Tag,Pairs),
86 87 88 89 90 91 92 pairs_to_entries(Pairs,SettingsDict,Entries),
93 94 95 96 lineify(Entries,SettingsDict,Lines,[]), 97 98 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
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). 114
115add_with_border_with_tag(Lines,Tag,DecisionForPadding,SettingsDict,ResultLines) :-
116 get_padding_settings_clamped(DecisionForPadding,SettingsDict,PadTop,PadBottom,PadLeft,PadRight), 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), 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), 159 ResultLines=[TagLine|Lines]. 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
173
174strings_of_given_length(List,Length) :-
175 maplist({Length}/[X]>>stringy_type_with_length(X,string(Length)),List).
176
178
179append_content(Lines,BgLine,Pos,PadTop,PadBottom,ResultLines,FinalFin) :-
180 pad_around(PadTop,BgLine,ResultLines,Fin1), 181 maplist_onto_open_list(mpl_overwrite(BgLine,Pos),Lines,Fin1,Fin2), 182 pad_around(PadBottom,BgLine,Fin2,FinalFin). 183
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).
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)
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
*/