66 lines
1.6 KiB
Nix
66 lines
1.6 KiB
Nix
{ lib, stdenv, pkgs, writeShellScript, ... }:
|
|
stdenv.mkDerivation rec {
|
|
name = "sway-move-to-output";
|
|
phases = "installPhase";
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
echo "$script" > $out/bin/move-to-output
|
|
chmod +x $out/bin/move-to-output
|
|
'';
|
|
script = ''
|
|
#!${pkgs.python310}/bin/python
|
|
import sys
|
|
import json
|
|
import subprocess
|
|
|
|
def get_next_output(outputs):
|
|
for i in range(len(outputs)):
|
|
if outputs[i]['focused']:
|
|
if i+1 == len(outputs):
|
|
return outputs[0]['name']
|
|
else:
|
|
return outputs[i+1]['name']
|
|
|
|
|
|
def get_previous_output(outputs):
|
|
for i in range(len(outputs)):
|
|
if outputs[i]['focused']:
|
|
if i-1 < 0:
|
|
return outputs[-1]['name']
|
|
else:
|
|
return outputs[i-1]['name']
|
|
|
|
|
|
def get_outputs():
|
|
p = subprocess.run(
|
|
[
|
|
"${pkgs.sway}/bin/swaymsg",
|
|
"-t",
|
|
"get_outputs"
|
|
],
|
|
capture_output=True,
|
|
encoding="utf-8")
|
|
return json.loads(p.stdout)
|
|
|
|
|
|
def main():
|
|
outputs = list(filter(lambda o: o['active'], get_outputs()))
|
|
|
|
ws = None
|
|
|
|
if len(sys.argv) > 1:
|
|
if sys.argv[1] == 'next':
|
|
ws = get_next_output(outputs)
|
|
if sys.argv[1] == 'previous':
|
|
ws = get_previous_output(outputs)
|
|
|
|
if ws:
|
|
return subprocess.run(["${pkgs.sway}/bin/swaymsg", "move", "workspace", "to", ws]).returncode
|
|
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
exit(main())
|
|
'';
|
|
}
|