I finished my first project for my coding bootcamp! This post will demo my Ruby CLI project, explain its basic file structure, and highlight some of its code functionalities. Additionally, I’ll call attention to a few simple tips to easily improve the visuals of a CLI program.
My program starts by welcoming the user and printing out a numbered list of Ghibli film titles pulled from this Ghibli API. Inputting a value between 1 and 20 allows the user to select which film he/she wants to know more about. The program returns the film’s title, director, release date, and description. Then the user has the choice to learn about another film, or to exit the program.
My code is written inside 5 main files: (1) bin/run, (2) environment.rb, (3) api.rb, (4) ghibli_film.rb, and (5) cli.rb. The run file is the main executable and is how you initialize and start the program. I required the
environment.rb file, which lists all required gems and relative files, inside the run file. The
api.rb file stores the API endpoint and uses
Net::HTTP to retrieve the response body in string form; then it uses
JSON to parse the string into an array of hashes, and finally, iterates through the array to instantiate
GhibliFilm objects. This is possible via the
ghibli_film.rb file that writes and reads the required attributes to create and store instances of the
GhibliFilm class. Lastly, the
cli.rb file contains all the logic required to run through the program.
You can find my full code here.
Highlight: Listing Titles in Multiple Columns
In my opinion, the coolest part of my project that wasn’t a part of the requirements is the creation of columns for the menu list. I saw that one of my classmates implemented this feature and reached out to him for some guidance.
I essentially utilized a
while statement to loop through all of my films using 2 indices.
column1 starts at 0 and
column2 starts at 10 because I wanted to have 10 films listed in each column. Every loop increments both
column2 by 1 while
column1 < 10 (which is the same as
column2 < 20). In this way, the first line prints the film title for the objects in the index 0 and index 10 positions, the second line prints index 1 and index 11 values, and so forth.
But what’s the
#convert_film_title_length before listing out the film titles, you ask? Due to the varying film title character lengths in the 1st column, the 2nd column was printing misaligned! Therefore, I had to create a helper method that iterated through all film titles and added a blank character until its size reached 20 characters. It was pretty cool to learn this use case of the
Tips for Simple Visual Improvements
Other than splitting the options list into multiple columns, there were a few other ways to make the aesthetic of my CLI project to display more cleanly. I found this part to be highly enjoyable, especially because CLI projects can be pretty minimal visually and it gave some room for artistic freedom.
There are plenty of free ASCII art resources online that you can incorporate into your CLI programs. Sure enough, typing “ghibli ascii art” in Google pulled up a few My Neighbor Totoro artworks. Of course you can create your own if you desire, but I left this task to my more talented counterparts.
Note: One thing to be aware of when using ASCII art is that the backslash will not be read in the terminal unless preceded by another backslash. Therefore, the art will look misaligned in the code file (if using backslashes), but will print out properly in the terminal.
Space Out Your Lines
puts “" lets you create spaces to spread out the lines being printed onto the terminal. This surprisingly made a huge difference in how my program displayed, making it easier and clearer to navigate.
print vs puts
I’ve been used to using the
puts method whenever printing out anything to the console. Using
puts, however, automatically adds a new line at the end of your message. So when prompting the user to input any value, I decided to use the
This was my first ever project that I created from start to finish, from the creation of the files, folders, to architecture. It was also pretty cool to have my program successfully talk to an outside API. There’s definitely a certain confidence boost from being able to make something from scratch versus passing lessons and tests like I’ve been doing up until now. This makes me even more excited for the next modules to come where I’ll be able to eventually create real interactive user interfaces… but for now, using ASCII artwork is plenty neat, too.