Skip to content

Instantly share code, notes, and snippets.

@muromec
Last active September 13, 2023 07:44
Show Gist options
  • Save muromec/b94e528c78978ef68517304caca9a664 to your computer and use it in GitHub Desktop.
Save muromec/b94e528c78978ef68517304caca9a664 to your computer and use it in GitHub Desktop.
{application, cdash,
[{description, "Dashboard for something"},
{vsn, "0.1.0"},
{registered, []},
{applications,
[kernel,
stdlib,
cecho,
inets
]},
{env,[]},
{modules, [cdash]},
{maintainers, []},
{licenses, ["BSD"]},
{links, []}
]}.
-module(cdash).
-export([start/0]).
-define(MAX_HLINE, 300).
-include_lib("cecho/include/cecho.hrl").
start() ->
init_screen(),
ViewPid = erlang:spawn(fun() -> draw() end),
StorePid = erlang:spawn(fun() -> store(ViewPid) end),
erlang:spawn(fun() -> control(StorePid) end),
ok.
init_screen() ->
process_flag(trap_exit, true),
application:start(cecho),
ok = cecho:cbreak(),
ok = cecho:noecho(),
ok = cecho:curs_set(?ceCURS_INVISIBLE),
ok = cecho:keypad(?ceSTDSCR, true),
ok.
-record(cursor, { y, x }).
-record(state, { number, cursor }).
store(ParentPid) ->
Cursor = #cursor { y= 20 , x = 10 },
State = #state { number = 10, cursor = Cursor },
store(ParentPid, State).
store(ParentPid, State) ->
Cursor = State#state.cursor,
receive
{get_state, Pid} ->
Pid ! {ok, state, State},
NewState = State;
{move, left} ->
NewState = State#state{ cursor = Cursor#cursor{ x = Cursor#cursor.x - 1}};
{move, right} ->
NewState = State#state{ cursor = Cursor#cursor{ x = Cursor#cursor.x + 1}};
{move, up} ->
NewState = State#state{ cursor = Cursor#cursor{ y = Cursor#cursor.y + 1}};
{move, down} ->
NewState = State#state{ cursor = Cursor#cursor{ y = Cursor#cursor.y - 1}}
after 1000 ->
NewState = State#state{ number = State#state.number - 1}
end,
if
NewState#state.number == 0 ->
do_exit(),
exit(self(), normal);
true -> ok
end,
ParentPid ! {update_state, NewState},
store(ParentPid, NewState).
draw_screen_borders() ->
{Y, X} = cecho:getmaxyx(),
draw_borders(0, 0, Y, X).
draw_borders(Y0, X0, H, W) ->
Y1 = Y0 + H,
X1 = X0 + W,
cecho:move(Y0, X0),
cecho:hline($-, W),
cecho:vline($|, H),
cecho:move(Y0, X1-1),
cecho:vline($|, H),
cecho:move(Y1-1, X0),
cecho:hline($-, W),
cecho:move(Y0, X0),
cecho:addch($+),
cecho:move(Y1-1, X0),
cecho:addch($+),
cecho:move(Y0, X1-1),
cecho:addch($+),
cecho:move(Y1-1, X1-1),
cecho:addch($+),
cecho:move(2, 4),
cecho:addstr(io_lib:format("borders ~p ~p to ~p ~p", [Y0, X0, Y1, X1])).
get_state() ->
State = receive
{update_state, OkState} -> OkState
end,
{ok, State}.
draw() ->
{ok, State} = get_state(),
Number = State#state.number,
cecho:erase(),
draw_screen_borders(),
draw_borders(8, 8, 5, 17),
cecho:move(10, 10),
cecho:addstr("Countdown: "),
cecho:addstr(io_lib:format("~p", [Number])),
{cursor, Y, X} = State#state.cursor,
draw_borders(Y - 2, X - 2, 5, 25),
cecho:move(Y, X),
cecho:addstr(io_lib:format("Cursor: ~p", [State#state.cursor])),
cecho:refresh(),
draw().
move_cursor(StorePid, Direction) ->
StorePid ! {move, Direction}.
control(StorePid) ->
P = cecho:getch(),
case P of
$q -> do_exit();
3 -> do_exit(); %Ctrl-C
261 -> move_cursor(StorePid, right), control(StorePid);
260 -> move_cursor(StorePid, left), control(StorePid);
258 -> move_cursor(StorePid, up), control(StorePid);
259 -> move_cursor(StorePid, down), control(StorePid);
_ ->
io:format("controle ~p", [P]),
control(StorePid)
end.
do_exit() ->
cecho:curs_set(?ceCURS_NORMAL),
application:stop(cecho),
io:format("exit~n"),
halt().
{erl_opts, [fail_on_warning,
debug_info,
{platform_define, "^(R14|R15|R16B|17)", 'random_module_available'}
]}.
{escript_emu_args, "%%! -hidden -noinput +A 20 +Bc"}.
{ deps, [
{cecho, ".*",
{git, "https://github.com/mazenharake/cecho.git", {tag, "0.5.3"}}
}
]
}.
#!/bin/sh
set -e
# patch shit
sed '/erl_interface.h/d' -i _build/default/lib/cecho/c_src/cecho.c
sed 's,^.*port_compiler.*$,pc,' -i _build/default/lib/cecho/rebar.config
rebar3 compile
#source ./.env
env TOKEN=$TOKEN erl -noinput -hidden -eval "cdash:start()." \
-pa _build/default/lib/cdash/ebin \
-pa _build/default/lib/cecho/ebin
# start.sh
# rebar.config
# src/cdash.app.src
# src/cdash.erl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment