Table with selectable rows in CLI

While working on a project that involved creating an application in CLI (Command Line Interface), I faced some challenges. The one I considered to be one of my top priorities was the UI (User Interface). Since everything in this application was displayed in the terminal, there were limitations and as a programmer, I had to find the best way to present information to the user and create a seamless flow to enhance the User Experience (UX).
I was able to find several gems like table_print that allowed me to print my Product information, which was a step forward in presenting my Products in a table format.

But to allow the user to select a product, I would have to list a product identifier like the ID. Then the user would type this ID Number and continue with the application work flow. I did not like this solution, because I wanted a way for the user to be able to scroll through the table and select a row without needing to know what the ID was for that product.
Just like TTY::PROMPT allows you to create a menu of options and the user can scroll through them and select the one they wanted.

So I tried to incorporate both table_print and TTY::PROMPT but failed in my attempts. Then I came up with a solution that allowed me to create a table feel experience and allow the user to scroll through the rows of such table without needing to remember what was the Product ID they wanted.
All the user has to do is scroll through the rows and press Enter on their selection and continue on with the application flow.

Solution
This solution involved four (4) steps:
- Add a header for the table Column Names
- Create a Hash that will store the message and Product ID
- Format the message string so it looks like columns
- Use TTY::Prompt to create a menu and capture the Product ID
Format the rows
I’m going to focus on a critical part of this code, which is formatting the hash product_h to hold the rows in a column style format. For this I used the help of printf(), and this reference guide.
'%-6s|' % product.id + '%3s' % “”This above code means, put what is in the placeholder (%), which in this case is the product.id, and left-justify it (-). Allow 6 spaces in total including the value of product.id, which I’m denoting as a String (s) and add a | after the value of product.id plus add 3 spaces after | that are blank “”.
From lines 1–5, it generates the key for product_h and assign it the value of product.id.

I hope this will help someone that is facing similar situation as I did. You can find the code in my GitHub here.
