bookercodes / dotfiles

🎚 Configuration files for macOS and Linux
745 stars 67 forks source link

Problem with volume script #25

Closed mnissov closed 6 years ago

mnissov commented 6 years ago

The index for my default sink is 0. When passing 0 as an argument to your volume script I get an error message returned, and I'm not entirely sure how to fix it.

bookercodes commented 6 years ago

Sorry, I haven't used this script in a while and am unable to help :(

petrleocompel commented 6 years ago

@mnissov It's pretty easy. You need to chande that 5 line to this

my $sink=$ARGV[0];

Because in if statement 0 == false thats the reason why it died. Also at the end is missing exit command. So after 43 line we need to exit 0;

So in the end whole script will look like

#!/usr/bin/env perl 

## The sink we are interested in should be given as the 
## 1st argument to the script. (No die because 0 is allowed)
my $sink=$ARGV[0];

## If the script has been run with a second argument,
## that argument will be the volume threshold we are checking
my $volume_limit=$ARGV[1]||undef;

## Run the pactl command and save the output in 
## ther filehandle $fh
open(my $fh, '-|', 'pactl list sinks');

## Set the record separator to consecutive newlines (same as -000)
## this means we read the info for each sink as a single "line".
$/="\n\n";

## Go through the pactl output
while (<$fh>) {
    ## If this is the sink we are interested in
    if (/#$sink/) {
        ## Extract the current colume of this sink
        /Volume:.*?(\d+)%/;
        my $volume=$1;
        ## If the script has been run with a second argument,
        ## check whether the volume is above or below that
        if ($volume_limit) {
            ## If the volume os greater than or equal to the
            ## value passed, print "y"
            if ($volume >= $volume_limit) {
               print "y\n";
                exit 0;
            }
            else {
                print "n\n";
                exit 1;
            }
        }   
        ## Else, if the script has been run with just one argument,
        ## print the current volume + exit
        else {
            print "$volume%\n";
            exit 0;
        }
    }
}

Buala. Enjoy working volume :slightly_smiling_face: