Created
April 6, 2012 04:53
-
-
Save brycied00d/2317019 to your computer and use it in GitHub Desktop.
la11111's dwstat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use threads; | |
use threads::shared; | |
my $script_dir = $ENV{"HOME"}."/.dwm/dwstat"; | |
my $divider = " | "; | |
my @scripts; | |
my @timeouts; | |
my @outputs :shared; | |
#prints the output to the status bar | |
sub update () { | |
my $out_string = $outputs[0]; | |
foreach (1..$#scripts) { | |
if ($outputs[$_]) { | |
$out_string .= $divider.$outputs[$_]; | |
} | |
} | |
system("xsetroot -name '$out_string'"); | |
} | |
#reads scripts dir, assigns scripts indices, | |
#figures out timeouts in seconds for each script | |
sub setup_scripts () { | |
#list script files in script dir | |
@scripts = `ls $script_dir`; | |
chomp(@scripts); | |
#figure out sleep duration for each script in seconds | |
#must be on line 2 in a comment in the script, no spaces, | |
#a pound sign followed by a series of digits followed by either or- h,m,s | |
foreach (0..$#scripts) { | |
open (SCRIPT, "$script_dir/$scripts[$_]") or die($!); | |
<SCRIPT>; #skip first line | |
my $t = <SCRIPT>; #read in second line | |
my $unit; | |
chomp $t; | |
if ($t =~ /^#(\d+)([hms])$/) { | |
$timeouts[$_] = $1; | |
$unit = $2; | |
} elsif ($t =~ /^#r$/) { | |
$timeouts[$_] = 0; | |
$unit = "r"; | |
} else { | |
die("can't parse refresh value!\n"); | |
} | |
close(SCRIPT); | |
if ($unit eq "m") { | |
$timeouts[$_]*=60; | |
} | |
if ($unit eq "h") { | |
$timeouts[$_]*=3600; | |
} | |
} | |
} | |
sub thread_main() { | |
my $i = pop; | |
if ($timeouts[$i] == 0) { | |
my $l; | |
open(INPUT, "$script_dir/$scripts[$i] |"); | |
while($l = <INPUT>){ | |
chomp $l; | |
$outputs[$i] = $l; | |
update(); | |
} | |
close(INPUT); | |
} else { | |
while(1) { | |
$outputs[$i] = `$script_dir/$scripts[$i]`; | |
chomp $outputs[$i]; #for in case there's and \n's | |
update(); | |
sleep $timeouts[$i]; | |
} | |
print "bye! $script_dir/$scripts[$i]\n"; | |
} | |
} | |
sub main() { | |
my @threads; | |
setup_scripts(); | |
foreach my $i (0..$#scripts) { | |
push(@threads, threads->new(\&thread_main, $i)); | |
} | |
foreach my $t (@threads) { | |
$t->join(); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment