Why you should be using TTY-Prompt

John Long
JL Codes
Published in
2 min readDec 4, 2017

If you’re making any kind of terminal app, you should be using tty-prompt.

Find it on Github: https://github.com/piotrmurach/tty-prompt

Art by Noah Bradley

So, what is tty-prompt? First, let’s take a look at what the beginnings of a vanilla terminal app might look like.

← Seems like a pretty simple menu. Just type in your selection, follow an if/else statement under the hood and you’re good to go. We’ve all been here before.

But what if this menu needs to be expanded, or you want to add a new option #1, or swap positions of options?

Things can quickly go from annoying to out of hand. Adding a new option means you need to change potentially every number in the if/else statement. This is troublesome and increases your chances of making an error.

And here I present the solution — use the gem tty-prompt. You’ll thank yourself later.

TTY-Prompt is a dynamic gem that improves your menu building capabilities. Making your menus, intuitive and interactive. If you don’t believe me, take a look at the readme from the creator. tty-prompt provides many more options than I’m capable of sharing in one post.

Below is a sample of what your new menu could look like using tty-prompt. (taken directly from a small project of mine)

It’s simple, it’s elegant, but perhaps most importantly using tty-prompt makes your code easier to maintain. Here’s the code used to build the menu shown above

That’s right, no if/else statements, no artificial numbering, nada. Clean and simple. If I wanted to add a line or change the order of my menu I would only need to shuffle around these few lines of code.

As you might notice, I’ve stuck methods into the values of my menu options hash. Calling on these with prompt is even easier than meets the eye. Prompt has a build in system to trigger the Proc objects without having to call them later on. This is just one of the many features tty-prompt offers.

So what are you waiting for? Go get tty-prompt now and start using it in your terminal applications.

Find it on Github: https://github.com/piotrmurach/tty-prompt

--

--