Chapter 13. Rascalry

Time is flying like an arrow And the clock hands go so fast, they make the wind blow And it makes the pages of the calendar go flying out the window, one by one

They Might Be Giants, “Hovering Sombrero” (2001)

In this chapter, you will create a clone of cal, which will show you a text calendar in the terminal. I often don’t know what the date is (or even the day of the week), so I use this (along with date) to see vaguely where I am in the space-time continuum. As is commonly the case, what appears to be a simple app becomes much more complicated as you get into the specifics of implementation.

You will learn how to do the following:

  • Find today’s date and do basic date manipulations

  • Use Vec::chunks to create groupings of items

  • Combine elements from multiple iterators

  • Produce highlighted text in the terminal

How cal Works

I’ll start by showing you the manual page for BSD cal to consider what’s required. It’s rather long, so I’ll just include some parts relevant to the challenge program:

CAL(1) BSD General Commands Manual CAL(1) NAME cal, ncal — displays a calendar and the date of Easter SYNOPSIS cal [-31jy] [-A number] [-B number] [-d yyyy-mm] [[month] year] cal [-31j] [-A number] [-B number] [-d yyyy-mm] -m month [year] ncal [-C] [-31jy] [-A number] [-B number] [-d yyyy-mm] [[month] year] ncal [-C] [-31j] [-A number] [-B number] [-d yyyy-mm] -m month [year] ncal [-31bhjJpwySM] [-A number] [-B number] [-H yyyy-mm-dd] [-d yyyy-mm] [-s country_code] ...

Get Command-Line Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.