aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2025-09-16 23:27:18 +0200
committerGuillermo Ramos2025-09-17 00:50:25 +0200
commit376c0d834474f657ed9598f1b765cf0bcd6ad6b6 (patch)
tree00e0bfc396267b30fa36eb0f732d32f1923be62e
parent7f3db6f949d632bb9f21436bf6e52a949f4e21aa (diff)
downloadcli-376c0d834474f657ed9598f1b765cf0bcd6ad6b6.tar.gz
capibarra: colors
-rwxr-xr-xcapibarra65
1 files changed, 50 insertions, 15 deletions
diff --git a/capibarra b/capibarra
index 88d2a18..93efb37 100755
--- a/capibarra
+++ b/capibarra
@@ -17,6 +17,21 @@ $| = 1;
################################################################################
# Formatting
+use constant {
+ COLOR_WARN => "#ffff44",
+ COLOR_ERR => "#ff6600",
+};
+
+sub color_thresholds($x, $warn, $err) {
+ if ($x >= $err) {
+ return { type => 'background', val => COLOR_ERR };
+ } elsif ($x >= $warn) {
+ return { type => 'color', val => COLOR_WARN };
+ } else {
+ return 0;
+ }
+}
+
sub pango_warning($txt, $enabled) {
return $enabled ? "<span background='red'>$txt</span>" : $txt;
}
@@ -73,7 +88,7 @@ sub show_time_delta($section, $time0, $computed) {
# compute => sub {
# read($loadf, my $loads, 14);
# seek($loadf, 0, 0);
-# return sprintf ("CPU %s/%s/%s", split ' ', $loads);
+# return { text => sprintf ("CPU %s/%s/%s", split ' ', $loads) };
# }
# );
@@ -83,7 +98,11 @@ my %mod_cpu_openbsd = (
compute => sub {
my $loads = `sysctl -n vm.loadavg`;
chomp $loads;
- return sprintf ("CPU %s/%s/%s", split ' ', $loads);
+ my @loads = split ' ', $loads;
+ return {
+ text => sprintf ("CPU %s/%s/%s", @loads),
+ color => color_thresholds($loads[1], 1, 3),
+ };
}
);
@@ -92,7 +111,7 @@ my %mod_cpu_openbsd = (
# period => 5,
# compute => sub {
# my $mused = `free | awk '/^Mem:/ {printf("%d", 100 * (\$3/\$2))}'`;
-# return pango_warning(sprintf("MEM %s%%", $mused), $mused > 90);
+# return { text => pango_warning(sprintf("MEM %s%%", $mused), $mused > 90) };
# }
# );
@@ -104,7 +123,10 @@ my %mod_mem_openbsd = (
my ($mtotal) = $uvmexp =~ /([0-9]+) pages managed/;
my ($mused) = $uvmexp =~ /([0-9]+) pages active/;
$mused = 100 * $mused / $mtotal;
- return pango_warning(sprintf("MEM %d%%", $mused), $mused > 90);
+ return {
+ text => sprintf("MEM %d%%", $mused),
+ color => color_thresholds($mused, 70, 90),
+ };
}
);
@@ -120,7 +142,7 @@ my %mod_mem_openbsd = (
# 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_linux(@$_) } @wired, @wireless;
+# return { text => join ' | ', map { render_iface_linux(@$_) } @wired, @wireless };
# }
# );
@@ -161,7 +183,7 @@ my %mod_net_openbsd = (
# print Dumper(\%ifaces);
# print Dumper(\@wired);
# print Dumper(\@wireless);
- return join ' ยท ', map { render_iface_openbsd(\%ifaces, $_) } sort(@wired), @wireless;
+ return { text => join ' ยท ', map { render_iface_openbsd(\%ifaces, $_) } sort(@wired), @wireless };
}
);
@@ -177,7 +199,7 @@ my %mod_net_openbsd = (
# seek($bcapf, 0, 0);
# my $batcharging = <$bstatf> eq "Charging\n" ? 'โšก' : '';
# seek($bstatf, 0, 0);
-# return pango_warning(sprintf("๐Ÿ”‹%s%s", $batlevel, $batcharging), $batlevel < 10);
+# return { text => pango_warning(sprintf("๐Ÿ”‹%s%s", $batlevel, $batcharging), $batlevel < 10) };
# }
# );
@@ -193,7 +215,10 @@ my %mod_bat_openbsd = (
my $batmax = obsd_get_sysctl('hw.sensors.acpibat0.watthour0');
my $batcur = obsd_get_sysctl('hw.sensors.acpibat0.watthour3');
my $batlevel = 100 * $batcur / $batmax;
- return pango_warning(sprintf("๐Ÿ”‹%d%%%s", $batlevel, $batcharging), $batlevel < 10);
+ return {
+ text => sprintf("๐Ÿ”‹%d%%%s", $batlevel, $batcharging),
+ color => color_thresholds(100 - $batlevel, 70, 90)
+ };
}
);
@@ -201,9 +226,10 @@ my %mod_bat_openbsd = (
# name => 'AUDIO',
# period => 2,
# compute => sub {
-# return sprintf "%s%s",
+# return { text => sprintf "%s%s",
# pango_onoff('๐Ÿ”Š', do {`volctl vol-on`; $? == 0}),
-# pango_onoff('๐ŸŽค', do {`volctl mic-on`; $? == 0});
+# pango_onoff('๐ŸŽค', do {`volctl mic-on`; $? == 0})
+# };
# }
# );
@@ -214,9 +240,10 @@ my %mod_audio_openbsd = (
my $audioinfo = `sndioctl`;
my ($muted) = $audioinfo =~ /output\.mute=([01])/;
my ($micmuted) = $audioinfo =~ /input\.mute=([01])/;
- return sprintf "%s %s",
+ return { text => sprintf "%s %s",
txt_onoff('๐Ÿ”Š', $muted == 0),
- txt_onoff('๐ŸŽค', $micmuted == 0);
+ txt_onoff('๐ŸŽค', $micmuted == 0)
+ };
}
);
@@ -226,7 +253,7 @@ my %mod_date = (
compute => sub {
my $date = `date +'%Y-%m-%d %H:%M'`;
chomp $date;
- return $date;
+ return { text => $date };
}
);
@@ -243,6 +270,14 @@ foreach my $arg (@ARGV) {
}
}
+printf("{ \"version\": 1 }\n[\n");
+
+sub render_section($result) {
+ my $color = $result->{color};
+ my $color = $color ? ",\"$color->{type}\":\"$color->{val}\"" : '';
+ return sprintf "{\"full_text\":\"%s\",\"separator\":false$color}", $result->{text};
+}
+
my $counter = 0;
while (1) {
my @sections;
@@ -257,12 +292,12 @@ while (1) {
} else {
$result = $cache{$mod->{name}};
}
- push(@sections, $result);
+ push(@sections, render_section($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));
+ printf("[%s],\n", join(',{"full_text":"ยท","separator":false},', @sections));
$counter += 1;
sleep 1;