Time and the Command Line

Time and the Command Line

Type "time" into the command prompt window and press "Enter." The current time setting will now display. To change it, type the proper time into the window in the "00:00:00" 24-hour format -- for example, "13:30:00" for 1:30 p.m. -- and press "Enter." The new time will now be saved.

A CLI (command line interface) application is one that takes place entirely within a computer’s terminal. The terminal is meant to do exactly two things: read user input, and write a response. No graphics, no complex interface, only text. While CLI applications are limited in what they can express to a user, they can still accomplish much more than one might think at first glance.

While creating my first CLI application in Ruby, the first hurdle I came across was in implementing time as a factor in my app. In a format that’s designed exclusively to respond to user input — no matter when it may arrive — how can an outside factor like time dictate how an application works?

Well, as it turns out, it can do so through a lot of different methods. The following are a number of ways to have CLI applications change over time, without necessary user input.

The sleep method

The easiest way to integrate time into a CLI application — or any Ruby application, for that matter — is by using the built-in sleep method. This method simply “pauses” the application for a specified number of seconds.

puts “This prints immediately”

sleep(5)
puts “This prints 5 seconds later”

Keep in mind that the argument it takes in is a float, not an integer. The method works just as well with only increments of seconds. Here’s a fun trick: in conjunction with an escape sequence like “\e[2J\e[f” that clears the terminal screen, quick sleeps can be used to effectively animate within the terminal.

print "\e[2J\e[f"

puts '  ___
{o,o}
|)__)
--"-"-- '
sleep(0.1)
print "\e[2J\e[f"
puts '  ___
{  o},
|)__)
--"-"-- '
sleep(0.1)
print "\e[2J\e[f"
puts '  ___
{   }
|)__)
--"-"--'
sleep(0.1)
print "\e[2J\e[f"
puts '  ___
{o  }
|)__)
--"-"--'
sleep(0.1)
print "\e[2J\e[f"
puts '  ___
{o,o}
|)__)
--"-"--

cli coding flatiron-school ruby

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Day 14 of 30 Ruby Coding Challenge - Fibonacci Sequence the Ruby Way

Day 14 of 30. We're going to solve the famous Fibonacci sequence by taking advantage of features that Ruby offers.

How Benchmarking Your Code Will Improve Your Ruby Skills

Learning to code is a path full of struggles, and learning Ruby isn't the exception. But you'll agree with me, that practice is the best way to learn and develop your skills.

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

Day 18 of 30 Ruby Coding Challenge - Finding the Missing Number Game

We're going to play a game: find the missing number in a given array. This first solution will be not that great but we'll get the job done!

Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers

Day 16 of 30 - Ruby Coding Challenges in 30 Days. We're going to sum all the even numbers in a given Fibonacci sequence.This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series