Current section
Files
Jump to
Current section
Files
src/styles.erl
-module(styles).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([foreground/2, background/2, bold/2, underline/2, italic/2, dim/2, blink/2, invert/2, render/2, strikethrough/2, new_style/0]).
-export_type([style/0]).
-type style() :: {style, gleam@dict:dict(binary(), binary())}.
-spec insert(style(), binary(), binary()) -> style().
insert(Style, Key, Value) ->
{style, gleam@dict:insert(erlang:element(2, Style), Key, Value)}.
-spec get(style(), binary()) -> binary().
get(Style, Key) ->
case gleam@dict:get(erlang:element(2, Style), Key) of
{ok, Value} ->
Value;
{error, Err} ->
gleam@io:debug(Key),
gleam@io:debug(Err),
erlang:error(#{gleam_error => panic,
message => <<"not defined"/utf8>>,
module => <<"styles"/utf8>>,
function => <<"get"/utf8>>,
line => 106})
end.
-spec render_boolean(style(), binary(), binary()) -> binary().
render_boolean(Style, Key, True_value) ->
case get(Style, Key) of
<<"true"/utf8>> ->
True_value;
_ ->
<<""/utf8>>
end.
-spec set_boolean_style(style(), binary(), boolean()) -> style().
set_boolean_style(Style, Key, Value) ->
case Value of
true ->
insert(Style, Key, <<"true"/utf8>>);
false ->
insert(Style, Key, <<"false"/utf8>>)
end.
-spec foreground(style(), types:color()) -> style().
foreground(Style, Color) ->
insert(Style, <<"foreground"/utf8>>, erlang:element(2, Color)).
-spec render_color(style(), boolean()) -> binary().
render_color(Style, Is_foreground) ->
Key = (case Is_foreground of
true ->
<<"foreground"/utf8>>;
false ->
<<"background"/utf8>>
end),
Ansi_code = utils:get_ansi_code({color24, get(Style, Key)}),
case Ansi_code of
<<"clear"/utf8>> ->
<<"\x{001b}[0m"/utf8>>;
_ ->
case Is_foreground of
true ->
<<"\x{001b}[38;2;"/utf8, Ansi_code/binary>>;
false ->
<<"\x{001b}[48;2;"/utf8, Ansi_code/binary>>
end
end.
-spec background(style(), types:color()) -> style().
background(Style, Color) ->
insert(Style, <<"background"/utf8>>, erlang:element(2, Color)).
-spec bold(style(), boolean()) -> style().
bold(Style, Value) ->
set_boolean_style(Style, <<"bold"/utf8>>, Value).
-spec underline(style(), boolean()) -> style().
underline(Style, Value) ->
set_boolean_style(Style, <<"underline"/utf8>>, Value).
-spec italic(style(), boolean()) -> style().
italic(Style, Value) ->
set_boolean_style(Style, <<"italic"/utf8>>, Value).
-spec dim(style(), boolean()) -> style().
dim(Style, Value) ->
set_boolean_style(Style, <<"dim"/utf8>>, Value).
-spec blink(style(), boolean()) -> style().
blink(Style, Value) ->
set_boolean_style(Style, <<"blink"/utf8>>, Value).
-spec invert(style(), boolean()) -> style().
invert(Style, Value) ->
set_boolean_style(Style, <<"invert"/utf8>>, Value).
-spec render(style(), binary()) -> binary().
render(Style, Value) ->
Builder = begin
_pipe = gleam@string_builder:new(),
_pipe@1 = gleam@string_builder:append(_pipe, render_color(Style, true)),
_pipe@2 = gleam@string_builder:append(
_pipe@1,
render_color(Style, false)
),
_pipe@3 = gleam@string_builder:append(
_pipe@2,
render_boolean(Style, <<"bold"/utf8>>, <<"\x{001b}[1m"/utf8>>)
),
_pipe@4 = gleam@string_builder:append(
_pipe@3,
render_boolean(Style, <<"dim"/utf8>>, <<"\x{001b}[2m"/utf8>>)
),
_pipe@5 = gleam@string_builder:append(
_pipe@4,
render_boolean(Style, <<"italic"/utf8>>, <<"\x{001b}[3m"/utf8>>)
),
_pipe@6 = gleam@string_builder:append(
_pipe@5,
render_boolean(Style, <<"underline"/utf8>>, <<"\x{001b}[4m"/utf8>>)
),
_pipe@7 = gleam@string_builder:append(
_pipe@6,
render_boolean(Style, <<"blink"/utf8>>, <<"\x{001b}[5m"/utf8>>)
),
_pipe@8 = gleam@string_builder:append(
_pipe@7,
render_boolean(Style, <<"invert"/utf8>>, <<"\x{001b}[7m"/utf8>>)
),
_pipe@9 = gleam@string_builder:append(
_pipe@8,
render_boolean(
Style,
<<"strikethrough"/utf8>>,
<<"\x{001b}[9m"/utf8>>
)
),
_pipe@10 = gleam@string_builder:append(_pipe@9, Value),
gleam@string_builder:append(_pipe@10, <<"\x{001b}[0m"/utf8>>)
end,
gleam@string_builder:to_string(Builder).
-spec strikethrough(style(), boolean()) -> style().
strikethrough(Style, Value) ->
set_boolean_style(Style, <<"strikethrough"/utf8>>, Value).
-spec new_style() -> style().
new_style() ->
_pipe = {style, gleam@dict:new()},
_pipe@1 = foreground(_pipe, {color24, <<"clear"/utf8>>}),
_pipe@2 = background(_pipe@1, {color24, <<"clear"/utf8>>}),
_pipe@3 = bold(_pipe@2, false),
_pipe@4 = underline(_pipe@3, false),
_pipe@5 = italic(_pipe@4, false),
_pipe@6 = dim(_pipe@5, false),
_pipe@7 = blink(_pipe@6, false),
_pipe@8 = invert(_pipe@7, false),
strikethrough(_pipe@8, false).