1:- module(onepointfour_dict_pp_string_stuff,
    2          [
    3           max_line_width/2                    % max_line_width(+Lines,-MaxLineWidth)
    4          ,make_tag_line/6                     % make_tag_line(+Tag,+MaxLineWidth,+PadLeft,+PadRight,+SettingsDict,-Result)
    5          ,make_bordery_lines/5                % make_bordery_lines(+MaxLineWidth,+PadLeft,+PadRight,-HorizontalBorderResult,-BackgroundResult)
    6          ,make_background_line_for_padding/4  % make_background_line_for_padding(+MaxLineWIdth,+PadLeft,+PadRight,-Result)
    7          ]).    8
    9:- use_module(library('onepointfour_basics/checks.pl')).   10:- use_module(library('onepointfour_basics/stringy_concat.pl')).   11:- use_module(library('onepointfour_basics/stringy_justify.pl')).   12:- use_module(library('onepointfour_basics/space_stringy.pl')).   13:- use_module(library('onepointfour_basics/dict_settings.pl')).   14
   15/*  MIT License Follows (https://opensource.org/licenses/MIT)
   16
   17    Copyright 2021 David Tonhofer <ronerycoder@gluino.name>
   18
   19    Permission is hereby granted, free of charge, to any person obtaining
   20    a copy of this software and associated documentation files
   21    (the "Software"), to deal in the Software without restriction,
   22    including without limitation the rights to use, copy, modify, merge,
   23    publish, distribute, sublicense, and/or sell copies of the Software,
   24    and to permit persons to whom the Software is furnished to do so,
   25    subject to the following conditions:
   26
   27    The above copyright notice and this permission notice shall be
   28    included in all copies or substantial portions of the Software.
   29
   30    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   31    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   32    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   33    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   34    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   35    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   36    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   37*/

dict prettyprinter helper predicates

This module collects a few very simple and very specialized helper predicates dealing with strings in the context of dict prettyprinting, which just add noise to the main module. So they are here, in a separate module.

The homepage for this module is at

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

*/

 max_line_width(+Lines, -MaxLineWidth)
Find "maximum line width" over all lines (this means find "max string length" as the lines are strings)
   55max_line_width(Lines,MaxLineWidth) :-
   56   foldl(foldl_mlw,Lines,0,MaxLineWidth).
   57
   58foldl_mlw(Line,FromLeft,ToRight) :-
   59   string_length(Line,LineWidth),
   60   ToRight is max(FromLeft,LineWidth).
 make_tag_line(+Tag, +MaxLineWidth, +PadLeft, +PadRight, +SettingsDict, -Result)
Create the TagLineOut (a string) with the dict's tag Tag by justifying the tag inside that line according to the values passed. In principle, the length of string Tag =< MaxLineWidth. At call time, Tag should be an atom or an integer, but we cannot be sure, so we pump it through format/3 once. Note that is likely that both PadLeft and PadRight are 0.

Explainer for the various values:

  |<-----PadLeft--->|<---- MaxLineWidth ---->|<----PadRight----------->|
  |                 |left_justified_tag      |                         |
  |                 |     right_justified_tag|                         |
  |                 |  center_justified_tag  |                         |
  |full_left_justified_tag                   |                         |
  |                 |                        | full_right_justified_tag|
  |                 |    full_center_justified_tag                     |

SettingsDict may carry:

justify_tag : left,right,center , default: center justify_tag_full : true, false , default: true

To be done
- justify_how/4 could be extended to not perform assertion checks internally. Maybe.
- justify_how/4 generates trailing spaces in "Result", which might be unwanted.
   90make_tag_line(Tag,MaxLineWidth,PadLeft,PadRight,SettingsDict,Result) :-
   91   get_setting(SettingsDict,justify_tag,How,center),
   92   get_setting(SettingsDict,justify_tag_full,Full,true),
   93   assertion(check_that(How ,[hard(member(left,right,center))])),
   94   assertion(check_that(Full,[hard(boolean)])),
   95   preprocess_tag(Tag,TagString),
   96   make_tag_line_2(Full,How,TagString,MaxLineWidth,PadLeft,PadRight,Result).
   97
   98preprocess_tag(Tag,TagString) :-
   99   atom(Tag)
  100   ->
  101   atom_string(Tag,TagString)
  102   ;
  103   string(Tag)
  104   ->
  105   Tag=TagString
  106   ;
  107   integer(Tag)
  108   ->
  109   format(string(TagString),"~d",[Tag])
  110   ;
  111   format(string(TagString),"~q",[Tag]).
 make_tag_line_2(Full, How, TagString, MaxLineWidth, PadLeft, PadRight, Result)
Build the actual line containing the (justified) tag. Not exported.
  118make_tag_line_2(true,How,TagString,MaxLineWidth,PadLeft,PadRight,Result) :- 
  119   FullWidth is PadLeft + MaxLineWidth + PadRight,
  120   justify_how(How,FullWidth,TagString,Result,string).  % TODO in all cases whitespace over FullWidth, maybe unwanted?
  121
  122make_tag_line_2(false,How,TagString,MaxLineWidth,PadLeft,_PadRight,Result) :- 
  123   justify_how(How,MaxLineWidth,TagString,M,string),    % TODO in all cases whitespace over MaxLineWidth, maybe unwanted?
  124   space_stringy(PadLeft,LS,string),                    % PadLeft may be 0.
  125   stringy_concat([LS,M],Result,string).                % Don't bother to append a string of "PadRight" spaces. (should we?)
 make_bordery_lines(+MaxLineWidth, +PadLeft, +PadRight, -HorizontalBorderResult, -BackgroundResult)
A packaged call to perform make_horizontal_border_line/2 and make_background_line_with_border/2.
  131make_bordery_lines(MaxLineWidth,PadLeft,PadRight,HorizontalBorderResult,BackgroundResult) :-
  132   FullWidth is PadLeft + MaxLineWidth + PadRight,
  133   make_horizontal_border_line(FullWidth,HorizontalBorderResult),  % create "+-----+" line to put at the top and bottom
  134   make_background_line_with_border(FullWidth,BackgroundResult).   % create "|     |" line to use as background
 make_horizontal_border_line(+Width, -Result)
Create a "horizontal border line" that looks like "+--------+". There are Width dashes surrounded by "+" for the corners. This predicate is only called if a border had been requested. Not exported.
  143make_horizontal_border_line(Width,Result) :-
  144   length(Chars,Width),                          % TODO: same as the space_stringy, we need a dash_stringy!
  145   maplist(=("-"),Chars),                        % "Chars" is now "Width" dashes
  146   atomics_to_string(Chars,S),                   % Fuse into a string
  147   stringy_concat(["+",S,"+"],Result,string).
 make_background_line_with_border(+Width, -Result)
Create a "background line" that looks like "| |" with Width whitespace in between the surrounding "|". This predicate is only called if a border has been requested. Not exported.
  155make_background_line_with_border(Width,Result) :-
  156   space_stringy(Width,S,string),                
  157   stringy_concat(["|",S,"|"],Result,string).
 make_background_line_for_padding(+MaxLineWidth, +PadLeft, +PadRight, -Result)
Create a "background line" for padding only: it's just whitespace.
  163make_background_line_for_padding(MaxLineWidth,PadLeft,PadRight,Result) :-
  164   FullWidth is PadLeft + MaxLineWidth + PadRight,
  165   space_stringy(FullWidth,Result,string)