/[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

Annotation of /trunk/dwm-status.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations)
Sun May 27 08:39:20 2007 UTC (15 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 2367 byte(s)
calulate time correctly, hide battery when charged

1 dpavlin 30 #!/usr/bin/perl -w
2    
3     # dwm-status.pl
4     #
5     # 05/26/07 23:08:31 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
6    
7     use strict;
8     use POSIX qw/strftime/;
9     use File::Slurp;
10     use Time::HiRes;
11     use Data::Dump qw/dump/;
12    
13     my $dt = 3;
14 dpavlin 31 my $acpi_every = 10;
15 dpavlin 30
16 dpavlin 31 my $debug = shift @ARGV;
17 dpavlin 30
18     $|=1;
19    
20     sub proc2hash {
21     my $f = shift;
22     open(my $fh, '<', $f) || die "can't open $f: $!";
23     my $h;
24     while(<$fh>) {
25     chomp;
26     my ( $key, $value ) = split(/:\s+/, $_, 2);
27     $value =~ s/ m[VW]h*$//;
28     $h->{$key} = $value;
29     }
30     warn dump( $h ) if ( $debug );
31     return $h;
32     }
33    
34     sub unit {
35     my $v = shift;
36 dpavlin 31
37     warn "unit( $v )\n" if ($debug);
38    
39 dpavlin 30 my @units = qw/b k m g/;
40     my $o = 0;
41    
42     while ( ( $v / 1024 ) >= 1 ) {
43     $o++;
44     $v /= 1024;
45     }
46    
47 dpavlin 31 if ( $v >= 1 ) {
48     return sprintf("%d%s/s", $v, $units[$o]);
49     } else {
50     return sprintf("%.1f%s/s", $v, $units[$o]);
51     }
52 dpavlin 30 }
53    
54     my ( $lrx, $ltx ) = ( 0, 0 );
55     my $bat;
56    
57     my $i = 0;
58    
59     while ( 1 ) {
60     my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());
61    
62     if ( $i % $acpi_every == 0 ) {
63    
64 dpavlin 32 my $state = proc2hash( '/proc/acpi/battery/BAT0/state' );
65 dpavlin 30
66 dpavlin 32 if ( $state->{'present rate'} != 0 ) {
67     my $info = proc2hash( '/proc/acpi/battery/BAT0/info' );
68 dpavlin 30
69 dpavlin 32 my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
70 dpavlin 30
71 dpavlin 32 my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
72 dpavlin 31
73 dpavlin 32 warn "time = $time\n" if ($debug);
74 dpavlin 30
75 dpavlin 32 my $hh = int( $time );
76     my $mm = int( ( $time - $hh ) * 60 );
77     my $ss = ( $time * 3600 ) % 60;
78 dpavlin 30
79 dpavlin 32 $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
80     substr($state->{'charging state'},0,1),
81     $pcnt * 100, $hh, $mm, $ss,
82     $state->{'present rate'} / 1000
83     );
84    
85     } else {
86     $bat = '';
87     }
88 dpavlin 30 } else {
89 dpavlin 32 $bat =~ s/!(\|\s)$/ $1/;
90 dpavlin 30 }
91     $i++;
92    
93     my $load = read_file('/proc/loadavg');
94     chomp( $load );
95     $load =~ s!\s\d+/\d+.*!!;
96    
97     my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature');
98     chomp( $temp );
99     $temp =~ s!^.*:\s+!!;
100    
101     my $net = read_file('/proc/net/dev');
102     my ( $rx, $tx ) = ( 0,0 );
103    
104     foreach ( split(/\n/, $net) ) {
105     s/^\s+//;
106     my @n = split(/\s+/, $_, 17);
107     next unless ( $n[0] =~ m!(eth\d|ath\d):! );
108    
109     warn dump( @n ) if ($debug);
110     $rx += $n[1];
111     $tx += $n[9];
112     }
113     warn "rx: $rx tx: $tx\n" if ($debug);
114    
115     my $r = ( $rx - $lrx ) / $dt;
116     my $t = ( $tx - $ltx ) / $dt;
117     ( $lrx, $ltx ) = ( $rx, $tx );
118    
119 dpavlin 32 printf "%s | %s |%6s >> %-6s| %s%s\n", $s, $load, unit( $r ), unit( $t ), $bat, $temp;
120 dpavlin 30
121     sleep $dt;
122     }
123    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26