paint-brush
Improve your command-line Go application with Promptuiby@luiz.manifold
3,681 reads
3,681 reads

Improve your command-line Go application with Promptui

by Luiz BrancoNovember 22nd, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<a href="https://hackernoon.com/tagged/promptui" target="_blank">promptui</a> is a Go library that provides a simple interface to create cli prompts. The library has two main modes: <em>Prompt</em> — which provides a single line for user input, and <em>Select</em> — which displays a list of items to choose from. Both modes have several options for customization and take advantage of the powerful <a href="https://github.com/chzyer/readline" target="_blank">readline</a> to support multiple terminals.
featured image - Improve your command-line Go application with Promptui
Luiz Branco HackerNoon profile picture

promptui is a Go library that provides a simple interface to create cli prompts. The library has two main modes: Prompt — which provides a single line for user input, and Select — which displays a list of items to choose from. Both modes have several options for customization and take advantage of the powerful readline to support multiple terminals.

In this post we are going to create a food recipe command-line app that asks for a dish type, retrieves information from an API and displays the relevant recipes.

If you want to jump to the complete app source code, go to: https://github.com/luizbranco/promptui-demo/

Initial Prompt

Before we begin our app, we first need to get the library:

go get -u github.com/manifoldco/promptui

With that in our Go path we can start writing our main.go file.

If you then run the program, you should see a prompt asking to search:

go run main.go

That seems nice, but if we just press enter without entering information it still works. It would be nice if we could prevent empty searches. So lets add a validation to the prompt:

This is a very simple validation. But you can create more complex ones using regexp or govalidator, for example.

Retrieving data

For this cli app we are going to use the awesome RecipePuppy to search for recipes. Its API doesn’t require authentication, so it’s perfect for our experiment.

First we define how to request the data:

That’s easy! Now we have to parse the response JSON. First we need an example of the data returned:

As we can see, there is an array of results with title, link and a list of ingredients for each recipe. With that, we can define a few structures to parse the body:

Displaying Data

Now that we have the list of recipes parsed, we can use Select to display it to the user.

Well, that’s a bit ugly. The issue here is that promptui usually expects a slice of strings to display. But we have recipe structures instead. We have to teach promptui how to display them. That’s when templates are handy:

If you have used Go templates/html you should be familiar with the syntax. Promptui out-of-the-box supports a few template functions such as colors and styles, but you can define your own helpers.

Much better! Notice the pizza unicode symbol there. Go supports UTF-8 source code 👍.

Additional Details

Since we also have a list of ingredients, we can show it when users are hovering an element with the Details template option. But first, lets be good cli citizens and avoid a long list of ingredients!

This defines a custom helper function that truncates strings longer than X characters. Then we change templates to be:

Search

Finally, lets allow users to search the list inline for either the recipe name or ingredients:

As it was with the validation example, this is a trivial string search, but you can make as complex as you need.

Final Thoughts

promptui was part of our command-line tool Manifold in which we use mainly as a fallback when users don’t input all flags necessary for commands. This small app shows a few features of promptui, but the library has much more to offer. Make sure to check the Github project page.