From d3386cd884d5b54b70afbe0513796439eb33674e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 31 Jul 2020 02:10:22 -0400 Subject: refactor --- bin/hush/i3-switch-workspace | 61 +++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/bin/hush/i3-switch-workspace b/bin/hush/i3-switch-workspace index bcbb8d2..acae465 100755 --- a/bin/hush/i3-switch-workspace +++ b/bin/hush/i3-switch-workspace @@ -27,47 +27,44 @@ chomp(my $path = qx(i3 --get-socketpath)); my $sock = IO::Socket::UNIX->new(Peer => $path); my $workspace_data = $json->decode(i3_msg('get_workspaces')); +my $tree_data = $json->decode(i3_msg('get_tree')); -my $next_workspace = $workspace_data->[-1]{num} + 1; -my $prev_workspace = $workspace_data->[0]{num} - 1; +my $first = 99999; +my $last = -99999; +my $cur; -if (@$workspace_data == 1) { - my $current_workspace = $workspace_data->[0]{num}; - my $tree_data = $json->decode(i3_msg('get_tree')); - my $workspace_tree = find_workspace($tree_data, $current_workspace); - if ($dir eq 'prev') { - i3_msg('command', "workspace number $prev_workspace"); - } - else { - i3_msg('command', "workspace number $next_workspace"); +for my $i (0..$#$workspace_data) { + my $num = $workspace_data->[$i]{num}; + if ($workspace_data->[$i]{focused}) { + $cur = $num; } + + my $workspace_tree = find_workspace($tree_data, $num); + next unless + @{ $workspace_tree->{nodes} } || + @{ $workspace_tree->{floating_nodes} }; + + $first = $num if $num < $first; + $last = $num if $num > $last; } -elsif ($workspace_data->[0]{focused} && $dir eq 'prev') { - my $current_workspace = $workspace_data->[0]{num}; - my $tree_data = $json->decode(i3_msg('get_tree')); - my $workspace_tree = find_workspace($tree_data, $current_workspace); - if (@{ $workspace_tree->{nodes} } || @{ $workspace_tree->{floating_nodes} }) { - if ($prev_workspace > 0) { - i3_msg('command', "workspace number $prev_workspace"); - } + +my $to; +if ($dir eq 'prev') { + if ($cur >= $first) { + $to = $cur - 1; } } -elsif ($workspace_data->[-1]{focused} && $dir eq 'next') { - my $current_workspace = $workspace_data->[-1]{num}; - my $tree_data = $json->decode(i3_msg('get_tree')); - my $workspace_tree = find_workspace($tree_data, $current_workspace); - if (@{ $workspace_tree->{nodes} } || @{ $workspace_tree->{floating_nodes} }) { - i3_msg('command', "workspace number $next_workspace"); +elsif ($dir eq 'next') { + if ($cur <= $last) { + $to = $cur + 1; } } else { - for my $i (0..$#$workspace_data) { - if ($workspace_data->[$i]{focused}) { - my $to = $workspace_data->[$i]{num} + ($dir eq 'prev' ? -1 : 1); - i3_msg('command', "workspace number $to"); - last; - } - } + die "unknown subcommand $dir"; +} + +if (defined $to) { + i3_msg('command', "workspace $to"); } sub find_workspace { -- cgit v1.2.3