Skip to content

Instantly share code, notes, and snippets.

@uesteibar
Created January 19, 2016 20:30
Show Gist options
  • Save uesteibar/3cc0ddc89ee2a431506d to your computer and use it in GitHub Desktop.
Save uesteibar/3cc0ddc89ee2a431506d to your computer and use it in GitHub Desktop.
terminal keynote exercise
Console keynote
Apple is presenting its new iTerminal. Of course millions of people will line up in front of apple stores to buy it and they need a new powerful presentation to impress the media. The presentation will be shown using the iTerminal, to show off how cool terminal output can be.
They have hired you to create the keynote!
Iteration 1: The keynote
First, make a text file to store the presentation content ( for example, slides.txt). Each “slide” is one line of text, and the lines of text are divided by four dashes ----. We can assume that each line will not be longer than the width of the screen. It should look like this:
Prepare for the next big thing
----
The iTerminal
----
Is here
In the same folder, create a new file for your Ruby program (for example, keynote.rb). This program should read the text file and present the lines of text in the terminal as slides.
The first slide should show something like this:
Prepare for the next big thing
>
So the text will be centered in the terminal, horizontally and vertically. You can use ruby-terminfo to get the size of the terminal.
require 'terminfo'
p TermInfo.screen_size
That snippet returns an array with the current height and width of the screen.
If you're using Windows, try using the Highline gem to get the terminal size. Check the documentation under System Extensions, where there is a method called #terminal_size.
At the bottom of the terminal, you need to show a prompt > that can take the following commands:
next: For the next slide (unless it’s the last slide)
previous: For the previous one (unless it’s the first slide)
When the user enters next, the next line of the text file should appear on the terminal, centered and with the prompt at the bottom, until you reach the last line.
Hints
Start by creating a class with methods to parse the text file slides.txt. It should give you an array where each item is a line of text to display as a slide.
Make a second class with the methods to show the presentation. You will need to find out the height of the user's terminal, and then calculate how many empty lines to print before and after the text to vertically center it. You will also need to get the width of the terminal and calculate how many spaces to print before and after the line of the text to horizontally center it.
Create a method that deals with the user input: next or previous. You can use the indices of the items in the array to select the one you want to show.
Iteration 2: Auto-advance
Add an option to auto advance: add a command called auto that will move to the next slide every 3 seconds until the last slide. Once in auto advance mode, there should be no user prompt.
Iteration 3: Multi-line slides
So far, all of our slides are just one line of text. But what if we want to add some multi-line slides or cool graphics to our presentation? We need to add support for slides that are more than one line. The multi-line content should still be centered vertically and horizontally on the screen. For example, if our slides.txt file looks like this:
| |
| Hi |
| |
----
This is
----
Apple's new product
----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x x
x x
x The iTerminal x
x x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Then the keynote.rb script should output slides such as this:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x x
x x
x The iTerminal x
x x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment