diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2024-02-04 14:28:52 +0800 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2024-02-04 14:28:52 +0800 |
commit | 7f5bb6e760b852544b2a42c31dc7de7ce535b5b0 (patch) | |
tree | 9b2c04ba067bd9779ad758cc533e6f751d93a8ec | |
parent | 96f98100ffa39f4a4c7d32d11f58aee49c6f5b27 (diff) | |
download | dotfiles-7f5bb6e760b852544b2a42c31dc7de7ce535b5b0.tar.gz |
i3status-wrapper: factor out for_each_node
-rwxr-xr-x | scripts/desktop/i3status-wrapper | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/scripts/desktop/i3status-wrapper b/scripts/desktop/i3status-wrapper index 8c3b9bf5..b57f05ab 100755 --- a/scripts/desktop/i3status-wrapper +++ b/scripts/desktop/i3status-wrapper @@ -53,6 +53,16 @@ sub with_ignored_events (&) { $wmipc->send_tick("i3status-wrapper-unign"); } +sub for_each_node (&) { + my @trees = $wmipc->get_tree; + while (@trees) { + foreach my $node ((shift @trees)->{nodes}->@*) { + $_[0]->($node); + unshift @trees, $node; + } + } +} + my @all_workspaces = ( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11:F1", "12:F2", @@ -79,22 +89,18 @@ unless (fork // warn "couldn't fork monitoring loop") { map("[con_id=$_] move container workspace current", @old_ids), "focus child"); } - my @trees = $wmipc->get_tree; - while (@trees) { - foreach my $node ((shift @trees)->{nodes}->@*) { - if ($node->{type} eq "workspace" - && grep $_ eq $node->{name}, @all_workspaces) { - my $entry = $info{paper_ws}{$node->{id}} - //= { name => $node->{name}, - off_left => [], off_right => [] }; - sync_cols($node => $entry); - $entry->{ncols} = max 2, scalar $entry->{cols}->@*; - } elsif (grep $_ eq "caffeinated", $node->{marks}->@*) { - register_caffeinated($node); - } - unshift @trees, $node; + for_each_node { + my $node = shift; + if ($node->{type} eq "workspace" + && grep $_ eq $node->{name}, @all_workspaces) { + my $entry = $info{paper_ws}{$node->{id}} + //= { name => $node->{name}, off_left => [], off_right => [] }; + sync_cols($node => $entry); + $entry->{ncols} = max 2, scalar $entry->{cols}->@*; + } elsif (grep $_ eq "caffeinated", $node->{marks}->@*) { + register_caffeinated($node); } - } + }; tied(%info)->unlock; # Now loop forever reading events, assuming no exceptions. @@ -424,18 +430,16 @@ sub normalise_ws_cols { my $old_cols = $ws->{cols}; my $old_i = shift // first { $old_cols->[$_] == $ws->{focused_col} } 0..$#$old_cols; - my @trees = $wmipc->get_tree; - while (@trees) { - for ((shift @trees)->{nodes}->@*) { - if ($_->{id} == $info{focused_ws}) { - sync_cols($_ => $ws); - my $first_focus = $_->{focus}->[0]; - $floating_focus = ! grep $_ == $first_focus, $ws->{cols}->@*; - last; - } - unshift @trees, $_ + for_each_node { + my $node = shift; + if ($node->{id} == $info{focused_ws}) { + sync_cols($node => $ws); + my $first_focus = $node->{focus}->[0]; + $floating_focus = ! grep $_ == $first_focus, $ws->{cols}->@*; + goto DONE; } - } + }; + DONE: my $cols = $ws->{cols}; my $i = first { $cols->[$_] eq $ws->{focused_col} } 0..$#$cols; |