Advertisement
Guest User

Simple Pomodoro Timer

a guest
Feb 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.55 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # This script shows a countdown timer of 3 different values.
  4. # It starts with a 25 minute countdown ($work countdown)
  5. # At the end of 25 minutes notify-send is called to
  6. # alert you that it's break time. A new countdown timer
  7. # is started based on whether it's a long break or a
  8. # short break.
  9. # The type of break is determined by how many breaks
  10. # have been taken. Every 4th break is 15min, otherwise
  11. # it's 5min.
  12. # https://en.wikipedia.org/wiki/Pomodoro_Technique
  13.  
  14. use Modern::Perl;
  15. use IPC::System::Simple qw( systemx );
  16.  
  17. my $work_countdown  = 25*60; # in seconds
  18. my $short_countdown = 5*60;
  19. my $long_countdown  = 15*60;
  20. my $break_tracker = 1;
  21. $| = 1; # This ensures the countdown gets updated every second.
  22.  
  23. while (1) {
  24.     countdown($work_countdown);
  25.  
  26.     if ($break_tracker % 4) {
  27.         systemx('notify-send', 'long-break');
  28.         countdown($long_countdown);
  29.     }
  30.     else {
  31.         systemx('notify-send', 'short-break');
  32.         countdown($short_countdown);
  33.     }
  34.  
  35.     systemx('notify-send', 'back_to_work');
  36.     $break_tracker++;
  37. }
  38.  
  39. # This sub was adapted from code found at
  40. # https://stackoverflow.com/questions/25835958/perl-countdown
  41. sub countdown {
  42.     my ($sec) = @_;
  43.     my $beginTime = time;
  44.     my $endTime = $beginTime + $sec;
  45.  
  46.     for (;;) {
  47.         my $time = time;
  48.         last if ($time >= $endTime);
  49.         printf("\r%02d:%02d:%02d",
  50.            0,
  51.            ($endTime - $time) / 60 % ($sec),
  52.            ($endTime - $time) % 60,
  53.         );
  54.  
  55.         sleep(1);
  56.     }
  57. }
  58.  
  59.  
  60. exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement