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

The Data in the table is represented as a list of dicts, where each dict represents a Row. The Keys in the dict (i.e. Row) correspond to a Column. The Value associated to a Key, represents the contents of a Cell. It is wrapped to fit the Width of the Column (the wrapping alogyrithm alows a text to break at whitespace and hyphens, see wrap_text/3).

The formatting of the Table (i.e. Width and Height of Rows and Collumns) is automatically calculated. The heuristic used is prioritize the columns with the most content to use the largest share of available width (i.e minimize the hight of a row). If the content cannot be fit within the available width an Exception is raised.

The formatting and rendering of the Table can be adapted in a number of ways: Use a subset of available Columns, and define the order in which they are presented Define the formatting of an individual Column (e.g width, alignmet, header, ...) The Caption presented with the Table The visual style of the Table (currently sypported, default, unicode, mysql, github) The maximum Width the table can use.

print_table/6 allow the user to set all parrameters. The print_table/N with fewer arguments implement defaults.

Example

:- Data = [_{a:11,b:0.001,c:13},_{a:21,b:2.12,c:23},_{a:31,b:12.1111,c:33}],
print_table(Data,[b,c,a],_{b:_{align:right,format:"~2f"}},"Table",mysql,30).

%       Table (3 records)
%      +-------+----+----+
%      |     b | c  | a  |
%      +-------+----+----+
%      |  0.00 | 13 | 11 |
%      |  2.12 | 23 | 21 |
%      | 12.11 | 33 | 31 |
%      +-------+----+----+
author
- Joost Geurts
license
- MIT License
 print_table(+Data:list(dict)) is det
 print_table(+Data:list(dict), +Keys:list(atom)) is det
 print_table(+Data:list(dict), +Keys:list(atom), +ColumnsSpec:dict) is det
 print_table(+Data:list(dict), +Keys:list(atom), +ColumnsSpec:dict, +Caption:string) is det
 print_table(+Data:list(dict), +Keys:list(atom), +ColumnsSpec:dict, +Caption:string, +Style:atom) is det
 print_table(+Data:list(dict), +Keys:list(atom), +ColumnsSpec:dict, +Caption:string, +Style:atom, +MaxWidth:integer) is det
  • Data is a list of Dicts, where each Dict represent a Row of the Table. The union of all Keys in the Rows reprent the Columns of the table (if a Dict doesn't define a Key it is filled with 'null')
  • Keys selects the Columns that will be printed, as well a their order.
  • ColumnsSpec allows the user to specify the formating of a particular column (see below) It is a is a Dict, whose Keys reference a Column. The Value is a Dict recognizes the folowing Keys:
    • header to define the text that is printed as header (by default the Key is used)
    • width to define the width of the column (by default this is calculated automatically)
    • format to define the formatting template (see format/2) used to print the value (by default "~w" is used)
    • align sets the alignment within the cell (left, center, right)
  • Caption Sets the Caption of the Table
  • Style sets the visual style to print the table. current options are default, unicode, mysql, github
  • MaxWidth defines the Maxium width (in characters) the Table can have. By default the whole terminal width is used

Undocumented predicates

The following predicates are exported, but not or incorrectly documented.

 print_table(Arg1, Arg2)
 print_table(Arg1, Arg2, Arg3)
 print_table(Arg1, Arg2, Arg3, Arg4)
 print_table(Arg1, Arg2, Arg3, Arg4, Arg5)
 print_table(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)