forked from pflanze/chj-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbat
executable file
·166 lines (137 loc) · 3.37 KB
/
bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl -w
# Sam Apr 18 14:13:16 CEST 2015
(my $email='XXX%YYY,ch')=~ tr/%,/@./;
use strict; use warnings FATAL => 'uninitialized';
our $loopsleep= 10; # seconds
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname
Show battery status.
Options:
-l|--loop show BAT0 percentage oneline with timestamp
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
use Getopt::Long;
our $verbose=0;
our $opt_loop;
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
"loop"=> \$opt_loop,
#"dry-run"=> \$opt_dry,
) or exit 1;
usage if @ARGV;
# XX move?
sub xset_once ($$$) {
my ($table,$keys,$val)=@_;
if (@$keys==1) {
exists $$table{$$keys[0]} ? die "key already set"
: ($$table{$$keys[0]}= $val);
} elsif (@$keys==2) {
exists $$table{$$keys[0]}{$$keys[1]} ? die "key already set"
: ($$table{$$keys[0]}{$$keys[1]}= $val);
} elsif (@$keys==2) {
exists $$table{$$keys[0]}{$$keys[1]}{$$keys[2]} ? die "key already set"
: ($$table{$$keys[0]}{$$keys[1]}{$$keys[2]}= $val);
} else {
die "don't know how to handle more than 3 keys at this time"
}
}
use Chj::xopen 'xopen_read';
sub contents ($) {
my ($path)=@_;
my $f= xopen_read $path;
my $cntref= $f->xcontentref;
$f->xclose;
chomp $$cntref;
$$cntref
}
sub pseudonumify {
my ($v)=@_;
length $v ? $v : -1
}
# /move
use Chj::xopendir;
our $power_base = "/sys/class/power_supply";
sub hardware_by_prefixes {
my $by_prefix={};
my $d= xopendir $power_base;
while (defined (my $item= $d->xnread)) {
my ($pref,$perhaps_num)= $item=~ /^(.+?)(\d*)\z/
or die "no match: '$item'";
xset_once $by_prefix, [$pref, $perhaps_num],
"$power_base/$item"; #spend some space..
}
for (keys %$by_prefix) {
$_ eq "AC" or $_ eq "BAT" or die "unknown type '$_'";
}
$by_prefix
}
our $by_prefix;
sub show_bat_percentage {
my ($prefix,$field)=@_;
my $now= &$field("energy_now");
my $full= &$field("energy_full");
printf "$prefix%3.1f\n", 100 * $now / $full;
}
sub kindnum2field {
my ($kind,$num)=@_;
my $path= $$by_prefix{$kind}{$num}
or die "do not have a $kind$num";
sub {
my ($name)=@_;
contents("$path/$name");
}
}
sub show {
my ($kind,$num)=@_;
print "$kind$num:\n";
my $field= kindnum2field ($kind,$num);
print " type: ", &$field("type"), "\n";
if ($kind eq "AC") {
print " online: ", &$field("online"), "\n";
} elsif ($kind eq "BAT") {
show_bat_percentage(" charge: ",$field);
} else {
die "bug"
}
print "\n";
}
$by_prefix= hardware_by_prefixes;
if ($opt_loop) {
my $lasthw= time;
my $rescan= sub {
my ($maybe_t)=@_;
my $t= $maybe_t || time;
# (could optimize _full file similarly.)
$lasthw= $t;
$by_prefix= hardware_by_prefixes;
};
while (1) {
eval {
my $t= time;
if ($t > ($lasthw + $loopsleep * 10)) {
&$rescan ($t);
}
my $field= kindnum2field ("BAT","0");
show_bat_percentage (localtime."\t", $field);
1
} || do {
print STDERR "note: $@";
&$rescan;
};
sleep $loopsleep;
}
} else {
for my $kind (sort { $a cmp $b } keys %$by_prefix) {
my $h= $$by_prefix{$kind};
for my $num (sort { pseudonumify($a) <=> pseudonumify($b) } keys %$h) {
show $kind, $num;
}
}
}
#use Chj::ruse;
#use Chj::Backtrace; use Chj::repl; repl;