-module(chrobot@internal@utils). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([add_optional/3, err/1, warn/1, alert_encode_dynamic/1, hint/1, info/1, start_progress/1, set_progress/2, stop_progress/1, show_cmd/1, try_call_with_subject/4, get_time_ms/0]). -spec term_supports_color() -> boolean(). term_supports_color() -> case gleam_erlang_ffi:get_env(<<"TERM"/utf8>>) of {ok, <<"dumb"/utf8>>} -> false; _ -> true end. -spec add_optional( list({binary(), gleam@json:json()}), gleam@option:option(VJM), fun((VJM) -> {binary(), gleam@json:json()}) ) -> list({binary(), gleam@json:json()}). add_optional(Prop_encoders, Value, Callback) -> case Value of {some, A} -> [Callback(A) | Prop_encoders]; none -> Prop_encoders end. -spec align(binary()) -> binary(). align(Content) -> gleam@string:replace(Content, <<"\n"/utf8>>, <<"\n "/utf8>>). -spec err(binary()) -> nil. err(Content) -> case term_supports_color() of true -> _pipe@4 = (<<<<(begin _pipe = <<"[-_-] ERR! "/utf8>>, _pipe@1 = gleam_community@ansi:bg_red(_pipe), _pipe@2 = gleam_community@ansi:white(_pipe@1), gleam_community@ansi:bold(_pipe@2) end)/binary, " "/utf8>>/binary, (begin _pipe@3 = align(Content), gleam_community@ansi:red(_pipe@3) end)/binary>>), gleam@io:println(_pipe@4); false -> gleam@io:println(<<"[-_-] ERR! "/utf8, Content/binary>>) end. -spec warn(binary()) -> nil. warn(Content) -> case term_supports_color() of true -> _pipe@4 = (<<<<(begin _pipe = <<"[O_O] HEY! "/utf8>>, _pipe@1 = gleam_community@ansi:bg_yellow(_pipe), _pipe@2 = gleam_community@ansi:black(_pipe@1), gleam_community@ansi:bold(_pipe@2) end)/binary, " "/utf8>>/binary, (begin _pipe@3 = align(Content), gleam_community@ansi:yellow(_pipe@3) end)/binary>>), gleam@io:println(_pipe@4); false -> gleam@io:println(<<"[O_O] HEY! "/utf8, Content/binary>>) end. -spec alert_encode_dynamic(any()) -> gleam@json:json(). alert_encode_dynamic(Input_value) -> warn( <<"You passed a dymamic value to a protocol encoder! Dynamic values cannot be encoded, the value will be set to null instead. This is unlikely to be intentional, you should fix that part of your code."/utf8>> ), gleam@io:println( <<"The value was: "/utf8, (gleam@string:inspect(Input_value))/binary>> ), gleam@json:null(). -spec hint(binary()) -> nil. hint(Content) -> case term_supports_color() of true -> _pipe@4 = (<<<<(begin _pipe = <<"[>‿0] HINT "/utf8>>, _pipe@1 = gleam_community@ansi:bg_cyan(_pipe), _pipe@2 = gleam_community@ansi:black(_pipe@1), gleam_community@ansi:bold(_pipe@2) end)/binary, " "/utf8>>/binary, (begin _pipe@3 = align(Content), gleam_community@ansi:cyan(_pipe@3) end)/binary>>), gleam@io:println(_pipe@4); false -> gleam@io:println(<<"[>‿0] HINT "/utf8, Content/binary>>) end. -spec info(binary()) -> nil. info(Content) -> case term_supports_color() of true -> _pipe@4 = (<<<<(begin _pipe = <<"[0‿0] INFO "/utf8>>, _pipe@1 = gleam_community@ansi:bg_white(_pipe), _pipe@2 = gleam_community@ansi:black(_pipe@1), gleam_community@ansi:bold(_pipe@2) end)/binary, " "/utf8>>/binary, (begin _pipe@3 = align(Content), gleam_community@ansi:white(_pipe@3) end)/binary>>), gleam@io:println(_pipe@4); false -> gleam@io:println(<<"[0‿0] INFO "/utf8, Content/binary>>) end. -spec start_progress(binary()) -> gleam@option:option(spinner:spinner()). start_progress(Text) -> case term_supports_color() of true -> Spinner = begin _pipe = spinner:new(Text), _pipe@1 = spinner:with_colour( _pipe, fun gleam_community@ansi:blue/1 ), spinner:start(_pipe@1) end, {some, Spinner}; false -> gleam@io:println(<<"Progress: "/utf8, Text/binary>>), none end. -spec set_progress(gleam@option:option(spinner:spinner()), binary()) -> nil. set_progress(Spinner, Text) -> case Spinner of {some, Spinner@1} -> spinner:set_text(Spinner@1, Text); none -> gleam@io:println(<<"Progress: "/utf8, Text/binary>>), nil end. -spec stop_progress(gleam@option:option(spinner:spinner())) -> nil. stop_progress(Spinner) -> case Spinner of {some, Spinner@1} -> spinner:stop(Spinner@1); none -> nil end. -spec show_cmd(binary()) -> nil. show_cmd(Content) -> case term_supports_color() of true -> _pipe = (<<<<<<<<"\n "/utf8, (gleam_community@ansi:dim(<<"$"/utf8>>))/binary>>/binary, " "/utf8>>/binary, (gleam_community@ansi:bold(Content))/binary>>/binary, "\n"/utf8>>), gleam@io:println(_pipe); false -> gleam@io:println( <<<<"\n $ "/utf8, Content/binary>>/binary, "\n"/utf8>> ) end. -spec try_call_with_subject( gleam@erlang@process:subject(VKA), fun((gleam@erlang@process:subject(VKC)) -> VKA), gleam@erlang@process:subject(VKC), integer() ) -> {ok, VKC} | {error, gleam@erlang@process:call_error(VKC)}. try_call_with_subject(Subject, Make_request, Reply_subject, Timeout) -> Monitor = gleam@erlang@process:monitor_process( gleam@erlang@process:subject_owner(Subject) ), gleam@erlang@process:send(Subject, Make_request(Reply_subject)), Result = begin _pipe = gleam_erlang_ffi:new_selector(), _pipe@1 = gleam@erlang@process:selecting( _pipe, Reply_subject, fun(Field@0) -> {ok, Field@0} end ), _pipe@2 = gleam@erlang@process:selecting_process_down( _pipe@1, Monitor, fun(Down) -> {error, {callee_down, erlang:element(3, Down)}} end ), gleam_erlang_ffi:select(_pipe@2, Timeout) end, gleam_erlang_ffi:demonitor(Monitor), case Result of {error, nil} -> {error, call_timeout}; {ok, Res} -> Res end. -spec get_time_ms() -> integer(). get_time_ms() -> chrobot_ffi:get_time_ms().