2
\$\begingroup\$

I have made this program to simulate polybar's bspwm module. So far, it works flawlessly, however it consumes too much ram and cpu ie 16-30%. I suspect it is the while loop causing the trouble but what can be done to reduce resource usage ?

use strict;
use warnings;
use Switch;
use List::MoreUtils qw(first_index);

$SIG{INT}=sub{ die "goodnight !\n $!"; };
my $underline='#ffffff';
my $default_background='#51002f';
my $default_workspace_background='#727272';
my $default_workspace_foreground='#ffffff';
my $default_foreground='#ffffff';
my $focussed_background='#000000';
my $focussed_foreground='#ffffff';
my $occupied_background='#3a3a3a';
my $occupied_foreground='#ffffff';
my @desktops=qx/ bspc query -D /;
my @lemonbar_array=();
my $lemonbar_string="%{B$default_background}%{F$default_foreground}%{+u}    0x00400004  %{-u}%{B-}%{F-}";
my $date_string="%{c}%{B$focussed_background}%{F$default_foreground}%{+u}   %date   %{-u}%{B-}%{F-}";

my $add_this="%{+u}%{B$default_background}%{F$default_foreground} %{A:firefox:}  web  %{A}  %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:evince:}  pdf  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:libreoffice:}  doc  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:firefox youtube.com:}  youtube  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:firefox netflix.com:}  netflix  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:firefox gmail.com:}  gmail  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:firefox protonmail.com:}  protonmail  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:nautilus:}  files  %{A} %{B-}%{F-} %{B$default_background}%{F$default_foreground} %{A:urxvt:}  terminal  %{A} %{B-}%{F-}%{-u}";

for(my $i=0;$i<=9;$i++){ 
    push(@lemonbar_array,$lemonbar_string); 
}

my $current_monitor='';

while() { 
    my $date=gmtime();
    $date_string=~s/%date/$date/;
    $current_monitor=qx/bspc query -D -d/;
    my $index_current_monitor=first_index { $_ eq $current_monitor } @desktops;
    my $index_print=$index_current_monitor+1;
    $lemonbar_array[$index_current_monitor]=~s/(?<=B)#.{6}/$focussed_background/;
    $lemonbar_array[$index_current_monitor]=~s/(?<=F)#.{6}/$focussed_foreground/;
    $lemonbar_array[$index_current_monitor]=~s/0x00.{6}/$index_print/;
    for(my $i=0;$i<10;$i++){
        if($desktops[$i] ne $current_monitor){
            my $i1=$i+1;
            $lemonbar_array[$i]=~s/0x00.{6}/$i1/;
            if(scalar(my @n=qx/bspc query -N -d $desktops[$i]/) gt 0){ 
                $lemonbar_array[$i]=~s/(?<=B)#\w{6}/$occupied_background/;
                $lemonbar_array[$i]=~s/(?<=F)#\w{6}/$occupied_foreground/;
                }
            else{
                $lemonbar_array[$i]=~s/(?<=B)#\w{6}/$default_workspace_background/;
                $lemonbar_array[$i]=~s/(?<=F)#\w{6}/$default_workspace_foreground/;



            }
        }
    }

    print "%{l}@lemonbar_array $date_string %{r}$add_this\n";
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Don't link to programs. Include the code here so we don't depend on an external site. \$\endgroup\$ – choroba Oct 26 '18 at 8:16
  • \$\begingroup\$ As for cpu, cut down number of shell calls per minute. \$\endgroup\$ – mpapec Oct 27 '18 at 18:52
  • 1
    \$\begingroup\$ Don't use Switch! The POD states: "There are undoubtedly serious bugs lurking somewhere in code this funky". \$\endgroup\$ – toolic Nov 14 '18 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.