From b8be6a3a4e918953f9c460637fda9c8839470e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Tue, 4 Jul 2023 11:44:55 +0200 Subject: [PATCH] feat(tmux): add fish completions --- .../programs/{tmux.nix => tmux/default.nix} | 30 +++--------- .../tmux/tmux-attach/completions.fish | 1 + modules/programs/tmux/tmux-attach/script.fish | 5 ++ .../tmux/tmux-sessionizer/completions.fish | 1 + .../tmux/tmux-sessionizer/script.fish | 13 +++++ overlays/builders.nix | 49 +++++++++++++++++++ 6 files changed, 77 insertions(+), 22 deletions(-) rename modules/programs/{tmux.nix => tmux/default.nix} (80%) create mode 100644 modules/programs/tmux/tmux-attach/completions.fish create mode 100644 modules/programs/tmux/tmux-attach/script.fish create mode 100644 modules/programs/tmux/tmux-sessionizer/completions.fish create mode 100644 modules/programs/tmux/tmux-sessionizer/script.fish create mode 100644 overlays/builders.nix diff --git a/modules/programs/tmux.nix b/modules/programs/tmux/default.nix similarity index 80% rename from modules/programs/tmux.nix rename to modules/programs/tmux/default.nix index 32836bd..c2c620c 100644 --- a/modules/programs/tmux.nix +++ b/modules/programs/tmux/default.nix @@ -41,32 +41,18 @@ let ''; }; - tmux-sessionizer = pkgs.writeShellApplication { + tmux-sessionizer = pkgs.writeFishApplication { name = "ts"; - runtimeInputs = with pkgs; [ tmux findutils coreutils procps fd fzf ]; - text = '' - #!/usr/bin/env bash - - selected=$(fd -HIg '.git' ~/ --min-depth 1 --max-depth 5 --type d --prune --exec dirname {} | ${getExe fzf1} "$*") - - selected_name=$(basename "$selected" | tr . _) - - if ! tmux has-session -t="$selected_name" 2> /dev/null; then - tmux new-session -ds "$selected_name" -c "$selected" - fi - - ${getExe tmux-switch} "$selected_name" - ''; + runtimeInputs = with pkgs; [ tmux findutils coreutils procps fd fzf1 tmux-switch ]; + text = readFile ./tmux-sessionizer/script.fish; + completions = readFile ./tmux-sessionizer/completions.fish; }; - tmux-attach = pkgs.writeShellApplication { + tmux-attach = pkgs.writeFishApplication { name = "ta"; - runtimeInputs = with pkgs; [ tmux ]; - text = '' - #!/usr/bin/env bash - selected=$(tmux list-sessions -F '#{session_name}' | ${getExe fzf1} "$*") - ${getExe tmux-switch} "$selected" - ''; + runtimeInputs = with pkgs; [ tmux fzf1 tmux-switch ]; + text = readFile ./tmux-attach/script.fish; + completions = readFile ./tmux-attach/completions.fish; }; in diff --git a/modules/programs/tmux/tmux-attach/completions.fish b/modules/programs/tmux/tmux-attach/completions.fish new file mode 100644 index 0000000..6e31f9f --- /dev/null +++ b/modules/programs/tmux/tmux-attach/completions.fish @@ -0,0 +1 @@ +complete -c ta -f -a '(tmux list-sessions -F "#{session_name}" 2>/dev/null)' diff --git a/modules/programs/tmux/tmux-attach/script.fish b/modules/programs/tmux/tmux-attach/script.fish new file mode 100644 index 0000000..442dec2 --- /dev/null +++ b/modules/programs/tmux/tmux-attach/script.fish @@ -0,0 +1,5 @@ +set selected (tmux list-sessions -F '#{session_name}' 2>/dev/null | fzf1 $argv) +if not test -n "$selected" + exit 1 +end +tmux-switch "$selected" diff --git a/modules/programs/tmux/tmux-sessionizer/completions.fish b/modules/programs/tmux/tmux-sessionizer/completions.fish new file mode 100644 index 0000000..f48642c --- /dev/null +++ b/modules/programs/tmux/tmux-sessionizer/completions.fish @@ -0,0 +1 @@ +complete -c ts -f -a '(fd -HIg '.git' ~/ --min-depth 1 --max-depth 5 --type d --prune --exec realpath "{}/.." | xargs -I{} basename {} | string replace "." "")' diff --git a/modules/programs/tmux/tmux-sessionizer/script.fish b/modules/programs/tmux/tmux-sessionizer/script.fish new file mode 100644 index 0000000..d0ca484 --- /dev/null +++ b/modules/programs/tmux/tmux-sessionizer/script.fish @@ -0,0 +1,13 @@ +set selected (fd -HIg '.git' ~/ --min-depth 1 --max-depth 5 --type d --prune --exec dirname {} | fzf1 $argv) + +set selected_name (basename $selected 2>/dev/null | string replace "." "_") + +if not test -n "$selected_name" + exit 1 +end + +if ! tmux has-session -t $selected_name 2> /dev/null + tmux new-session -ds $selected_name -c $selected +end + +tmux-switch $selected_name diff --git a/overlays/builders.nix b/overlays/builders.nix new file mode 100644 index 0000000..498ebcc --- /dev/null +++ b/overlays/builders.nix @@ -0,0 +1,49 @@ +_: + +final: _: +with final.lib; +{ + writeFishApplication = + { name + , text + , completions ? null + , runtimeInputs ? [ ] + , checkPhase ? null + }: + let + fishFile = destination: content: final.writeTextFile { + inherit name destination; + executable = true; + allowSubstitutes = true; + preferLocalBuild = false; + text = '' + #!${getExe final.fish} + '' + optionalString (runtimeInputs != [ ]) '' + + export PATH="${makeBinPath runtimeInputs}:$PATH" + '' + '' + + ${content} + ''; + + checkPhase = + if checkPhase == null then '' + runHook preCheck + ${getExe final.fish} -n "$target" + runHook postCheck + '' + else checkPhase; + }; + + + script = fishFile "/bin/${name}" text; + + completions_file = fishFile "/share/fish/vendor_completions.d/${name}.fish" completions; + in + final.symlinkJoin { + inherit name; + paths = [ + script + ] ++ optional (completions != null) completions_file; + }; +}