User friendly interactive cli prompts

I really enjoy using gum (GitHub - charmbracelet/gum: A tool for glamorous shell scripts 🎀)
in my bash scripts, they also have a library called huh that you can use in your own Go apps. Node has packages like prompts. I wonder if Zig has an equivalent or if there is some c/c++ equivalent that can used via ffi.

3 Likes

I haven’t tried this so I am just brainstorming different possibilities here:

  1. figure out whether someone has created a c or zig binding to huh (I couldn’t find one)
  2. find another zig or c library that does what you want
  3. use the gum application directly as a subprocess, calling it with appropriate arguments pipe-ing input into and out of it and handling the exit code
    basically in this version your program pretends to be the bash script
  4. use go to create a simplified API based on what you actually need, that internally uses huh, then use a process like described here to compile it into a shared library that can be used from a zig program

I think for quick and easy uses 3. might be a a good way, although I don’t have practice with using things pretending to be a shell, also depends highly on gum itself whether you can just run it as a subprocess or whether it expects you to behave very shell like, that is something you would have to investigate.

If you want to use it in a highly application specific way and a lot I think 4. makes sense. If you want to use it very flexibly without having to wrap things / create bindings on demand, you probably need 1. or 2.

For 2. there are probably others that know more.

Another option would also be to build a new library for this, possibly using a more low level library made for interacting with the terminal, but I am not up to date on which libraries are good and actively maintained.

I think it would definitively be a good thing to have an easy to use solution for cli prompts. When creating a custom solution, the main thing I don’t know yet is how do you get key/mouse events? I would imagine that with that you could basically just clear screen and reprint for a badly performing naive implementation and I assume better solutions keep track of dirty rectangles just updating those.

I think this is a great topic, welcome to the forum!

1 Like