Selectable menu

This commit is contained in:
Logan Hunt 2023-01-13 17:48:00 -07:00
parent b1b62f154a
commit 9f56b735c6
No known key found for this signature in database
GPG Key ID: 8AC6A4B840C0EC49
2 changed files with 41 additions and 19 deletions

View File

@ -24,12 +24,19 @@ defmodule Chessh.SSH.Client do
@impl true @impl true
def init([%State{} = state]) do def init([%State{} = state]) do
{:ok, screen_pid} = send(self(), {:set_screen_process, Chessh.SSH.Client.Menu, %Chessh.SSH.Client.Menu.State{}})
GenServer.start_link(Chessh.SSH.Client.Board, [ {:ok, state}
%Chessh.SSH.Client.Board.State{client_pid: self()} end
])
{:ok, %{state | screen_processes: [screen_pid]}} @impl true
def handle_info(
{:set_screen_process, module, screen_state},
%State{width: width, height: height, screen_processes: screen_processes} = state
) do
{:ok, new_screen_pid} = GenServer.start_link(module, [%{screen_state | client_pid: self()}])
send(new_screen_pid, {:render, width, height})
{:noreply, %{state | screen_processes: [new_screen_pid | screen_processes]}}
end end
@impl true @impl true
@ -60,15 +67,22 @@ defmodule Chessh.SSH.Client do
def handle( def handle(
{:data, data}, {:data, data},
%State{width: width, height: height, screen_processes: [screen_pid | _]} = state %State{width: width, height: height, screen_processes: [screen_pid | rest_processes]} =
state
) do ) do
action = keymap(data) case keymap(data) do
:quit ->
{:stop, :normal, state}
if action == :quit do :previous_screen ->
{:stop, :normal, state} Logger.debug(inspect(rest_processes))
else Process.exit(screen_pid, :kill)
send(screen_pid, {:input, width, height, action})
{:noreply, state} {:noreply, %State{state | screen_processes: rest_processes}}
action ->
send(screen_pid, {:input, width, height, action})
{:noreply, state}
end end
end end
@ -109,6 +123,8 @@ defmodule Chessh.SSH.Client do
# Exit keys - C-c and C-d # Exit keys - C-c and C-d
<<3>> -> :quit <<3>> -> :quit
<<4>> -> :quit <<4>> -> :quit
# C-b
<<2>> -> :previous_screen
# Arrow keys # Arrow keys
"\e[A" -> :up "\e[A" -> :up
"\e[B" -> :down "\e[B" -> :down

View File

@ -24,9 +24,11 @@ defmodule Chessh.SSH.Client.Menu do
end end
@options [ @options [
{"Option 1", {}}, {"Start A Game", {Chessh.SSH.Client.Board, %Chessh.SSH.Client.Board.State{}}},
{"Option 2", {}}, {"Join A Game", {}},
{"Option 3", {}} {"My Games", {}},
{"Settings", {}},
{"Help", {}}
] ]
def input(width, height, action, %State{client_pid: client_pid, selected: selected} = state) do def input(width, height, action, %State{client_pid: client_pid, selected: selected} = state) do
@ -41,15 +43,19 @@ defmodule Chessh.SSH.Client.Menu do
:down -> :down ->
%State{state | selected: Utils.wrap_around(selected, 1, length(@options))} %State{state | selected: Utils.wrap_around(selected, 1, length(@options))}
# :return -> :return ->
# {_, new_state} = Enum.at(@options, selected) {_option, {module, state}} = Enum.at(@options, selected)
# new_state send(client_pid, {:set_screen_process, module, state})
state
_ -> _ ->
state state
end end
send(client_pid, {:send_to_ssh, render_state(width, height, new_state)}) if !(action == :return) do
send(client_pid, {:send_to_ssh, render_state(width, height, new_state)})
end
new_state new_state
end end