@decoded = decode_json($result);
####
#!/bin/perl
use v5.10;
use strict;
use warnings;
use JSON::XS qw/decode_json/;
use feature qw/say/;
sub main {
my $result = qx{ i3-msg -t get_workspaces }; # returns [{},{},{}]
my $decoded = decode_json($result);
my $current = 0;
for my $workspace (@$decoded) {
my $num = $workspace->{'num'};
if ($workspace->{'focused'}) {
$current = $num;
next;
}
# Ignore workspaces below the currently focused one.
if ($current == 0) { next; }
# Found the gap!
if ($current + 1 != $num) { last; }
# Keep increasing the soon-to-be current workspace.
$current = $num;
}
say $current + 1;
}
main();
####
#!/bin/python3
import json
import subprocess
def main():
result = subprocess.run(
['i3-msg', '-t', 'get_workspaces'],
capture_output=True,
check=True
)
i3_workspaces = json.loads(result.stdout)
current_workspace = next((x['num'] for x in i3_workspaces if x['focused']))
last_workspace = i3_workspaces[-1]['num']
remaining_workspaces = (x['num'] for x in i3_workspaces if x['num'] >= current_workspace)
for i in range(current_workspace, last_workspace):
if i != next(remaining_workspaces):
print(i)
return
print(last_workspace + 1)
if __name__ == '__main__':
main()
####
time $(for i in {1..100}; do ./move_to_next.pl> /dev/null; done;)
Perl:
real 0m1.113s
user 0m0.843s
sys 0m0.266s
Python:
real 0m1.852s
user 0m1.516s
sys 0m0.323s
####
time ./move_to_next.pl > /dev/null
Perl:
real 0m0.231s
user 0m0.116s
sys 0m0.116s
Python:
real 0m0.099s
user 0m0.078s
sys 0m0.018s