aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/adglent/src/showtime@internal@reports@table.erl
blob: 35dbba3deca744cf82bc9d9f44d281c35c567ed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
-module(showtime@internal@reports@table).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).

-export([to_string/1, align_table/1]).
-export_type([content/0, col/0, table/0]).

-type content() :: {content, binary()} | {styled_content, binary()}.

-type col() :: {align_right, content(), integer()} |
    {align_left, content(), integer()} |
    {align_right_overflow, content(), integer()} |
    {align_left_overflow, content(), integer()} |
    {separator, binary()} |
    {aligned, binary()}.

-type table() :: {table, gleam@option:option(binary()), list(list(col()))}.

-spec to_string(table()) -> binary().
to_string(Table) ->
    Rows = begin
        _pipe = erlang:element(3, Table),
        _pipe@3 = gleam@list:map(_pipe, fun(Row) -> _pipe@1 = Row,
                _pipe@2 = gleam@list:filter_map(_pipe@1, fun(Col) -> case Col of
                            {separator, Char} ->
                                {ok, Char};

                            {aligned, Content} ->
                                {ok, Content};

                            _ ->
                                {error, nil}
                        end end),
                gleam@string:join(_pipe@2, <<""/utf8>>) end),
        gleam@string:join(_pipe@3, <<"\n"/utf8>>)
    end,
    Header@1 = begin
        _pipe@4 = erlang:element(2, Table),
        _pipe@5 = gleam@option:map(
            _pipe@4,
            fun(Header) -> <<Header/binary, "\n"/utf8>> end
        ),
        gleam@option:unwrap(_pipe@5, <<""/utf8>>)
    end,
    <<Header@1/binary, Rows/binary>>.

-spec pad_left(binary(), integer(), binary()) -> binary().
pad_left(Str, Num, Char) ->
    Padding = begin
        _pipe = gleam@list:repeat(Char, Num),
        gleam@string:join(_pipe, <<""/utf8>>)
    end,
    <<Padding/binary, Str/binary>>.

-spec pad_right(binary(), integer(), binary()) -> binary().
pad_right(Str, Num, Char) ->
    Padding = begin
        _pipe = gleam@list:repeat(Char, Num),
        gleam@string:join(_pipe, <<""/utf8>>)
    end,
    <<Str/binary, Padding/binary>>.

-spec align_table(table()) -> table().
align_table(Table) ->
    Cols = begin
        _pipe = erlang:element(3, Table),
        gleam@list:transpose(_pipe)
    end,
    Col_width = begin
        _pipe@1 = Cols,
        gleam@list:map(_pipe@1, fun(Col) -> _pipe@2 = Col,
                _pipe@3 = gleam@list:map(
                    _pipe@2,
                    fun(Content) -> case Content of
                            {align_right, {content, Unstyled}, _} ->
                                Unstyled;

                            {align_right, {styled_content, Styled}, _} ->
                                showtime@internal@reports@styles:strip_style(
                                    Styled
                                );

                            {align_left, {content, Unstyled@1}, _} ->
                                Unstyled@1;

                            {align_left, {styled_content, Styled@1}, _} ->
                                showtime@internal@reports@styles:strip_style(
                                    Styled@1
                                );

                            {align_left_overflow, _, _} ->
                                <<""/utf8>>;

                            {align_right_overflow, _, _} ->
                                <<""/utf8>>;

                            {separator, Char} ->
                                Char;

                            {aligned, Content@1} ->
                                Content@1
                        end end
                ),
                gleam@list:fold(
                    _pipe@3,
                    0,
                    fun(Max, Str) ->
                        gleam@int:max(Max, gleam@string:length(Str))
                    end
                ) end)
    end,
    Aligned_col = begin
        _pipe@4 = Cols,
        _pipe@5 = gleam@list:zip(_pipe@4, Col_width),
        gleam@list:map(
            _pipe@5,
            fun(Col_and_width) ->
                {Col@1, Width} = Col_and_width,
                _pipe@6 = Col@1,
                gleam@list:map(_pipe@6, fun(Content@2) -> case Content@2 of
                            {align_right, {content, Unstyled@2}, Margin} ->
                                {aligned,
                                    pad_left(
                                        Unstyled@2,
                                        (Width + Margin) - gleam@string:length(
                                            Unstyled@2
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_right, {styled_content, Styled@2}, Margin@1} ->
                                {aligned,
                                    pad_left(
                                        Styled@2,
                                        (Width + Margin@1) - gleam@string:length(
                                            showtime@internal@reports@styles:strip_style(
                                                Styled@2
                                            )
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_right_overflow,
                                {content, Unstyled@3},
                                Margin@2} ->
                                {aligned,
                                    pad_left(
                                        Unstyled@3,
                                        (Width + Margin@2) - gleam@string:length(
                                            Unstyled@3
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_right_overflow,
                                {styled_content, Styled@3},
                                Margin@3} ->
                                {aligned,
                                    pad_left(
                                        Styled@3,
                                        (Width + Margin@3) - gleam@string:length(
                                            showtime@internal@reports@styles:strip_style(
                                                Styled@3
                                            )
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_left, {content, Unstyled@4}, Margin@4} ->
                                {aligned,
                                    pad_right(
                                        Unstyled@4,
                                        (Width + Margin@4) - gleam@string:length(
                                            Unstyled@4
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_left, {styled_content, Styled@4}, Margin@5} ->
                                {aligned,
                                    pad_right(
                                        Styled@4,
                                        (Width + Margin@5) - gleam@string:length(
                                            showtime@internal@reports@styles:strip_style(
                                                Styled@4
                                            )
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_left_overflow,
                                {content, Unstyled@5},
                                Margin@6} ->
                                {aligned,
                                    pad_right(
                                        Unstyled@5,
                                        (Width + Margin@6) - gleam@string:length(
                                            Unstyled@5
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {align_left_overflow,
                                {styled_content, Styled@5},
                                Margin@7} ->
                                {aligned,
                                    pad_right(
                                        Styled@5,
                                        (Width + Margin@7) - gleam@string:length(
                                            showtime@internal@reports@styles:strip_style(
                                                Styled@5
                                            )
                                        ),
                                        <<" "/utf8>>
                                    )};

                            {separator, Char@1} ->
                                {separator, Char@1};

                            {aligned, Content@3} ->
                                {aligned, Content@3}
                        end end)
            end
        )
    end,
    Aligned_rows = begin
        _pipe@7 = Aligned_col,
        gleam@list:transpose(_pipe@7)
    end,
    erlang:setelement(3, Table, Aligned_rows).