Skip to main content

src/gleamdoc@output.erl

-module(gleamdoc@output).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleamdoc/output.gleam").
-export([text/2, error/1]).
-export_type([style/0]).

-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 styling for human-readable command output.\n").

-type style() :: documentation | search_results | packages | index_result.

-file("src/gleamdoc/output.gleam", 122).
-spec ansi(binary(), binary()) -> binary().
ansi(Code, Text) ->
    <<<<<<<<"\x{001b}["/utf8, Code/binary>>/binary, "m"/utf8>>/binary,
            Text/binary>>/binary,
        "\x{001b}[0m"/utf8>>.

-file("src/gleamdoc/output.gleam", 102).
-spec green(binary()) -> binary().
green(Text) ->
    ansi(<<"32"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 94).
-spec dim(binary()) -> binary().
dim(Text) ->
    ansi(<<"2"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 90).
-spec bold(binary()) -> binary().
bold(Text) ->
    ansi(<<"1"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 75).
-spec style_package_line(binary()) -> binary().
style_package_line(Line) ->
    case gleam@string:split_once(Line, <<" "/utf8>>) of
        {ok, {Name, Details}} ->
            <<<<(bold(Name))/binary, " "/utf8>>/binary, (dim(Details))/binary>>;

        {error, _} ->
            Line
    end.

-file("src/gleamdoc/output.gleam", 29).
-spec style_lines(binary(), fun((binary()) -> binary())) -> binary().
style_lines(Text, Style) ->
    _pipe = Text,
    _pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>),
    _pipe@2 = gleam@list:map(_pipe@1, Style),
    gleam@string:join(_pipe@2, <<"\n"/utf8>>).

-file("src/gleamdoc/output.gleam", 82).
-spec style_index(binary()) -> binary().
style_index(Text) ->
    case gleam@string:split_once(Text, <<"\n"/utf8>>) of
        {ok, {Summary, Packages}} ->
            <<<<(green(Summary))/binary, "\n"/utf8>>/binary,
                (style_lines(Packages, fun style_package_line/1))/binary>>;

        {error, _} ->
            green(Text)
    end.

-file("src/gleamdoc/output.gleam", 118).
-spec cyan(binary()) -> binary().
cyan(Text) ->
    ansi(<<"36"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 114).
-spec magenta(binary()) -> binary().
magenta(Text) ->
    ansi(<<"35"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 106).
-spec yellow(binary()) -> binary().
yellow(Text) ->
    ansi(<<"33"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 110).
-spec blue(binary()) -> binary().
blue(Text) ->
    ansi(<<"34"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 64).
-spec style_kind(binary()) -> binary().
style_kind(Kind) ->
    case Kind of
        <<"module"/utf8>> ->
            blue(Kind);

        <<"fn"/utf8>> ->
            green(Kind);

        <<"const"/utf8>> ->
            yellow(Kind);

        <<"type"/utf8>> ->
            magenta(Kind);

        <<"alias"/utf8>> ->
            magenta(Kind);

        <<"variant"/utf8>> ->
            cyan(Kind);

        _ ->
            Kind
    end.

-file("src/gleamdoc/output.gleam", 57).
-spec style_search_line(binary()) -> binary().
style_search_line(Line) ->
    case gleam@string:split_once(Line, <<"  "/utf8>>) of
        {ok, {Kind, Name}} ->
            <<<<(style_kind(Kind))/binary, "  "/utf8>>/binary,
                (bold(Name))/binary>>;

        {error, _} ->
            Line
    end.

-file("src/gleamdoc/output.gleam", 50).
-spec is_metadata(binary()) -> boolean().
is_metadata(Line) ->
    ((gleam_stdlib:string_starts_with(Line, <<"package: "/utf8>>) orelse gleam_stdlib:string_starts_with(
        Line,
        <<"targets: "/utf8>>
    ))
    orelse gleam_stdlib:string_starts_with(Line, <<"type: "/utf8>>))
    orelse gleam_stdlib:string_starts_with(Line, <<"source: "/utf8>>).

-file("src/gleamdoc/output.gleam", 36).
-spec style_documentation_line(binary()) -> binary().
style_documentation_line(Line) ->
    case {gleam_stdlib:string_starts_with(Line, <<"pub "/utf8>>),
        Line =:= <<"module"/utf8>>,
        gleam_stdlib:string_starts_with(Line, <<"deprecated: "/utf8>>),
        is_metadata(Line)} of
        {true, _, _, _} ->
            bold(cyan(Line));

        {_, true, _, _} ->
            bold(cyan(Line));

        {_, _, true, _} ->
            yellow(Line);

        {_, _, _, true} ->
            dim(Line);

        {_, _, _, _} ->
            Line
    end.

-file("src/gleamdoc/output.gleam", 15).
?DOC(" Applies ANSI styling to human-readable output.\n").
-spec text(binary(), style()) -> binary().
text(Text, Style) ->
    case Style of
        documentation ->
            style_lines(Text, fun style_documentation_line/1);

        search_results ->
            style_lines(Text, fun style_search_line/1);

        packages ->
            style_lines(Text, fun style_package_line/1);

        index_result ->
            style_index(Text)
    end.

-file("src/gleamdoc/output.gleam", 98).
-spec red(binary()) -> binary().
red(Text) ->
    ansi(<<"31"/utf8>>, Text).

-file("src/gleamdoc/output.gleam", 25).
?DOC(" Applies ANSI error styling to a message.\n").
-spec error(binary()) -> binary().
error(Message) ->
    red(Message).