TTY::Prompt in a CLI Application

Irvin Natt
4 min readDec 22, 2020

A Command Line Interface Application, also known as a CLI application, is an interactive program that allows the user to navigate and process commands through their terminal.

During project week at Flatiron School, we were tasked with creating a CLI app that would utilize CRUD elements (Create, Read, Update, Delete) and would need to display some kind of output to our terminal. After brainstorming for a hot minute, we stumbled upon this neat, little Ruby gem called TTY::Prompt.

TTY::Prompt, created by Piotr Murach, is an amazing gem that allows users to respond to commands, or prompts, within the terminal. Users can insert inputs into the terminal and directly respond to prompts that have been programmed into the CLI app. It’s an interesting and easy-to-use gem that allows for a ton of interactivity with the user.

After doing a bit of research on TTY::Prompt usage, we discovered that one of the popular uses of the gem is to actually make a text-based adventure game. To put it simply, users are given a list of choices, and after selecting an option, they are then given another set of prompts, ultimately having some sort of end with a specific selection.

Getting Started

To get started with TTY::Prompt, follow these easy steps:

  1. Add this line to your application’s Gemfile:

2. And then execute:

3. Or install it yourself as:

Source: https://github.com/piotrmurach/tty-prompt

and it’s that simple! To start TTY::Prompt, you need to create a variable prompt which is set to create a new instance of the Prompt class.

prompt = TTY::Prompt.new

The Game

We ultimately decided to create a text-based game where the player must navigate through a house full of killers and find the exit before they run out of lives. The game would give the player a list of rooms, and one of these rooms will have a secret exit that allows them to successfully finish the game. If the player manages to continuously run into killers in the rooms, the game would end and start the game over.

Flatiron Psycho Mansion

Welcome to our Flatiron Psycho Mansion!

-HOW TO PLAY-

You wake up in an abandoned house, with no memory of how you ended up there.
The purpose of the game is to navigate through a series of rooms and (hopefully) survive the nasty killers that may be waiting for you in the rooms, while searching for the hidden exit.
You can choose to fight or run if you happen to meet a killer, but be careful — there are consequences to both.
Will you make it out?

I won’t run through the entire game but I do want to show how some of these prompts work, and how you can program prompts within prompts (super interesting stuff!)

Every time the user types in an input, the prompt gem will save that input and we build out multiple menus based off of that individual input. It allows for a style of game that allows it to constantly move forward, instead of having to reset or move backwards to a menu. I’ll show a quick example of some code for one of the menus

A method that we used to initially get the user to “sign up” for the game. Prompt.ask allows us to ask any question through a string (doesn’t necessarily need to be a question, but a command!)

Final Thoughts

Building this game out was definitely a blast, and was made enjoyable with how customizable TTY::Prompt is. Even though we used some simple commands within prompt (such as .ask) there are a number of commands you can use, and I’d like to check some more of these out in the future.

If you navigate over to Piotr Murach’s TTY::Prompt Github page, you’ll find an extensive list of commands that can be used with prompt. A couple of commands that I’d want to check out in the future are listed below.

Source: https://github.com/piotrmurach/tty-prompt#2621-cycle (I’m loving the Mortal Kombat references)
Source: https://github.com/piotrmurach/tty-prompt#2621-cycle
Source: https://github.com/piotrmurach/tty-prompt#2621-cycle

Thank you for reading! I would definitely recommend trying out TTY::Prompt for yourself and seeing just how interactive and customizable it can be for your program. Enjoy!

--

--