summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client/screen.ex
blob: 8eb9f2187f31db9438de1d38da8b0bd37e2a2e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
defmodule Chessh.SSH.Client.Screen do
  @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!()

      def center_rect({rect_width, rect_height}, {parent_width, parent_height}) do
        {
          div(parent_height - rect_height, 2),
          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