Did you know ... Search Documentation:
Pack onepointfour_basics -- prolog/dict_pp.pl
PublicShow source

Predicates

dict_pp/1dict_pp(+Dict)Prettyprint Dict to current_output, assuming default settings.
dict_pp/2dict_pp(+Dict,+SettingsDict)Prettyprint Dict to current_output, take settings in SettingsDict.
dict_pp/3dict_pp(+Dict,+SettingsDict,-Lines)Prettyprint Dict to a list of lines, Lines, take settings in SettingsDict.

Examples

Prettyprint Dict to a list of strings with default settings (SettingsDict set to _{}). Note that here, Dict has no valid tag.

?- dict_pp(_{a:1,b:2},_{},Lines).
Lines = ["a : 1","b : 2"].
true.
?- dict_pp(_{a:"hello world",b:"foo bar baz"},_{},Lines).
Lines = ["a : hello world","b : foo bar baz"].
true.

Direct output of result, with some settings.

?- dict_pp(_{w: 0.25984759, ww: 1.4587598, www: 643764856, wwww: 400},
           _{justify_key:right,spec_float:f}).
   w : 0.259848
  ww : 1.458760
 www : 643764856
wwww : 400
true.

Direct ouput of result, with different settings. Here, Dict has a valid tag.

?- dict_pp(various{w: 0.25984759, ww: 1.4587598, www: 643764856, wwww: 400},
           _{justify_key:right,justify_value:right,spec_float:e}).
    various
   w : 2.598476e-01
  ww : 1.458760e+00
 www :    643764856
wwww :          400
true.

Wrap the result in a border.

?- dict_pp(various{w: 0.25984759, ww: 1.4587598, www: 643764856, wwww: 400},
           _{border:true}).
+----------------+
|    various     |
+----------------+
|w    : 0.259848 |
|ww   : 1.458760 |
|www  : 643764856|
|wwww : 400      |
+----------------+
true.

No border, but with padding around the result.

?- dict_pp(various{w: 0.25984759, ww: 1.4587598, www: 643764856, wwww: 400},
           _{pad_left:5,pad_right:4,pad:true}).
         various
     w    : 0.259848
     ww   : 1.458760
     www  : 643764856
     wwww : 400
true.

With both border and padding.

?- dict_pp(various{w: 0.25984759, ww: 1.4587598, www: 643764856, wwww: 400},
           _{pad:true,pad_left:2,pad_top:1,pad_bottom:1,pad_right:2,
             border:true,justify_tag:left,justify_tag_full:false}).
+--------------------+
|  various           |
+--------------------+
|                    |
|  w    : 0.259848   |
|  ww   : 1.458760   |
|  www  : 643764856  |
|  wwww : 400        |
|                    |
+--------------------+
true.

Prettyprint a dict with subdicts.

?- dict_pp(alpha{w1: 10, w2: 200, w3: 3000,
                   w4: bravo{w1: 10, w2: 20,
                      w3: charlie{ a: 12, b: 13}}},
           _{border:true}).
+---------------------+
|        alpha        |
+---------------------+
|w1 : 10              |
|w2 : 200             |
|w3 : 3000            |
|w4 : +--------------+|
|     |    bravo     ||
|     +--------------+|
|     |w1 : 10       ||
|     |w2 : 20       ||
|     |w3 : +-------+||
|     |     |charlie|||
|     |     +-------+||
|     |     |a : 12 |||
|     |     |b : 13 |||
|     |     +-------+||
|     +--------------+|
+---------------------+
true.

Prettyprint a dict with subdicts, but suppress the tags.

?- dict_pp(alpha{w1: 10, w2: 200, w3: 3000,
                   w4: bravo{w1: 10, w2: 20,
                      w3: charlie{ a: 12, b: 13}}},
           _{border:true,tag:false}).
