summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client
diff options
context:
space:
mode:
authorSimponic <loganhunt@simponic.xyz>2023-01-11 14:21:48 -0700
committerSimponic <loganhunt@simponic.xyz>2023-01-11 14:21:48 -0700
commit628c6d95a32ba0dea67ab046a480ddfca7432c9b (patch)
tree4405ecd2fbee6027897706884d70ac5dd5de418b /lib/chessh/ssh/client
parent2ce03d4796ad54f332c70ff88f0a9f3a164bbc4a (diff)
downloadchessh-628c6d95a32ba0dea67ab046a480ddfca7432c9b.tar.gz
chessh-628c6d95a32ba0dea67ab046a480ddfca7432c9b.zip
Done moving menu to genserver architecture
Diffstat (limited to 'lib/chessh/ssh/client')
-rw-r--r--lib/chessh/ssh/client/client.ex79
-rw-r--r--lib/chessh/ssh/client/menu.ex67
-rw-r--r--lib/chessh/ssh/client/screen.ex25
3 files changed, 102 insertions, 69 deletions
diff --git a/lib/chessh/ssh/client/client.ex b/lib/chessh/ssh/client/client.ex
index a5f7bec..216af71 100644
--- a/lib/chessh/ssh/client/client.ex
+++ b/lib/chessh/ssh/client/client.ex
@@ -1,22 +1,18 @@
defmodule Chessh.SSH.Client do
alias IO.ANSI
- alias Chessh.SSH.Client.Menu
require Logger
use GenServer
@clear_codes [
ANSI.clear(),
- ANSI.reset(),
ANSI.home()
]
- @max_terminal_width 255
- @max_terminal_height 127
-
- @terminal_bad_dim_msg [
- @clear_codes | "The dimensions of your terminal are not within in the valid range"
- ]
+ @min_terminal_width 64
+ @min_terminal_height 31
+ @max_terminal_width 200
+ @max_terminal_height 100
defmodule State do
defstruct tui_pid: nil,
@@ -27,10 +23,10 @@ defmodule Chessh.SSH.Client do
end
@impl true
- def init([%State{tui_pid: tui_pid} = state]) do
+ def init([%State{} = state]) do
{:ok, screen_pid} =
GenServer.start_link(Chessh.SSH.Client.Menu, [
- %Chessh.SSH.Client.Menu.State{tui_pid: tui_pid}
+ %Chessh.SSH.Client.Menu.State{client_pid: self()}
])
{:ok, %{state | screen_processes: [screen_pid]}}
@@ -76,27 +72,36 @@ defmodule Chessh.SSH.Client do
end
end
- # def handle(
- # {:refresh, },
- # %State{screen_processes: [screen_pid | _] = screen_processes, width: width, height: height} = state
- # ) do
- # send(screen_pid, {:render, tui_pid, width, height})
- # {:noreply, state}
- # end
+ def handle(
+ :refresh,
+ %State{} = state
+ ) do
+ render(state)
+ {:noreply, state}
+ end
+
+ def handle(
+ {:send_to_ssh, data},
+ %State{width: width, height: height, tui_pid: tui_pid} = state
+ ) do
+ case get_terminal_dim_msg(width, height) do
+ {true, msg} -> send(tui_pid, {:send_data, msg})
+ {false, _} -> send(tui_pid, {:send_data, data})
+ end
+
+ {:noreply, state}
+ end
def handle(
{:resize, {width, height}},
%State{tui_pid: tui_pid, screen_processes: [screen_pid | _]} = state
) do
- new_state = %State{state | width: width, height: height}
-
- if height <= @max_terminal_height && width <= @max_terminal_width do
- send(screen_pid, {:render, width, height})
- else
- send(tui_pid, {:send_data, @terminal_bad_dim_msg})
+ case get_terminal_dim_msg(width, height) do
+ {true, msg} -> send(tui_pid, {:send_data, msg})
+ {false, _} -> send(screen_pid, {:render, width, height})
end
- {:noreply, new_state}
+ {:noreply, %State{state | width: width, height: height}}
end
def keymap(key) do
@@ -113,4 +118,30 @@ defmodule Chessh.SSH.Client do
x -> x
end
end
+
+ defp get_terminal_dim_msg(width, height) do
+ case {height > @max_terminal_height, height < @min_terminal_height,
+ width > @max_terminal_width, width < @min_terminal_width} do
+ {true, _, _, _} -> {true, @clear_codes ++ ["The terminal height is too large."]}
+ {_, true, _, _} -> {true, @clear_codes ++ ["The terminal height is too small."]}
+ {_, _, true, _} -> {true, @clear_codes ++ ["The terminal width is too large"]}
+ {_, _, _, true} -> {true, @clear_codes ++ ["The terminal width is too small."]}
+ {false, false, false, false} -> {false, nil}
+ end
+ end
+
+ defp render(%State{
+ tui_pid: tui_pid,
+ width: width,
+ height: height,
+ screen_processes: [screen_pid | _]
+ }) do
+ {out_of_range, msg} = get_terminal_dim_msg(width, height)
+
+ if out_of_range && msg do
+ send(tui_pid, {:send_data, msg})
+ else
+ send(screen_pid, {:render, width, height})
+ end
+ end
end
diff --git a/lib/chessh/ssh/client/menu.ex b/lib/chessh/ssh/client/menu.ex
index a69ad88..7e2ffbc 100644
--- a/lib/chessh/ssh/client/menu.ex
+++ b/lib/chessh/ssh/client/menu.ex
@@ -5,18 +5,10 @@ defmodule Chessh.SSH.Client.Menu do
require Logger
defmodule State do
- defstruct dy: 0,
- dx: 0,
- tui_pid: nil,
+ defstruct client_pid: nil,
selected: 0
end
- use Chessh.SSH.Client.Screen
-
- def init([%State{} = state | _]) do
- {:ok, state}
- end
-
@logo " Simponic's
dP MP\"\"\"\"\"\"`MM MP\"\"\"\"\"\"`MM M\"\"MMMMM\"\"MM
88 M mmmmm..M M mmmmm..M M MMMMM MM
@@ -25,6 +17,11 @@ defmodule Chessh.SSH.Client.Menu do
88. ... 88 88 88. ... M. .MMM' M M. .MMM' M M MMMMM MM
`88888P' dP dP `88888P' Mb. .dM Mb. .dM M MMMMM MM
MMMMMMMMMMM MMMMMMMMMMM MMMMMMMMMMMM"
+ use Chessh.SSH.Client.Screen
+
+ def init([%State{} = state | _]) do
+ {:ok, state}
+ end
# @options [
# {"Option 1", {Chessh.SSH.Client.Board, [%Chessh.SSH.Client.Board.State{}]}},
@@ -36,12 +33,7 @@ defmodule Chessh.SSH.Client.Menu do
{"Option 3", {}}
]
- def handle_info({:render, width, height}, %State{} = state) do
- render(width, height, state)
- {:noreply, state}
- end
-
- def handle_info({:input, width, height, action}, %State{selected: selected} = state) do
+ def input(width, height, action, %State{client_pid: client_pid, selected: selected} = state) do
new_state =
case(action) do
:up ->
@@ -61,36 +53,43 @@ defmodule Chessh.SSH.Client.Menu do
state
end
- render(width, height, new_state)
- {:noreply, new_state}
+ send(client_pid, {:send_to_ssh, render_state(width, height, new_state)})
+ new_state
+ end
+
+ def render(width, height, %State{client_pid: client_pid} = state) do
+ send(client_pid, {:send_to_ssh, render_state(width, height, state)})
+ state
end
- def render(width, height, %State{tui_pid: tui_pid, dy: dy, dx: dx, selected: selected}) do
- text = String.split(@logo, "\n")
+ defp render_state(
+ width,
+ height,
+ %State{selected: selected}
+ ) do
+ logo_lines = String.split(@logo, "\n")
{logo_width, logo_height} = Utils.text_dim(@logo)
- {y, x} = center_rect({logo_width, logo_height + length(text)}, {width, height})
+ {y, x} = center_rect({logo_width, logo_height + length(logo_lines)}, {width, height})
- rendered =
+ [ANSI.clear()] ++
Enum.flat_map(
- Enum.zip(1..length(text), text),
+ Enum.zip(1..length(logo_lines), logo_lines),
fn {i, line} ->
[
- ANSI.cursor(y + i + dy, x + dx),
+ ANSI.cursor(y + i, x),
line
]
end
) ++
- Enum.flat_map(
- Enum.zip(0..(length(@options) - 1), @options),
- fn {i, {option, _}} ->
- [
- ANSI.cursor(y + length(text) + i + dy, x + dx),
- if(i == selected, do: ANSI.format([:light_cyan, "* #{option}"]), else: option)
- ]
- end
- ) ++ [ANSI.home()]
-
- send(tui_pid, {:send_data, rendered})
+ Enum.flat_map(
+ Enum.zip(0..(length(@options) - 1), @options),
+ fn {i, {option, _}} ->
+ [
+ ANSI.cursor(y + length(logo_lines) + i + 1, x),
+ if(i == selected, do: ANSI.format([:bright, :light_cyan, "+ #{option}"]), else: option)
+ ]
+ end
+ ) ++ [ANSI.home()]
end
defp wrap_around(index, delta, length) do
diff --git a/lib/chessh/ssh/client/screen.ex b/lib/chessh/ssh/client/screen.ex
index 2dd9f9c..8eb9f21 100644
--- a/lib/chessh/ssh/client/screen.ex
+++ b/lib/chessh/ssh/client/screen.ex
@@ -1,21 +1,18 @@
defmodule Chessh.SSH.Client.Screen do
- @callback handle_info(
- {:render, width :: integer(), height :: integer()},
- state :: any()
- ) ::
- {:noreply, any()}
- @callback handle_info({:input, width :: integer(), height :: integer(), action :: any()}) ::
- {:noreply, any()}
-
- # @callback render(state :: Chessh.SSH.Client.State.t() | any()) :: any()
- # @callback handle_input(action :: any(), state :: Chessh.SSH.Client.State.t()) ::
- # Chessh.SSH.Client.State.t()
+ @callback render(width :: integer(), height :: integer(), state :: any()) :: any()
+ @callback input(width :: integer(), height :: integer(), action :: any(), state :: any()) ::
+ any()
defmacro __using__(_) do
quote do
@behaviour Chessh.SSH.Client.Screen
use GenServer
+ @clear_codes [
+ IO.ANSI.clear(),
+ IO.ANSI.home()
+ ]
+
@ascii_chars Application.compile_env!(:chessh, :ascii_chars_json_file)
|> File.read!()
|> Jason.decode!()
@@ -26,6 +23,12 @@ defmodule Chessh.SSH.Client.Screen do
div(parent_width - rect_width, 2)
}
end
+
+ def handle_info({:render, width, height}, state),
+ do: {:noreply, render(width, height, state)}
+
+ def handle_info({:input, width, height, action}, state),
+ do: {:noreply, input(width, height, action, state)}
end
end
end