/[scripts]/trunk/dwm-status.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/dwm-status.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 59 by dpavlin, Fri Jan 18 19:10:10 2008 UTC revision 135 by dpavlin, Wed Mar 31 06:55:19 2010 UTC
# Line 16  my $acpi_every = 10; Line 16  my $acpi_every = 10;
16  my $disk_blk_size = 512;  my $disk_blk_size = 512;
17    
18  my $debug = shift @ARGV;  my $debug = shift @ARGV;
19    my $awesome = 0;
20    my $dzen = 1;
21    
22  $|=1;  $|=1;
23    
# Line 27  sub proc2hash { Line 29  sub proc2hash {
29          while(<$fh>) {          while(<$fh>) {
30                  chomp;                  chomp;
31                  my ( $key, $value ) = split(/:\s+/, $_, 2);                  my ( $key, $value ) = split(/:\s+/, $_, 2);
32                  $value =~ s/ m[VW]h*$//;                  $value =~ s/ m[AVW]h*$//;
33                  $h->{$key} = $value;                  $h->{$key} = $value;
34          }          }
35          warn "$f ",dump( $h ) if ( $debug );          warn "$f ",dump( $h ) if ( $debug );
# Line 66  find_proc_acpi('battery', sub { Line 68  find_proc_acpi('battery', sub {
68          }          }
69  });  });
70    
71    my $unit_colors = {
72            'b' => '#ff0000',
73            'k' => '#ffff00',
74            'M' => '#00ff00',
75            'G' => '#0000ff',
76    };
77    
78  sub unit {  sub unit {
79          my $v = shift;          my ($v,$align) = @_;
80            $align ||= '';
81    
82          warn "unit( $v )\n" if ($debug);          warn "unit( $v )\n" if ($debug);
83    
# Line 79  sub unit { Line 89  sub unit {
89                  $v /= 1024;                  $v /= 1024;
90          }          }
91    
92            my $unit = $units[$o];
93    
94            my $out = '';
95            $out .= '^fg(' . $unit_colors->{$unit} . ')';
96    
97          if ( $v >= 1 ) {          if ( $v >= 1 ) {
98                  return sprintf("%d%s", $v, $units[$o]);                  $out .= sprintf("%${align}5d",$v);
99          } elsif ( $v == 0 ) {          } elsif ( $v == 0 ) {
100                  return '';                  $out = ' ' x 5;
101          } else {          } else {
102                  return sprintf("%.1f%s", $v, $units[$o]);                  $out .= sprintf("%${align}5.1f", $v);
103          }          }
104    
105            $unit = '^fg(#444444)' . $unit if $dzen;
106            $out =~ s/\s(\s*)$/$unit$1/ unless $out =~ m/\s{5}/;
107            $out .= '^fg(#ffffff)' if $dzen;
108    
109            return $out;
110  }  }
111    
112  my ( $lrx, $ltx ) = ( 0, 0 );  my ( $lrx, $ltx ) = ( 0, 0 );
113  my ( $ld_r, $ld_w ) = ( 0, 0 );  my ( $ld_r, $ld_w ) = ( 0, 0 );
114  my $bat;  my $bat = '';
115    
116  my $i = 0;  my $i = 0;
117    my $sys_fs = '/sys/class/power_supply/BAT0';
118    
119  while ( 1 ) {  while ( 1 ) {
120          my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());          my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());
121    
122          if ( $i % $acpi_every == 0 && $proc_acpi_battery ) {          if ( $i % $acpi_every == 0 ) {
123                    my $sysfs_path = glob "$sys_fs/*_full";
124                    if ( $sysfs_path ) {
125    
126                            my $full = read_file( $sysfs_path );
127                            $sysfs_path =~ s/_full/_now/;
128                            my $now = read_file( $sysfs_path );
129                            $bat = sprintf("%2d%%", $now * 100 / $full );
130    
131                  my $state = proc2hash( "$proc_acpi_battery/state" );                  } elsif ( $proc_acpi_battery ) {
132    
133                  if ( $state->{'present rate'} != 0 ) {                          my $state = proc2hash( "$proc_acpi_battery/state" );
                         my $info = proc2hash( "$proc_acpi_battery/info" );  
134    
135                          my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};                          if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) {
136                                    my $info = proc2hash( "$proc_acpi_battery/info" );
137    
138                          my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );                                  my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
                         $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );  
139    
140                          warn "time = $time\n" if ($debug);                                  my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
141                                    $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );
142    
143                          my $hh = int( $time );                                  warn "time = $time\n" if ($debug);
                         my $mm = int( ( $time - $hh ) * 60 );  
                         my $ss = ( $time * 3600 ) % 60;  
