summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-07-31 02:10:22 -0400
committerJesse Luehrs <doy@tozt.net>2020-07-31 02:10:22 -0400
commitd3386cd884d5b54b70afbe0513796439eb33674e (patch)
tree67903aaa13ca3ec6211b3021aaced56b67c74fe3
parent1edee0126acb9f22cad879417c28e4f86597eec7 (diff)
downloadconf-d3386cd884d5b54b70afbe0513796439eb33674e.tar.gz
conf-d3386cd884d5b54b70afbe0513796439eb33674e.zip
refactor
-rwxr-xr-xbin/hush/i3-switch-workspace61
1 files 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 {