Current section

Files

Jump to
glint src glint@internal@utils.erl
Raw

src/glint@internal@utils.erl

-module(glint@internal@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([max_string_length/1, wordwrap/2]).
-spec max_string_length(list(binary())) -> integer().
max_string_length(Strings) ->
gleam@list:fold(Strings, 0, fun(Max, F) -> _pipe = F,
_pipe@1 = gleam@string:length(_pipe),
gleam@int:max(_pipe@1, Max) end).
-spec do_wordwrap(list(binary()), integer(), binary(), list(binary())) -> list(binary()).
do_wordwrap(Tokens, Max_width, Line, Lines) ->
case Tokens of
[Token | Tokens@1] ->
Token_length = gleam@string:length(Token),
Line_length = gleam@string:length(Line),
case {Line, ((Line_length + 1) + Token_length) =< Max_width} of
{<<""/utf8>>, _} ->
do_wordwrap(Tokens@1, Max_width, Token, Lines);
{_, true} ->
do_wordwrap(
Tokens@1,
Max_width,
<<<<Line/binary, " "/utf8>>/binary, Token/binary>>,
Lines
);
{_, false} ->
do_wordwrap(Tokens@1, Max_width, Token, [Line | Lines])
end;
[] when Line =:= <<""/utf8>> ->
lists:reverse(Lines);
[] ->
lists:reverse([Line | Lines])
end.
-spec do_space_split_lines(list(binary()), {list(binary()), boolean()}) -> {list(binary()),
boolean()}.
do_space_split_lines(Ls, Acc) ->
case {Ls, erlang:element(1, Acc), erlang:element(2, Acc)} of
{[], _, _} ->
Acc;
{[<<"\n"/utf8>>, <<"\n"/utf8>> | Rest], _, false} ->
do_space_split_lines(Rest, {erlang:element(1, Acc), true});
{[<<"\n"/utf8>>, <<"\n"/utf8>> | Rest@1], [], true} ->
do_space_split_lines(Rest@1, {[], true});
{[<<"\n"/utf8>>, <<"\n"/utf8>> | Rest@2], [S | Accs], true} ->
do_space_split_lines(
[<<"\n"/utf8>> | Rest@2],
{[<<S/binary, "\n"/utf8>> | Accs], true}
);
{[<<"\n"/utf8>> | Rest@3], [], true} ->
do_space_split_lines(Rest@3, {[], true});
{[<<"\n"/utf8>> | Rest@4], [S@1 | Accs@1], true} ->
do_space_split_lines(
Rest@4,
{[<<S@1/binary, "\n"/utf8>> | Accs@1], true}
);
{[<<"\n"/utf8>> | Rest@5], [], false} ->
do_space_split_lines(Rest@5, {[], false});
{[<<"\n"/utf8>> | Rest@6], [S@2 | Accs@2], false} ->
do_space_split_lines(
Rest@6,
{[<<S@2/binary, " "/utf8>> | Accs@2], false}
);
{[C | Rest@7], _, true} ->
do_space_split_lines(Rest@7, {[C | erlang:element(1, Acc)], false});
{[C@1 | Rest@8], [], false} ->
do_space_split_lines(Rest@8, {[C@1], false});
{[C@2 | Rest@9], [S@3 | Accs@3], false} ->
do_space_split_lines(
Rest@9,
{[<<S@3/binary, C@2/binary>> | Accs@3], false}
)
end.
-spec space_split_lines(binary()) -> list(binary()).
space_split_lines(S) ->
_pipe@2 = erlang:element(
1,
begin
_pipe = S,
_pipe@1 = gleam@string:to_graphemes(_pipe),
do_space_split_lines(_pipe@1, {[], false})
end
),
lists:reverse(_pipe@2).
-spec wordwrap(binary(), integer()) -> list(binary()).
wordwrap(S, Max_width) ->
gleam@bool:guard(
S =:= <<""/utf8>>,
[],
fun() ->
gleam@list:flat_map(space_split_lines(S), fun(Line) -> _pipe = Line,
_pipe@1 = gleam@string:split(_pipe, <<" "/utf8>>),
do_wordwrap(_pipe@1, Max_width, <<""/utf8>>, []) end)
end
).