diff --git a/lib/chessh/ssh/client/client.ex b/lib/chessh/ssh/client/client.ex index 10dd3b2..f160ba5 100644 --- a/lib/chessh/ssh/client/client.ex +++ b/lib/chessh/ssh/client/client.ex @@ -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 diff --git a/lib/chessh/ssh/client/menu.ex b/lib/chessh/ssh/client/menu.ex index c207872..946928e 100644 --- a/lib/chessh/ssh/client/menu.ex +++ b/lib/chessh/ssh/client/menu.ex @@ -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