ghoetker

ghoetker

Zenity as a simple GUI

There has been a lot of discussion about GUIs and it will be incredibly cool to have Boyd Multerer’s Scenic framwork when he releases it. In the interim (or perhaps for simpler tasks), I’ve discovered that Zenity is actually not half bad to use. Not pretty, but functional. Although originally for Linux, a Mac version available from MacPorts. Not having found any mention of it by searching for Forums, I wanted to share in case anyone else might find it useful.

By “discovered”, I mean that I found Andrei Clinciu’s great post at https://andreiclinciu.net/article/zenity-gui-for-elixir/ and the links therein. Just to give a flavor, here is an example that takes a list of keywords, ensures they are unique and in alphabetic order, presents the list to the user as a check list and then returns the checked words as a list.

My example is more brute force that Andrei’s because some of the arguments to be sent required quotes and I was having a terrible time getting that to work (I am very much a beginner at Elixir and not primarily a developer.)

The desired command to be sent is as follows:

/opt/local/bin/zenity --list --checklist --column "Delete?" --column "Keywords" --separator=":" "FALSE" "Cat" "FALSE" "Furry dog" "FALSE" "Horse" 2>/dev/null

which I built programmatically and then just passed to Port.open as a blob. Zenity sometimes emits an irritating GTK+ warning, which I’d chosen to sent to /dev/null for simplicity. Obviously, in real use, kw_input would come from some external source.

Even I can tell that the structure of this example is horrid, but I hope it illustrates the Zenity-specific aspects adequately. Sometimes it’s nice to just be able to kludge a shell command together for what you need.

Would love to hear of other alternatives, especially for low-complexity use cases like this.

Glenn

defmodule Zenity do
  @zenity "/opt/local/bin/zenity"

  def do_it do
    kw_input = ["Cat", "Furry Dog", "Cat", "Horse"]

    kw_for_cmd =
      kw_input
      |> MapSet.new()
      |> Enum.sort()
      |> Enum.flat_map(fn x -> [~s("FALSE"), ~s("#{x}")] end)
      |> Enum.join(" ")

    cmds =
      ~s(#{@zenity} --list --checklist --column "Delete?" --column "Keywords" --separator=":" #{kw_for_cmd} 2>/dev/null)

    exec(cmds)
  end

  defp exec(cmd) do
    port = Port.open({:spawn, cmd}, [:stream, :binary, :exit_status, :hide, :use_stdio])

    port
    |> handle_output
    |> listify
  end

  defp handle_output(port, data \\ "") do
    receive do
      {^port, {:data, data}} ->
        handle_output(port, data)

      {^port, {:exit_status, status}} ->
        {data, status}
    end
  end

  defp listify({_, 1}) do
    IO.puts("Operation cancelled")
  end

  defp listify({"", 0}) do
    IO.puts("Nothing selected")
  end

  defp listify({kw_list, 0}) do
    kw_list
    |> String.trim_trailing("\n")
    |> String.split(":")
  end

end

Zenity.do_it

Most Liked

Andrei

Andrei

Hi there.
I’m glad you’re using Zenity together with Elixir.
That blog post attracted a lot more visitors than I had anticipated which is great.

Zenity is cool for fast prototyping, the sad part is that it’s development has stopped.

I recommend newcomers to try yad out (YAD download | SourceForge.net), it’s a Zenity fork.

It provides more flexibility and way more options than Zenity ever did.

Good luck in developing!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
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

We're in Beta

About us Mission Statement