Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to spawn a tty using a system command #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/extty.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,28 @@ defmodule ExTTY do
{Elixir.IEx, :start, opts}
end

defp shell_spawner(%{type: :muontrap, shell_opts: opts}) do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defp shell_spawner(%{type: :muontrap, shell_opts: opts}) do
defp shell_spawner(%{type: :system, shell_opts: opts}) do

MuonTrap is an implementation detail. What if we generalize this to System?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about letting the user pass a :custom shell spawner that calls an arbitrary function?

I'd be for adding an example to the docs if that's direction that you go since I wouldn't have expected calling System.cmd to work well with this. I'm thrilled that it does, though.

{__MODULE__, :spawn_muontrap, opts}
end

defp shell_spawner(state) do
Logger.warn(
"[#{inspect(__MODULE__)}] unknown shell type #{inspect(state.type)} - defaulting to :elixir"
)

shell_spawner(%{state | type: :elixir})
end

def spawn_muontrap(opts \\ []) do
exec = Keyword.fetch!(opts, :exec)
args = Keyword.fetch!(opts, :args)
gl = Process.group_leader()

spawn(fn ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
spawn(fn ->
spawn_link(fn ->

MuonTrap.cmd(exec, args,
into: IO.stream(gl, :line),
stderr_to_stdout: true
)
end)
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule ExTTY.MixProject do
defp deps do
[
{:dialyxir, "~> 1.0.0", only: :dev, runtime: false},
{:ex_doc, "~> 0.22", only: :docs}
{:ex_doc, "~> 0.22", only: :docs},
{:muontrap, "~> 1.0.0"}
]
end

Expand Down