34
35:- module(tabulator, [tabulate/3, align/4]). 36
37:- use_module(library(lists)). 38
39tabulate(FillChar, Table1, Table) :-
40 maplist(tabulate_row(Lists), Table1, Table),
41 maplist(close_list(FillChar), Lists).
42
43close_list(FillChar, List) :-
44 maplist('='(FillChar), List),
45 !.
46
47tabulate_row(Lengths, Row1, Row) :-
48 maplist(tabulate_element, Lengths, Row1, Row).
49
50tabulate_element(List, Column1, Column) :-
51 length(Column1, Length1),
52 length(List1, Length1),
53 append(Column1, Tail1, Column),
54 append(List1, Tail1, List).
55
56align(FillChar, Scheme, Table1, Table) :-
57 maplist(align_row(FillChar, Scheme), Table1, Table).
58
59align_row(FillChar, Scheme, Row1, Row) :-
60 maplist(align_cell_(FillChar), Scheme, Row1, Row).
61
62align_cell_(FillChar, Align, Cell1, Cell) :-
63 align_cell(Align, FillChar, Cell1, Cell).
64
65align_cell(left, FillChar, Cell1, Cell) :-
66 align_cell_left(Cell1, FillChar, Cell).
67align_cell(right, FillChar, Cell1, Cell) :-
68 align_cell_right(Cell1, FillChar, Cell).
69align_cell(center, FillChar, Cell1, Cell) :-
70 align_cell_center(Cell1, FillChar, Cell).
71align_cell(none, _, Cell, Cell).
72
73align_cell_left(Cell1, FillChar, Cell) :-
74 discompose_cell(Cell1, FillChar, [], FillStr, Cell2),
75 append(Cell2, FillStr, Cell).
76
77discompose_cell([], _, FillStr, FillStr, []).
78discompose_cell([Char|Cell1], Char, FillStr1, FillStr, Cell) :- !,
79 discompose_cell(Cell1, Char, [Char|FillStr1], FillStr, Cell).
80discompose_cell(Cell, _, FillStr, FillStr, Cell).
81
82align_cell_right(Cell1, FillChar, Cell) :-
83 reverse(Cell1, Cell2),
84 discompose_cell(Cell2, FillChar, [], FillStr, Cell3),
85 reverse(Cell3, Cell4),
86 append(FillStr, Cell4, Cell).
87
88align_cell_center(Cell1, FillChar, Cell) :-
89 discompose_cell(Cell1, FillChar, [], Fill1, Cell2),
90 reverse(Cell2, Cell3),
91 discompose_cell(Cell3, FillChar, [], Fill2, Cell4),
92 reverse(Cell4, Cell5),
93 length(Fill1, N1),
94 length(Fill2, N2),
95 NL is (N1 + N2) // 2,
96 length(FillL, NL),
97 append(Fill1, Fill2, Fill),
98 append(FillL, FillR, Fill),
99 append([FillL, Cell5, FillR], Cell)