summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2021-03-08 15:18:27 -0700
committerSean Whitton <spwhitton@spwhitton.name>2021-03-08 15:18:27 -0700
commit1f7f9f40a2203c50b1e5604816292e721aeb6b10 (patch)
tree28d7b13d53763745fa6f4b28cd5c3ded5450f11e /scripts
parent68ec754d8c5953532a39a69b8ca11d9dd4d724a0 (diff)
downloaddotfiles-1f7f9f40a2203c50b1e5604816292e721aeb6b10.tar.gz
i3status-wrapper: also include caffeinated mark
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/desktop/i3status-wrapper48
1 files changed, 26 insertions, 22 deletions
diff --git a/scripts/desktop/i3status-wrapper b/scripts/desktop/i3status-wrapper
index 2b891591..fffde8d1 100755
--- a/scripts/desktop/i3status-wrapper
+++ b/scripts/desktop/i3status-wrapper
@@ -1,26 +1,16 @@
#!/usr/bin/env perl
-# vim:ts=4:sw=4:expandtab
-# © 2012 Michael Stapelberg, Public Domain
-# This script is a simple wrapper which prefixes each i3status line with custom
-# information. To use it, ensure your ~/.i3status.conf contains this line:
-# output_format = "i3bar"
-# in the 'general' section.
-# Then, in your ~/.i3/config, use:
-# status_command i3status | ~/i3status/contrib/wrapper.pl
-# In the 'bar' section.
+# originally based on Michael Stapelberg's i3status-wrapper; added forking
+# i3status ourselves, user@host, and scanning for caffeinated mark
+use 5.032;
use strict;
use warnings;
-# You can install the JSON module with 'cpan JSON' or by using your
-# distribution’s package management system, for example apt-get install
-# libjson-perl on Debian/Ubuntu.
use JSON;
use IO::Pipe;
use Sys::Hostname;
-# Don’t buffer any output.
$| = 1;
my $pipe = IO::Pipe->new;
@@ -39,21 +29,35 @@ print scalar <$pipe>;
# The second line contains the start of the infinite array.
print scalar <$pipe>;
+my $caffeinated;
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
my $hostname = hostname;
+expensive();
+$SIG{USR1} = sub { kill USR1 => $i3status; expensive() };
+
# Read lines forever, ignore a comma at the beginning if it exists.
while (my ($statusline) = (<$pipe> =~ /^,?(.*)/)) {
- # Decode the JSON-encoded line.
- my @blocks = @{decode_json($statusline)};
+ my @blocks = @{ decode_json $statusline };
+
+ unshift @blocks,
+ { full_text => "Caffeinated: $caffeinated", name => "caffeinated" }
+ if $caffeinated;
- # Prefix our own information (you could also suffix or insert in the
- # middle).
- @blocks = ({
- full_text => $username . "@" . $hostname,
- name => 'hostinfo'
- }, @blocks);
+ unshift @blocks,
+ { full_text => $username . "@" . $hostname, name => "hostinfo" };
- # Output the line as JSON.
print encode_json(\@blocks) . ",\n";
}
+
+sub expensive {
+ my %marks;
+ walk_tree(\%marks, decode_json `i3-msg -t get_tree`);
+ $caffeinated = exists $marks{caffeinated} ? $marks{caffeinated} : undef;
+}
+
+sub walk_tree {
+ my ($marks, $tree) = @_;
+ $marks->{$_} = $tree->{name} for $tree->{marks}->@*;
+ walk_tree($marks, $_) for $tree->{nodes}->@*;
+}