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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
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

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement