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

View File

@ -24,9 +24,11 @@ defmodule Chessh.SSH.Client.Menu do
end
@options [
{"Option 1", {}},
{"Option 2", {}},
{"Option 3", {}}
{"Start A Game", {Chessh.SSH.Client.Board, %Chessh.SSH.Client.Board.State{}}},
{"Join A Game", {}},
{"My Games", {}},
{"Settings", {}},
{"Help", {}}
]
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 ->
%State{state | selected: Utils.wrap_around(selected, 1, length(@options))}
# :return ->
# {_, new_state} = Enum.at(@options, selected)
# new_state
:return ->
{_option, {module, state}} = Enum.at(@options, selected)
send(client_pid, {:set_screen_process, module, state})
state
_ ->
state
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
end