144    
145                          $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",                                  my $hh = int( $time );
146                                  substr($state->{'charging state'},0,1),                                  my $mm = int( ( $time - $hh ) * 60 );
147                                  $pcnt * 100, $hh, $mm, $ss,                                  my $ss = ( $time * 3600 ) % 60;
                                 $state->{'present rate'} / 1000  
                         );  
148    
149                          } else {                                  $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!",
150                                  $bat = '';                                          substr($state->{'charging state'},0,1),
151                                            $pcnt * 100, $hh, $mm, $ss,
152                                            $state->{'present rate'} / 1000
153                                    );
154                          }                          }
155          } else {                  }
                 $bat =~ s/!(\|\s)$/ $1/;  
156          }          }
157          $i++;          $i++;
158    
# Line 146  while ( 1 ) { Line 172  while ( 1 ) {
172    
173          foreach ( split(/\n/, $net) ) {          foreach ( split(/\n/, $net) ) {
174                  s/^\s+//;                  s/^\s+//;
175                  s/:\s+/:/;                  s/:\s*/ /;
176                  my @n = split(/\s+/, $_, 17);                  my @n = split(/\s+/, $_, 17);
177                  next unless ( $n[0] =~ s!(eth\d|ath\d):!! );                  #next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
178                    next unless $n[1] =~ m{^\d+$};
179    
180                  warn dump( @n ) if ($debug);                  warn dump( @n ) if ($debug);
181                  $rx += $n[0];                  $rx += $n[1];
182                  $tx += $n[8];                  $tx += $n[9];
183          }          }
184          warn "rx: $rx tx: $tx\n" if ($debug);          warn "rx: $rx tx: $tx\n" if ($debug);
185    
# Line 178  while ( 1 ) { Line 205  while ( 1 ) {
205          my $d_write = ( $d_w - $ld_w ) / $dt;          my $d_write = ( $d_w - $ld_w ) / $dt;
206          ( $ld_r, $ld_w ) = ( $d_r, $d_w );          ( $ld_r, $ld_w ) = ( $d_r, $d_w );
207    
208          my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",          my $out = join( $dzen ? ' ^fg(#333333)|^fg(#ffffff) ' : ' | ',
                 $s,  
209                  $load,                  $load,
210                  unit( $d_read ), unit( $d_write ),                  unit( $d_read ) . ' D ' . unit( $d_write, '-' ),
211                  unit( $net_rx ), unit( $net_tx ),                  unit( $net_rx ) . ' > ' . unit( $net_tx, '-' ),
212                  $bat, $temp,                  $bat, $temp,
213                    $s,
214          );          );
215    
216          print $out;  
217            print "$out\n";
218            if ( $awesome ) {
219                    open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!";
220                    print $fh
221                            $awesome == 3   ? qq{mytextbox.text="$out"\n}
222                                            : "0 widget_tell mystatusbar dwm-status text $out\n"
223                                            ;
224                    close($fh);
225            }
226    
227          sleep $dt;          sleep $dt;
228  }  }

Legend:
Removed from v.59  
changed lines
  Added in v.135

  ViewVC Help
Powered by ViewVC 1.1.26