+--------------------+
|w1 : 10             |
|w2 : 200            |
|w3 : 3000           |
|w4 : +-------------+|
|     |w1 : 10      ||
|     |w2 : 20      ||
|     |w3 : +------+||
|     |     |a : 12|||
|     |     |b : 13|||
|     |     +------+||
|     +-------------+|
+--------------------+
true.

Prettyprint a dict with subdicts, show the tags, don't show a border.

?- dict_pp(alpha{w1: 10, w2: 200, w3: 3000,
                   w4: bravo{w1: 10, w2: 20,
                      w3: charlie{ a: 12, b: 13}}},
           _{border:false,tag:true}).
      alpha
w1 : 10
w2 : 200
w3 : 3000
w4 :    bravo
     w1 : 10
     w2 : 20
     w3 : charlie
          a : 12
          b : 13

History

  1. 2021-01-29 - Version 1
  2. 2021-02-05 - Version 2
  3. 2021-06-22 - Version 3
 dict_pp(+Dict:dict)
Prettyprint Dict directly using format/2, writing to the stream given by current_output. An empty Dict does not lead to failure but to no output.

Wrap this goal with with_output_to/2 to redirect the output to a stream of your choice.

Behaves as dict_pp/3 called with default settings, followed by immediate printing of the resulting Lines.

See also
- dict_pp/3
 dict_pp(+Dict:dict, +SettingsDict:dict)
Prettyprint Dict directly using format/2, writing to the stream given by current_output. An empty Dict does not lead to failure but to no output.

Instructions on how to format the output can be given by SettingsDict. The tag of that dict is arbitrary. Default settings are requested by giving an empty dict here.

Wrap this goal with with_output_to/2 to redirect the output to a stream of your choice.

Behaves as dict_pp/3, followed by immediate printing of the resulting lines.

See also
- dict_pp/3
 dict_pp(+Dict:dict, +SettingsDict:dict, -LinesOut:list(string))
Prettyprint Dict by generating strings that are accumulated into the list LinesOut, maybe for later emission to an output stream. The lines do not have a newline at their end. An empty Dict does not lead to failure but to an empty list (or a nonempty list, depending on SettingsDict).

Instructions on how to format the output can be given by SettingsDict. The tag of that dict is arbitrary. Default settings are requested by giving an empty dict here.

The following settings are understood. Anything not recognized is disregarded, if a setting is missing when it is needed, the default value is assumed.

keyvaluedefaultexplainer
bordertrue, falsefalseDecorate outermost dict with an ASCII border.
sub_bordertrue, false, inheritinheritWhether to decorate subdicts with an ASCII border, too.
tagtrue, falsetruePrint the tag of the outermost dict. If the tag is an unbound variable, it is never printed.
sub_tagtrue, false, inheritinheritWhether to display the tags of subdicts, too.
justify_keyleft, right, centerleftHow to justify the keys inside the key column.
justify_valueleft, right, centerleftHow to justify the values inside the values column.
justify_tagleft, right, centercenterHow to justify the tag inside the tag line. f stands for "full".
justify_tag_fulltrue, falsetrueLeft and right padding is considered as being part of the tag field.
spec_floatsee format/2fA format/2 specifier used for floats. Passed to format/3 "as is".
spec_intsee format/2dA format/2 specifier used for integers. Passed to format/3 "as is".
depth_limitint >= 010"subdict depth" at which prettyprinting switches to a "one-liner". 0 means even the root dict is printed as a oneliner.
padtrue, falsefalseSwitch on padding according to pad_left etc. Note that if pad is true (and border is false), and none of the pad_X values has been given, then the output is a rectangle filled to rectangle-ness with whitespace.
sub_padtrue, false, inheritinheritWhether to pad subdicts with whitespace, too.
pad_leftint >= 00Pad with whitespace on the left depending on pad and sub_pad (inside the ASCII border if any).
pad_rightint >= 00As above, on the right.
pad_topint >= 00As above, on top (underneath the tag, if any).
pad_bottomint >= 00As above, on the bottom.