Giter Club home page Giter Club logo

cat-kata's Introduction

The Cat Kata

Today's Kata is to reimplement the classic Unix utility cat.

Our favorite cat

A brief history of the utility, which you can find on the cat man page:

HISTORY

A cat utility appeared in Version 1 AT&T UNIX.  Dennis Ritchie designed
and wrote the first man page.  It appears to have been cat(1).

While this is one of the oldest Unix utilities, don't underestimate it. There is still plenty of work ahead of us.

Usage

In its simplest use case, cat will spit out the contents of a file into your terminal window.

$ cat short_and_sweet.txt
This is a good test file.

It only has 3 short lines.

That's not all it does though! The name "cat" comes from the original use case for cat, which is to concatentate the contents of multiple files together and send them to a another file.

$ cat one_liner.txt short_and_sweet.txt
One is the loneliest number.
This is a good test file.

It only has 3 short lines.

It is sometimes easy to forget that things printed to the terminal are actually just printing to a default file: STDOUT. We can redirect that output quite easily with the redirection operators.

$ cat one_liner.txt short_and_sweet.txt > cold_storage.txt
$ cat cold_storage.txt
One is the loneliest number.
This is a good test file.

It only has 3 short lines.

Don't forget that STDIN is a perfectly valid file, just like any other. Unless given a path as an argument cat will try STDIN. You can type into STDIN like a rudimentary text editor and stop by sending an EndOfFile character with a Control-d keystroke.

$ cat > hello.txt
hello
world<C-d>
$ cat hello.txt
hello
world

Let's not forget all the options! Check the man page for the full list. Here's a few highlights:

Number your lines with -n.

$ cat -n short_and_sweet.txt
     1	This is a good test file.
     2
     3	It only has 3 short lines.

Number your non-blank lines with -b.

$ cat -n short_and_sweet.txt
     1	This is a good test file.

     2	It only has 3 short lines.

Display an end of line character, $, at the end of each line with -e.

$ cat -e short_and_sweet.txt
This is a good test file.$
$
It only has 3 short lines.$

Last, but certainly not least, cat will give you this terrible help text that causes you to actually check the man page.

$ cat --help
cat: illegal option -- -
usage: cat [-benstuv] [file ...]

What Now?

This kata is not algorithmically hard, but rather full of small details. It is going to force you to explore some of Rust's file and io libraries. Don't be afraid to spend a few minutes reading documentation.

Start small. Work on the smallest piece of cat's functionality you can think to break off. You don't need to fully generalize file handling with STDIN/STDOUT immediately. At what point in argument/option parsing do you reach for a third party library? You probably don't for the most basic functionality.

If you think this is simple or finish early, make sure your solution works with unix pipes. Can you make it operate in a small, constant memory footprint?

This repo should compile, however uselessly:

$ cargo run
   Compiling cat v0.1.0 (file:///Users/alex/src/rust/katas/cat)
     Running `target/debug/cat`
usage: cat [-benstuv] [file ...]

Remember that you can always check your work by comparing the output of cargo run to the system's cat command.

cat-kata's People

Contributors

saterus avatar stellara avatar

Watchers

 avatar

Forkers

stellara

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.