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
|
#!/usr/bin/env perl
#
# Dependencies: iproute2 iw volctl
#
################################################################################
use strict;
use warnings;
use feature 'signatures';
use Time::HiRes qw<gettimeofday>;
# Turn off buffering
$| = 1;
################################################################################
# Formatting
sub pango_warning($txt, $enabled) {
return $enabled ? "<span background='red'>$txt</span>" : $txt;
}
sub pango_onoff($txt, $on) {
return $on ? $txt : "<span strikethrough='true'>$txt</span>";
}
sub render_iface($is_wireless, $name, $status, $addr) {
my $extra = '';
$extra .= " $addr" if $addr;
if ($is_wireless and $status eq 'UP') {
my ($ssid) = `iw dev $name link` =~ /SSID: ([^\n]*).*/;
my ($dbm) = `iw dev $name link` =~ /signal: ([0-9-]+ dBm)/;
$extra .= ' (' . $ssid . ', ' . $dbm . ')';
}
return pango_onoff($is_wireless eq 1 ? '๐' : '๐', $status eq 'UP') . $extra;
}
################################################################################
# Profiling
sub show_time_delta($section, $time0, $computed) {
my (undef, $time) = gettimeofday();
my $diff = ($time - $time0) / 1000;
my $extra = $computed ? ' (COMPUTED)' : '';
printf STDERR "Spent $diff milliseconds @ $section$extra\n";
}
################################################################################
# Modules
open(my $loadf, '<', '/proc/loadavg');
my %mod_cpu = (
name => 'CPU',
period => 5,
compute => sub {
read($loadf, my $loads, 14);
seek($loadf, 0, 0);
return sprintf ("CPU %s/%s/%s", split ' ', $loads);
}
);
my %mod_mem = (
name => 'MEMORY',
period => 5,
compute => sub {
my $mused = `free | awk '/^Mem:/ {printf("%d", 100 * (\$3/\$2))}'`;
return pango_warning(sprintf("MEM %s%%", $mused), $mused > 90);
}
);
my %mod_net = (
name => 'NETWORK',
period => 4,
compute => sub {
my @wired = ();
my @wireless = ();
for (split("\n", `ip --br a`)) {
next if substr($_, 0, 2) eq 'lo'; # prevent regex
my ($if, $status, $addr) = $_ =~ /^(\S+)\s+(\S+)(?:\s+(\S+))?/;
push @wired, [0, $if, $status, $addr] if (substr($if, 0, 2) eq 'en');
push @wireless, [1, $if, $status, $addr] if (substr($if, 0, 2) eq 'wl');
}
return join ' | ', map { render_iface(@$_) } @wired, @wireless;
}
);
my $BATDIR = glob '/sys/class/power_supply/BAT?';
open(my $bcapf, '<', "$BATDIR/capacity");
open(my $bstatf, '<', "$BATDIR/status");
my %mod_bat = (
name => 'BATTERY',
period => 3,
compute => sub {
my $batcharging = <$bstatf> eq "Charging\n" ? 'โก' : '';
seek($bstatf, 0, 0);
my $batlevel = <$bcapf>;
chomp $batlevel;
seek($bcapf, 0, 0);
return pango_warning(sprintf("๐%s%s", $batlevel, $batcharging), $batlevel < 10);
}
);
my %mod_audio = (
name => 'AUDIO',
period => 2,
compute => sub {
return sprintf "%s%s",
pango_onoff('๐', do {`volctl vol-on`; $? == 0}),
pango_onoff('๐ค', do {`volctl mic-on`; $? == 0});
}
);
my %mod_date = (
name => 'DATE',
period => 2,
compute => sub {
my $date = `date +'%Y-%m-%d %H:%M'`;
chomp $date;
return $date;
}
);
################################################################################
# Main
my @mods = (\%mod_cpu, \%mod_mem, \%mod_net, \%mod_bat, \%mod_audio, \%mod_date);
my %cache;
my $profile = 0;
foreach my $arg (@ARGV) {
if ($arg eq '--profile') {
$profile = 1;
}
}
my $counter = 0;
while (1) {
my @sections;
my (undef, $time0) = gettimeofday() if $profile;
foreach my $mod (@mods) {
my (undef, $time) = gettimeofday() if $profile;
my $result;
if ($counter % $mod->{period} == 0) {
$result = &{$mod->{compute}};
$cache{$mod->{name}} = $result;
} else {
$result = $cache{$mod->{name}};
}
push(@sections, $result);
show_time_delta($mod->{name}, $time, $counter % $mod->{period} == 0) if $profile;
}
show_time_delta('TOTAL', $time0, 0) if $profile;
printf("%s\n", join(' ยท ', @sections));
$counter += 1;
sleep 1;
}
# vim: set sw=4
|