DEV Community

loading...

Build a Command-Line Pomodoro Timer in Node.js

matthewcaseres profile image Matthew Caseres ・2 min read

Alt Text

Here is the source code - https://github.com/Open-EdTech/node-pomodoro

Motivations

When I started learning about task execution systems like Pomodoro, I thought putting together a CLI myself would be a great way to create a sense of ownership and pride in my work. Also, it would let me ship the features I needed.

Requirements

  • Tracks progress towards completing a given number of hours of work
  • Sets goals for continuous work block and break lengths
  • Looks nice so productivity streamers can use it
  • Emits logs for analysis at a future date

Design

Classes vs. Functional

Originally I just made a countdown timer and built off of that. I usually don't write classes, but having a mutable internal state was a really easy way of thinking about the problem.

Start Small, Build Up

I'm not very experienced with node.js, so I started by displaying a simple countdown to get my feet wet. The general idea is to use setInterval to set a 1000 millisecond delay between each second on the clock. Originally I used process.stdout.write but once I found a library for displaying progress bars I realized that the progress bars would be a great way of displaying information about your study goals.

Using npm libraries

I used enquirer to take the user input.
Here's what it looks like -
Alt Text

The display is simply the multi-progress bar from the CLI-progress library. This is way better than writing to the console yourself. Here's the block I'm on right now -
Alt Text

Write to a file

The code writes to a file in the sessions folder of the project. The filename is the ending time of the session. The contents look like this -

Alt Text

This way I can perform an analysis at a later date to track the time I spend contributing to open-source.

Thanks for reading!

If you give it a try be sure to read the readme. https://github.com/Open-EdTech/node-pomodoro

Discussion (0)

pic
Editor guide