ndreynolds / ratatouille

A TUI (terminal UI) kit for Elixir
MIT License
752 stars 39 forks source link

Ratatouille.run() works but supervision does not? #37

Closed sschuldenzucker closed 4 months ago

sschuldenzucker commented 4 months ago

Hey all, I'm stuck with an issue trying to write my first ratatouille app; I find this very surprising. (is this a bug??)

I have defined a test app using a copy of the "Counter" example. I've defined my app module (called SrtdElixir here) as follows:

defmodule SrtdElixir do
  use Application

  def start(_type, _args) do
    # This works:
    # Ratatouille.run(SrtdElixir.App)

    # This doesn't work:
    # Ratatouille.Runtime.Supervisor.start_link(runtime: [app: SrtdElixir.App])

    # This doesn't work:
    Supervisor.start_link(
      [
        {
          Ratatouille.Runtime.Supervisor,
          runtime: [app: SrtdElixir.App]
        }
      ],
      strategy: :one_for_one,
      name: SrtdElixir.Supervisor
    )
  end

  def stop(_state) do
    # Hard shutdown. Should this be System.stop() instead?
    # System.halt()
  end
end

But when I run this, the app doesn't show: the application goes into fullscreen terminal mode, then crashes (leaving my terminal corrupted, have to $ reset). It works fine if I call Ratatouille.run() (second line) and I can also run the example script directly using mix run --no-start. So overall, nothing happens unless I use Ratatouillle.run().

Test app SrtdElixir.App:

defmodule SrtdElixir.App do
  @behaviour Ratatouille.App

  import Ratatouille.View

  require Logger

  def init(_context) do
    0
  end

  def update(model, msg) do
    case msg do
      {:event, %{ch: ?+}} -> model + 1
      {:event, %{ch: ?-}} -> model - 1
      _ -> model
    end
  end

  def render(model) do
    view do
      label(content: "Counter is #{model} (+/- to increment/decrement, q to quit)")
    end
  end
end

System config:

02:22:57 steffen@srs ~/srtd_elixir (main)> elixir --version
Erlang/OTP 26 [erts-14.2.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit] [dtrace]

Elixir 1.17.0 (compiled with Erlang/OTP 26)
02:23:03 steffen@srs ~/srtd_elixir (main)> mix deps
* asciichart 1.2.0 (Hex package) (mix)
  locked at 1.2.0 (asciichart) 28a87009
  ok
* dialyxir 1.4.3 (Hex package) (mix)
  locked at 1.4.3 (dialyxir) bf2cfb75
  ok
* elixir_make 0.8.4 (Hex package) (mix)
  locked at 0.8.4 (elixir_make) 6e7f1d61
  ok
* erlex 0.2.7 (Hex package) (mix)
  locked at 0.2.7 (erlex) 3ed95f79
  ok
* ex_termbox 1.0.2 (Hex package) (mix)
  locked at 1.0.2 (ex_termbox) ca7b14d1
  ok
* ratatouille 0.5.1 (Hex package) (mix)
  locked at 0.5.1 (ratatouille) b2394eb1
  ok
sschuldenzucker commented 4 months ago

Ah sh,,, I forgot that you need --no-halt in mix run to keep the app running after everything is set up with the usual pattern. And Ratatouille.run() blocks so this works.

Closing this issue.