Skip to content

Commit

Permalink
Moved custom REPL code into own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Aug 4, 2023
1 parent b31549e commit a6d1551
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 77 deletions.
82 changes: 5 additions & 77 deletions src/rebar3_lfe_prv_repl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 2,16 @@

-export([init/1,
do/1,
format_error/1,
make_banner/0,
start/0, start/1]).
format_error/1]).

-include("rebar3_lfe.hrl").

-define(PROVIDER, repl).
-define(DEPS, [compile]).
-define(DEFAULT_CFG,
#{nobanner => false,
version => get_lfe_version(),
quit_message => get_quit_message(),
version => rebar3_lfe_repl:lfe_version(),
quit_message => rebar3_lfe_repl:quit_message(),
banner_template => ""
}).
%% ===================================================================
Expand Down Expand Up @@ -96,8 94,6 @@ info(Description) ->
repl(State) ->
rebar_api:debug("\tStarting LFE REPL ...", []),
rebar_paths:set_paths([deps, plugins], State),
rebar_api:debug("\t\tPlain args: ~p", [init:get_plain_arguments()]),
rebar_api:debug("\t\tSetting shell args ...", []),
LfeCfg = rebar_state:get(State, lfe, []),
rebar_api:debug("\t\tLFECfg: ~p", [LfeCfg]),
ReplCfg = make_config(LfeCfg),
Expand All @@ -114,78 110,10 @@ set_shell_args(ReplCfg) ->
OTPRelease = erlang:system_info(otp_release),
if OTPRelease >= "26" ->
rebar_api:debug("\t\tRunning newer Erlang ...", []),
[{shell_args, #{initial_shell => {rebar3_lfe_prv_repl,start,[ReplCfg]}}}];
[{shell_args, #{initial_shell => {rebar3_lfe_repl,start,[ReplCfg]}}}];
true ->
[{shell_args, ['tty_sl -c -e',{rebar3_lfe_prv_repl,start,[ReplCfg]}]}]
[{shell_args, ['tty_sl -c -e',{rebar3_lfe_repl,start,[ReplCfg]}]}]
end.

make_config(Parsed) ->
maps:merge(?DEFAULT_CFG, maps:from_list(proplists:get_value(repl, Parsed, []))).

start(#{nobanner := NoBnr, version := Vsn, quit_message := QuitMsg, banner_template := BnrTmpl}) ->
case BnrTmpl of
"" ->
start(NoBnr, Vsn, QuitMsg);
_ ->
Banner = make_banner(BnrTmpl, Vsn, QuitMsg),
start(NoBnr, Banner)
end.

start() ->
Vsn = get_lfe_version(),
QuitMsg = get_quit_message(),
start(false, Vsn, QuitMsg).

start(NoBnr, Vsn, QuitMsg) ->
Banner = make_banner(Vsn, QuitMsg),
start(NoBnr, Banner).

start(NoBnr, Bnr) ->
case NoBnr of
true ->
ok;
_ ->
ok = io:put_chars(erlang:whereis(user), Bnr)
end,
lfe_shell:start().

%% XXX When the banner functions are public, delete these:
%% * https://github.com/lfe/lfe/issues/476

%% Coloured strings for the LFE banner, red, green, yellow and blue.
-define(RED(Str), "\e[31m" Str "\e[0m").
-define(GRN(Str), "\e[1;32m" Str "\e[0m").
-define(YLW(Str), "\e[1;33m" Str "\e[0m").
-define(BLU(Str), "\e[1;34m" Str "\e[0m").
-define(BOLD(Str), "\e[1m" Str "\e[0m").

make_banner(Vsn, QuitMsg) ->
?GRN(" ..-~") ?YLW(".~_") ?GRN("~---..") "\n"
?GRN(" ( ") ?YLW("\\\\") ?GRN(" )") " | A Lisp-2 on the Erlang VM\n"
?GRN(" |`-.._") ?YLW("/") ?GRN("_") ?YLW("\\\\") ?GRN("_.-':") " | Type " ?GRN("(help)") " for usage info.\n"
?GRN(" | ") ?RED("g") ?GRN(" |_ \\") " |\n"
?GRN(" | ") ?RED("n") ?GRN(" | |") " | Docs: " ?BLU("http://docs.lfe.io/") "\n"
?GRN(" | ") ?RED("a") ?GRN(" / /") " | Source: " ?BLU("http://github.com/lfe/lfe") "\n"
?GRN(" \\ ") ?RED("l") ?GRN(" |_/") " |\n"
?GRN(" \\ ") ?RED("r") ?GRN(" /") " | LFE v"
Vsn " " QuitMsg "\n"
?GRN(" `-") ?RED("E") ?GRN("___.-'") "\n\n".
make_banner() ->
make_banner(get_lfe_version()).
make_banner(Vsn) ->
make_banner(Vsn, get_quit_message()).
make_banner(Tmpl, Vsn, QuitMsg) ->
[io_lib:format(Tmpl, [Vsn, QuitMsg])].
get_quit_message() ->
%% We can update this later to check for env variable settings for
%% shells that require a different control character to abort, such
%% as jlfe.
"(abort with ^C; enter JCL with ^G)".
get_lfe_version() ->
{ok, [App]} = file:consult(code:where_is_file("lfe.app")),
proplists:get_value(vsn, element(3, App)).
73 changes: 73 additions & 0 deletions src/rebar3_lfe_repl.erl
Original file line number Diff line number Diff line change
@@ -0,0 1,73 @@
-module(rebar3_lfe_repl).

-export([start/0, start/1]).

-export([lfe_version/0, quit_message/0]).

start(#{nobanner := NoBnr, version := Vsn, quit_message := QuitMsg, banner_template := BnrTmpl}) ->
case BnrTmpl of
"" ->
start(NoBnr, Vsn, QuitMsg);
_ ->
Banner = banner(BnrTmpl, Vsn, QuitMsg),
start(NoBnr, Banner)
end.

start() ->
Vsn = lfe_version(),
QuitMsg = quit_message(),
start(false, Vsn, QuitMsg).

start(NoBnr, Vsn, QuitMsg) ->
Banner = banner(Vsn, QuitMsg),
start(NoBnr, Banner).

start(NoBnr, Bnr) ->
case NoBnr of
true ->
ok;
_ ->
ok = io:put_chars(erlang:whereis(user), Bnr)
end,
lfe_shell:start().

%% XXX When the banner functions are public, delete everything below this line:
%% * https://github.com/lfe/lfe/issues/476

%% Coloured strings for the LFE banner, red, green, yellow and blue.
-define(RED(Str), "\e[31m" Str "\e[0m").
-define(GRN(Str), "\e[1;32m" Str "\e[0m").
-define(YLW(Str), "\e[1;33m" Str "\e[0m").
-define(BLU(Str), "\e[1;34m" Str "\e[0m").
-define(BOLD(Str), "\e[1m" Str "\e[0m").

%�nner() ->
%% banner(lfe_version()).

%�nner(Vsn) ->
%% banner(Vsn, quit_message()).

banner(Vsn, QuitMsg) ->
?GRN(" ..-~") ?YLW(".~_") ?GRN("~---..") "\n"
?GRN(" ( ") ?YLW("\\\\") ?GRN(" )") " | A Lisp-2 on the Erlang VM\n"
?GRN(" |`-.._") ?YLW("/") ?GRN("_") ?YLW("\\\\") ?GRN("_.-':") " | Type " ?GRN("(help)") " for usage info.\n"
?GRN(" | ") ?RED("g") ?GRN(" |_ \\") " |\n"
?GRN(" | ") ?RED("n") ?GRN(" | |") " | Docs: " ?BLU("http://docs.lfe.io/") "\n"
?GRN(" | ") ?RED("a") ?GRN(" / /") " | Source: " ?BLU("http://github.com/lfe/lfe") "\n"
?GRN(" \\ ") ?RED("l") ?GRN(" |_/") " |\n"
?GRN(" \\ ") ?RED("r") ?GRN(" /") " | LFE v"
Vsn " " QuitMsg "\n"
?GRN(" `-") ?RED("E") ?GRN("___.-'") "\n\n".
banner(Tmpl, Vsn, QuitMsg) ->
[io_lib:format(Tmpl, [Vsn, QuitMsg])].
quit_message() ->
%% We can update this later to check for env variable settings for
%% shells that require a different control character to abort, such
%% as jlfe.
"(abort with ^C; enter JCL with ^G)".
lfe_version() ->
{ok, [App]} = file:consult(code:where_is_file("lfe.app")),
proplists:get_value(vsn, element(3, App)).

0 comments on commit a6d1551

Please sign in to comment.