X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=i3-wsbar;h=a485c1500740d0d921006408dbac99f68fde0514;hb=607ba6fcde36a710c08202c7155b7b64a6c99819;hp=b1a50c40fd46a5131babf11d304dea06f7e463df;hpb=96128c9cfbda0029775cabaa22b82b97a0a1f2f3;p=i3%2Fi3 diff --git a/i3-wsbar b/i3-wsbar index b1a50c40..a485c150 100755 --- a/i3-wsbar +++ b/i3-wsbar @@ -7,14 +7,22 @@ use warnings; use Getopt::Long; use Pod::Usage; use IPC::Run qw(start pump); +use Try::Tiny; use AnyEvent::I3; use AnyEvent; use v5.10; my $stdin; -my $i3 = i3; +my $socket_path = undef; my ($workspaces, $outputs) = ([], {}); my $last_line = ""; +my $w = AnyEvent->timer( + after => 2, + cb => sub { + say "Connection to i3 timed out. Verify socket path ($socket_path)"; + exit 1; + } +); my $command = ""; my $input_on = ""; @@ -23,6 +31,7 @@ my $show_all = 0; my $result = GetOptions( 'command=s' => \$command, + 'socket=s' => \$socket_path, 'input-on=s' => \$input_on, 'output-on=s' => \$output_on, 'show-all' => \$show_all, @@ -35,6 +44,8 @@ if ($command eq '') { exit 1; } +my $i3 = i3($socket_path); + my @input_on = split(/,/, $input_on); my @output_on = split(/,/, $output_on); @@ -44,6 +55,16 @@ $| = 1; # Wait a short amount of time and try to connect to i3 again sub reconnect { my $timer; + if (!defined($w)) { + $w = AnyEvent->timer( + after => 2, + cb => sub { + say "Connection to i3 timed out. Verify socket path ($socket_path)"; + exit 1; + } + ); + } + my $c = sub { $timer = AnyEvent->timer( after => 0.01, @@ -62,6 +83,8 @@ sub connected { return; } + $w = undef; + $i3->subscribe({ workspace => \&ws_change, output => \&output_change, @@ -113,9 +136,12 @@ sub got_outputs { } my $x = $new{$name}->{rect}->{x}; + my $w = $new{$name}->{rect}->{width}; my $launch = $command; $launch =~ s/([^%])%x/$1$x/g; + $launch =~ s/([^%])%w/$1$w/g; $launch =~ s/%%x/%x/g; + $launch =~ s/%%w/%w/g; $new{$name}->{cmd_input} = ''; my @cmd = ('/bin/sh', '-c', $launch); @@ -141,19 +167,28 @@ sub output_change { sub update_output { my $dzen_bg = "#111111"; my $out; + my $previous_output; for my $name (keys %{$outputs}) { my $width = $outputs->{$name}->{rect}->{width}; + $previous_output = undef; $out = qq|^pa(;2)|; for my $ws (@{$workspaces}) { next if $ws->{output} ne $name and !$show_all; + # Display a separator if we are on a different output now + if (defined($previous_output) and + ($ws->{output} ne $previous_output)) { + $out .= qq|^fg(#900000)^ib(1)\|^ib(0)^p(+4)|; + } + $previous_output = $ws->{output}; + my ($bg, $fg) = qw(333333 888888); ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible}; ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent}; - my $cmd = q|i3-msg "| . $ws->{num} . q|"|; + my $cmd = q|i3-msg "workspace | . $ws->{name} . q|"|; my $name = $ws->{name}; # Begin the clickable area @@ -176,13 +211,18 @@ sub update_output { $out .= qq|^p(2)^pa(;2)|; } - $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg(white)|; + $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg()|; $out .= qq|^p(+5)|; $out .= $last_line if (!@input_on or $name ~~ @input_on); $out .= "\n"; $outputs->{$name}->{cmd_input} = $out; - pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input}; + try { + pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input}; + } catch { + warn "Could not write to dzen2"; + exit 1; + } } } @@ -192,7 +232,12 @@ $stdin = AnyEvent->io( fh => \*STDIN, poll => 'r', cb => sub { - chomp (my $line = ); + my $line = ; + if (!defined($line)) { + undef $stdin; + return; + } + chomp($line); $last_line = $line; update_output(); }); @@ -217,10 +262,11 @@ i3-wsbar -c [options] =item B<--command> This command (at the moment only dzen2 is supported) will be started for each -output. C<%x> will be replaced with the X coordinate of the output. +output. C<%x> will be replaced with the X coordinate of the output, C<%w> will +be replaced with the width of the output. Example: - --command "dzen2 -dock -x %x" + --command "dzen2 -dock -x %x -w %w" =item B<--input-on>