fireproofsocks

fireproofsocks

Clear the screen using termbox (ex_termbox)

I have been playing around with ex_termbox (which provides bindings to termbox) as a way to “paint” the screen when running CLI apps. I’ve got most things working – the package has some excellent examples. However I’m not sure how to “clear” the screen when I launch something from iex. My loop is all in the context of a GenServer – the important parts look something like this:

use GenServer

alias ExTermbox.Bindings, as: Termbox
alias ExTermbox.{Cell, EventManager, Event, Position}

def start_link(%{name: name} = state) do
  GenServer.start_link(__MODULE__, %{response: ""}, name: name)
end

@impl true
def init(%{name: name}) do

  :ok = Termbox.init()

  {:ok, em_pid} = EventManager.start_link(name: :"#{name}_event_mgr")
  :ok = EventManager.subscribe(em_pid, name)

  {:ok, state, {:continue, :draw_screen}}
end

  @impl true
  def handle_continue(:draw_screen, %{response: response} = state) do

    Termbox.clear()

    response = String.to_charlist(response)

    for {ch, x} <- Enum.with_index(response) do
      :ok = Termbox.put_cell(%Cell{position: %Position{x: x, y: 0}, ch: ch})
    end

    Termbox.present()

    {:noreply, state}
  end

However, when I start an instance of this GenServer from iex, the screen is not fully cleared. Things work, but I end up seeing parts of the iex session “bleeding through”. Something like this:


❯ █  ok, #PID<0.200.0>}
iex(2)>

where I can type input at the prompt and things work, but it just looks wrong. Things are a bit better if I manually clear the screen, e.g. via my own function like this:

  defp clear_screen do
    {:ok, width} = Termbox.width()
    {:ok, height} = Termbox.height()

    for x <- 0..width, y <- 0..height do
      :ok = Termbox.put_cell(%Cell{position: %Position{x: x, y: y}, ch: 8206})
    end
  end

If I use a unicode whitespace character (e.g. ch 8206), then most of the screen gets covered like I want and the iex> prompt and pid are visible only at the very bottom of the screen.

My guess is that iex and termbox are wrestling for control over the current view port. Is there a way for termbox to fully take control over the screen, or am I bound to accommodate iex so long as I am launching my app from within iex?

Thanks for any pointers, thoughts, enlightenment.

First Post!

ityonemo

ityonemo

Maybe don’t run IEx? Just launch elixir by itself.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement