94 lines
2.4 KiB
Elixir
94 lines
2.4 KiB
Elixir
defmodule PrymnWeb.Terminal do
|
|
use PrymnWeb, :live_component
|
|
|
|
alias Prymn.Agents
|
|
|
|
@impl true
|
|
def mount(socket) do
|
|
{:ok, assign(socket, :open, false)}
|
|
end
|
|
|
|
@impl true
|
|
def update(assigns, socket) do
|
|
socket =
|
|
if assigns[:data],
|
|
do: push_event(socket, "data", %{"data" => assigns[:data]}),
|
|
else: socket
|
|
|
|
{:ok, assign(socket, assigns)}
|
|
end
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<div>
|
|
<PrymnWeb.Button.primary
|
|
:if={not @open}
|
|
type="button"
|
|
phx-target={@myself}
|
|
phx-click="open_terminal"
|
|
>
|
|
Open Terminal
|
|
</PrymnWeb.Button.primary>
|
|
<PrymnWeb.Button.primary
|
|
:if={@open}
|
|
type="button"
|
|
phx-target={@myself}
|
|
phx-click="close_terminal"
|
|
>
|
|
Close Terminal
|
|
</PrymnWeb.Button.primary>
|
|
<div :if={@open} class="mt-2 bg-black p-2">
|
|
<div phx-hook="Terminal" id="terminal" />
|
|
</div>
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("open_terminal", _params, %{assigns: assigns} = socket) do
|
|
# TODO: make up a terminal id
|
|
Agents.open_terminal(assigns.agent)
|
|
|
|
{:noreply, assign(socket, :open, true)}
|
|
end
|
|
|
|
def handle_event("close_terminal", _params, socket) do
|
|
Agents.close_terminal(socket.assigns.agent)
|
|
{:noreply, assign(socket, open: false, data: "")}
|
|
end
|
|
|
|
def handle_event("data_event", data, %{assigns: assigns} = socket) when is_binary(data) do
|
|
Agents.send_terminal_input(assigns.agent, data)
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("resize_event", %{"cols" => cols, "rows" => rows}, socket) do
|
|
Agents.resize_terminal(socket.assigns.agent, rows, cols)
|
|
{:noreply, socket}
|
|
end
|
|
|
|
# defp receive_loop(stream) do
|
|
# receive do
|
|
# {:data_event, data} ->
|
|
# GRPC.Stub.send_request(stream, %TerminalRequest{input: data})
|
|
# receive_loop(stream)
|
|
|
|
# {:resize_event, rows, cols} ->
|
|
# GRPC.Stub.send_request(stream, %TerminalRequest{
|
|
# resize: %TerminalRequest.Resize{rows: rows, cols: cols}
|
|
# })
|
|
|
|
# receive_loop(stream)
|
|
|
|
# :data ->
|
|
# receive_loop(stream)
|
|
|
|
# :close ->
|
|
# GRPC.Stub.send_request(stream, %TerminalRequest{input: ""}, end_stream: true)
|
|
# after
|
|
# 120_000 ->
|
|
# GRPC.Stub.send_request(stream, %TerminalRequest{input: ""}, end_stream: true)
|
|
# end
|
|
# end
|
|
end
|