Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created August 23, 2019 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacoby/40486a81f75b7d5ef75191e9923c7a3c to your computer and use it in GitHub Desktop.
Save jacoby/40486a81f75b7d5ef75191e9923c7a3c to your computer and use it in GitHub Desktop.
Perl program that plays a sound based on error condition
#!/usr/bin/env perl
# tool to tell you when a long-running process is over
# usage:
# ding.pl cp -r ~/bigdir ~/otherdir
use feature qw{ say state } ;
use strict ;
use warnings ;
use utf8 ;
use DateTime ;
use DateTime::Duration ;
use lib '/home/jacoby/lib' ;
use Locked ;
use Notify qw{ notify } ;
use Pushover ;
use Polly qw{ say_message } ;
my $sounds = join '/', $ENV{HOME}, 'sounds' ;
my $ding = join '/', $sounds, 'ding.mp3' ;
my $error = join '/', $sounds, 'error.mp3' ;
my $icons = join '/', $ENV{HOME}, 'icons' ;
my $thmbup = join '/', $icons, 'thumbs_up.png' ;
my $thmbdn = join '/', $icons, 'thumbs_dn.png' ;
my $mpg123 = '/usr/bin/mpg123 --stereo --aggressive --quiet ';
if ( scalar @ARGV ) {
my $command = join ' ', @ARGV ;
my $start = DateTime->now() ;
report( system($command), $command, $start ) ;
}
else {
qx{$mpg123 $error} ;
say qq{No process to wait on} ;
}
sub report {
my ( $status, $command, $start ) = @_ ;
my $end = DateTime->now() ;
my $duration = $end->subtract_datetime_absolute($start) ;
my $seconds = $duration->delta_seconds() ;
my $proc = qq{process took $seconds seconds} ;
my $title = $status == 0 ? 'SUCCESS' : 'FAIL' ;
my $sound = $status == 0 ? $ding : $error ;
my $icon = $status == 0 ? $thmbup : $thmbdn ;
my $message ;
$message->{title} = $title ;
$message->{message} = qq{$command\n$proc} ;
$message->{icon} = $icon ;
if ( is_locked() == 0 ) {
notify($message) ;
qx{$mpg123 $sound} ;
}
else {
pushover($message) ;
say 'gone' ;
}
}
__DATA__
As I understand ding
if there's something in ARGV
get starting timestamp
try
sound = error
syscall ARGV
sound = ding
catch
if error, print error
get ending timestamp
find duration between both
if duration > 1 seconds
print elaptsed time
else
play ding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment