Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A perl script to notify oneself
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use feature 'switch';
use English '-no_match_vars';
$| = 1;
my $msg = '';
sub parse {
my $state = 'msg';
foreach my $arg (@ARGV) {
given ($state) {
when('msg') {
if ($arg =~ /at|in/) {
if (!$msg) {return}
$state = $arg;
} else {
$msg .= " $arg";
}
}
when('at') {
my ($_,$min,$hour,,,,,,) = localtime;
my ($newhour, $newmin) = split /:/, $arg;
my $hdiff = $newhour - $hour;
my $mdiff = $newmin - $min;
my $diff = (($hdiff * 60) + $mdiff) * 60;
if ($diff < 0) {$diff += 24 * 3600}
print "notification in $diff seconds\n";
sleep $diff;
return 1;
}
when('in') {
#TODO
}
}
}
}
sub executable {
foreach my $p (split /:/, $ENV{'PATH'}) {
if (-x $p.'/'.$_[0]) {return 1}
}
}
if (parse) {
if (executable('kdialog')) {
`kdialog --title Remember --passivepopup "$msg"`;
} elsif (executable('zenity')) {
`zenity --title=Remember --notification "$msg"`;
} else {
print $msg;
}
} else {
print {*STDERR} "Usage: $PROGRAM_NAME <thing-to-remember> [at <hour>:<min> | in [<hh> [h|hours] [and]] <mm> [m|minutes]]\n";
exit 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment