ghoetker

ghoetker

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

Showing Posts 1 to 1

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews