src/qrkit@render@ascii.erl

-module(qrkit@render@ascii).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/qrkit/render/ascii.gleam").
-export([to_string_compact/1, to_string/1, with_inverse/1]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(" Terminal renderers for qrkit QR codes.\n").

-file("src/qrkit/render/ascii.gleam", 53).
-spec render_row(list(boolean()), binary(), binary()) -> binary().
render_row(Row, Dark, Light) ->
    _pipe = Row,
    _pipe@1 = gleam@list:map(_pipe, fun(Value) -> case Value of
                true ->
                    Dark;

                false ->
                    Light
            end end),
    gleam@string:join(_pipe@1, <<""/utf8>>).

-file("src/qrkit/render/ascii.gleam", 77).
-spec compact_row(list(boolean()), list(boolean())) -> binary().
compact_row(Top, Bottom) ->
    _pipe = gleam@list:map2(
        Top,
        Bottom,
        fun(Top_value, Bottom_value) -> case {Top_value, Bottom_value} of
                {true, true} ->
                    <<"█"/utf8>>;

                {true, false} ->
                    <<"▀"/utf8>>;

                {false, true} ->
                    <<"▄"/utf8>>;

                {false, false} ->
                    <<" "/utf8>>
            end end
    ),
    gleam@string:join(_pipe, <<""/utf8>>).

-file("src/qrkit/render/ascii.gleam", 64).
-spec compact_lines(list(list(boolean())), list(binary())) -> list(binary()).
compact_lines(Rows, Acc) ->
    case Rows of
        [Top, Bottom | Rest] ->
            compact_lines(Rest, [compact_row(Top, Bottom) | Acc]);

        [Top@1] ->
            compact_lines(
                [],
                [compact_row(
                        Top@1,
                        gleam@list:repeat(false, erlang:length(Top@1))
                    ) |
                    Acc]
            );

        [] ->
            lists:reverse(Acc)
    end.

-file("src/qrkit/render/ascii.gleam", 33).
-spec pad_rows(list(list(boolean()))) -> list(list(boolean())).
pad_rows(Rows) ->
    Width = case Rows of
        [First | _] ->
            erlang:length(First);

        [] ->
            0
    end,
    Padded_row = gleam@list:repeat(false, Width + (4 * 2)),
    Body = begin
        _pipe = Rows,
        gleam@list:map(
            _pipe,
            fun(Row) ->
                lists:append(
                    gleam@list:repeat(false, 4),
                    lists:append(Row, gleam@list:repeat(false, 4))
                )
            end
        )
    end,
    lists:append(
        gleam@list:repeat(Padded_row, 4),
        lists:append(Body, gleam@list:repeat(Padded_row, 4))
    ).

-file("src/qrkit/render/ascii.gleam", 20).
?DOC(" Render the QR code using Unicode half blocks to halve the height.\n").
-spec to_string_compact(qrkit:qr_code()) -> binary().
to_string_compact(Qr) ->
    Padded = pad_rows(qrkit:rows(Qr)),
    _pipe = compact_lines(Padded, []),
    gleam@string:join(_pipe, <<"\n"/utf8>>).

-file("src/qrkit/render/ascii.gleam", 26).
-spec render(qrkit:qr_code(), binary(), binary()) -> binary().
render(Qr, Dark, Light) ->
    Rows = pad_rows(qrkit:rows(Qr)),
    _pipe = Rows,
    _pipe@1 = gleam@list:map(
        _pipe,
        fun(Row) -> render_row(Row, Dark, Light) end
    ),
    gleam@string:join(_pipe@1, <<"\n"/utf8>>).

-file("src/qrkit/render/ascii.gleam", 10).
?DOC(" Render the QR code using double-width full blocks.\n").
-spec to_string(qrkit:qr_code()) -> binary().
to_string(Qr) ->
    render(Qr, <<"██"/utf8>>, <<"  "/utf8>>).

-file("src/qrkit/render/ascii.gleam", 15).
?DOC(" Render the QR code with inverted colours.\n").
-spec with_inverse(qrkit:qr_code()) -> binary().
with_inverse(Qr) ->
    render(Qr, <<"  "/utf8>>, <<"██"/utf8>>).