From eecd0a0ed11df1878d588a25cf1b17838ed4705b Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Fri, 12 Apr 2024 08:43:45 +0800 Subject: i3status-wrapper: tidying --- scripts/desktop/i3status-wrapper | 46 ++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/scripts/desktop/i3status-wrapper b/scripts/desktop/i3status-wrapper index 22b78bd0..7298fb2f 100755 --- a/scripts/desktop/i3status-wrapper +++ b/scripts/desktop/i3status-wrapper @@ -27,7 +27,6 @@ use AnyEvent; use AnyEvent::I3 ":all"; use Sys::Hostname; use POSIX "floor", "mkfifo"; -use Fcntl; use File::Basename "basename", "dirname"; use File::Spec::Functions "catfile"; use List::Util qw(first min max zip); @@ -36,17 +35,18 @@ $| = 1; my $pipe = IO::Pipe->new; -my $i3status = fork // die "couldn't fork()!"; +my $i3status = fork // die "couldn't fork: $!"; unless ($i3status) { $pipe->writer; open STDOUT, ">&=", $pipe->fileno - or die "couldn't open child's STDOUT"; + or die "couldn't re-open i3status's STDOUT: $!"; exec "i3status"; } -my (%paper_ws, $focused_ws, %col_rows, $caffeinated_id, $caffeinated_name); +my (%paper_ws, $focused_ws, %col_rows, $caffe_id, $caffe_name); -my $wm_ipc_socket = $ENV{SWAYSOCK} || $ENV{I3SOCK}; +my $have_sway = !!$ENV{SWAYSOCK}; +my $wm_ipc_socket = $have_sway ? $ENV{SWAYSOCK} : $ENV{I3SOCK}; my $wmipc = AnyEvent::I3->new($wm_ipc_socket); $wmipc->connect->recv or die "couldn't connect to WM IPC socket"; sub with_ignored_events (&) { @@ -121,8 +121,7 @@ $wmipc->subscribe({ elsif ($e->{change} && $e->{change} eq "mark") { if (grep $_ eq "caffeinated", $e->{container}{marks}->@*) { register_caffeinated($e->{container}); - } elsif ($caffeinated_id - && $caffeinated_id == $e->{container}{id}) { + } elsif ($caffe_id && $caffe_id == $e->{container}{id}) { clear_caffeinated(); } } @@ -199,10 +198,9 @@ for_each_node { $pipe->reader; -my $hostname = hostname; -my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<); -my $hostinfo - = { name => "hostinfo", full_text => $username . "@" . $hostname }; +my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid $<; +my $hostinfo = { name => "hostinfo", + full_text => sprintf "%s@%s", $username, hostname }; # Skip the first line which contains the version header. print scalar <$pipe>; @@ -210,8 +208,6 @@ print scalar <$pipe>; # The second line contains the start of the infinite array. print scalar <$pipe>; -wsbuttons("no"); - # Basic idea here from Michael Stapelberg's sample i3status-wrapper. my $i3status_wrapper = AnyEvent->io( fh => $pipe, poll => "r", cb => sub { @@ -231,8 +227,8 @@ my $i3status_wrapper = AnyEvent->io( sprintf +($focused_ws == $key ? "%s" : "%s"), ws_name($paper_ws{$key}{name}) } - unshift @$blocks, { name => "ws", markup => "pango", - full_text => join " ", @disp }; + unshift @$blocks, + { name => "ws", markup => "pango", full_text => "@disp" }; } if ($focused_ws && exists $paper_ws{$focused_ws}) { @@ -252,10 +248,8 @@ my $i3status_wrapper = AnyEvent->io( } unshift @$blocks, - { - name => "caffeinated", - full_text => "Caffeinated: " . $caffeinated_name } - if $caffeinated_name; + { name => "caffeinated", full_text => "Caffeinated: $caffe_name" } + if $caffe_name; unshift @$blocks, $hostinfo; @@ -263,6 +257,7 @@ my $i3status_wrapper = AnyEvent->io( }); # Start main loop. +cmd("bar bar-0 workspace_buttons no") if $have_sway; for (;;) { $have_pending->recv; $have_pending = AnyEvent::condvar; @@ -282,20 +277,15 @@ for (;;) { process_msg(shift @pending_msgs) while @pending_msgs; } -sub wsbuttons { - return unless $ENV{XDG_CURRENT_DESKTOP} eq "sway"; - cmd("bar bar-0 workspace_buttons $_[0]"); -} - sub register_caffeinated { - $caffeinated_id = $_[0]->{id}; - $caffeinated_name = $_[0]->{name}; + $caffe_id = $_[0]->{id}; + $caffe_name = $_[0]->{name}; kill USR1 => $i3status; } sub clear_caffeinated { - undef $caffeinated_id; - undef $caffeinated_name; + undef $caffe_id; + undef $caffe_name; kill USR1 => $i3status; } -- cgit v1.2.3