Pomodoro Timer
Recently, I finally got around to completing a project. I had started work on a simple pomodoro timer in the terminal, but I only got as far as the backend timer portion of it. However, I finally got a basic version of it working and I thought for posterity’s sake I would go through my code.
The code is located on my git repository at https://git.foswret.com/beefsteak/. I called it beefsteak because beefsteak is a variety of tomato, and the name sounds very un-like the functionality of the program and the pomodoro method in general. The program is coded in C and platform compatible to the best of my ability.
The Code
|
|
^^^
This first chunk is defining time variables and the clear_stream() function. clear_stream() allows the timer to continuously be on the same line after every second change. For the variables, the default work session timing is 50 minutes with each break being 10 minutes. These constants are turned into seconds, which is usable with the next function.
|
|
^^^
This is probably the most important part of the whole program. The actual timer value is just an integer value for seconds and the time remaining is printed using this function. For every second that goes by, this function is called again, but with the seconds variable being decreased by one.
|
|
^^^
The send_notification function does what it says: send a notification. It uses the libnotify library, which is defined in the org.freedesktop.Notifications Desktop Specification. I have only tested this program out with dunst, but if it works with that I’m pretty sure it will function with desktop environment notifiers.
|
|
^^^ Putting the program together. Basically:
- Repeat the following for however many sessions you want (Ex. 3):
- Send a notification, saying work has begun
- Every one second, decrease the remaining work time by one second
- Once time has reached zero, switch the timer over to the
break_time_secondsvariable and send a notification. Decrease time by one every second. Until it reaches zero.
The Future
In the future, I want to add an audible alarm that says work has finished. I also want a key to be pressed in order for the timers to switch. This timer isn’t perfect, but it works well enough and is pretty similar to a physical timer.