From c0ca8281f3abdf61a3721b7632aada5a8e925a42 Mon Sep 17 00:00:00 2001 From: Gerg-L Date: Thu, 22 May 2025 20:48:41 -0400 Subject: [PATCH] feat: auto zellij script --- nixosConfigurations/gerg-desktop/main.nix | 21 ++++++++++ nixosConfigurations/gerg-desktop/monitor.ps | 45 +++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 nixosConfigurations/gerg-desktop/monitor.ps diff --git a/nixosConfigurations/gerg-desktop/main.nix b/nixosConfigurations/gerg-desktop/main.nix index 95ef532..e24f381 100644 --- a/nixosConfigurations/gerg-desktop/main.nix +++ b/nixosConfigurations/gerg-desktop/main.nix @@ -130,6 +130,27 @@ silent = true; }; + + zsh = { + interactiveShellInit = + let + monitorScript = pkgs.replaceVarsWith { + src = ./monitor.ps; + replacements = builtins.mapAttrs (_: lib.getExe) { + inherit (pkgs) perl xdotool; + }; + isExecutable = true; + }; + in + '' + if [[ -z "$ZELLIJ" ]]; then + MONITOR="$(${monitorScript} || true)" + zellij attach -c "''${MONITOR:+"$MONITOR@"}$USER" + exit + fi + ''; + }; + nix-index = { enable = true; package = nix-index-database.packages.nix-index-with-db; diff --git a/nixosConfigurations/gerg-desktop/monitor.ps b/nixosConfigurations/gerg-desktop/monitor.ps new file mode 100644 index 0000000..04e52c3 --- /dev/null +++ b/nixosConfigurations/gerg-desktop/monitor.ps @@ -0,0 +1,45 @@ +#!@perl@ + +use strict; + +my %M; # hash to hold keys from 'xdotool getmouselocation --shell' +my $pipe; # file handle for the pipes we're going to open + +# get the current cursor location into $M{X} and $M{Y} +open($pipe, "-|", qw(@xdotool@ getmouselocation --shell)) || + die "couldn't open pipe from xdotool: $!\n"; + +while(<$pipe>) { + chomp; + my($key, $val) = split /=/; + $M{$key} = $val; +}; +close($pipe); + +# compare mouse location to monitor co-ordinates +open($pipe, "-|", "xrandr") || + die "couldn't open pipe from xrandr: $!\n"; + +while(<$pipe>) { + my ($display, $width, $height, $x_offset, $y_offset); + + next unless m/ connected /; + my @F = split; + $display = $F[0]; + + # co-ordinates are on fourth field (F[3]) on the primary display + # or on third field (F[2]) on non-primary displays. Perl arrays + # start from zero, not one (same as in bash). + if ($F[2] eq "primary") { + ($width, $height, $x_offset, $y_offset) = split /[x+]/, $F[3]; + } else { + ($width, $height, $x_offset, $y_offset) = split /[x+]/, $F[2]; + }; + + if ($M{X} >= $x_offset && $M{X} <= $width + $x_offset && + $M{Y} >= $y_offset && $M{Y} <= $height + $y_offset) { + print "$display\n"; + last; + }; +}; +close($pipe);