Add zsh completions

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2025-01-10 16:35:58 +00:00
parent 465198f3c8
commit d967d5d603
9 changed files with 101 additions and 126 deletions

View File

@@ -11,6 +11,32 @@
kind
];
programs.zsh.p10k.extraRightPromptElements = [ "kubecontext" ];
programs.zsh = {
initExtra = ''
kubeswitch() {
local target="$HOME/.kube/$1"
local config="$HOME/.kube/config"
if [[ -f "$target" && "$target" != "$config" ]]; then
ln -sf "$target" "$config"
echo "Switched kube context to $1"
p10k reload
else
echo "Invalid kube context: $1"
echo "Ensure the file exists in ~/.kube"
fi
}
_kubeswitch-completion() {
local dir="$HOME/.kube"
local config="$dir/config"
compadd -- ''${(f)"$(find "$dir" -maxdepth 1 \( -type f -o -type l \) -not -name 'config' -exec basename {} \;)"}
}
compdef _kubeswitch-completion kubeswitch
'';
p10k.extraRightPromptElements = [ "kubecontext" ];
};
};
}

View File

@@ -2,12 +2,14 @@
user ? throw "user argument is required",
home ? throw "home argument is required",
}:
{ ... }:
{ lib, inputs, ... }:
{
home-manager.users.${user}.programs.zsh = {
shellAliases.nd = "nix-develop";
initExtra = ''
initExtra = let
devShells = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells);
in ''
nix-develop() {
if [ -z "$1" ]; then
echo "Usage: nix-develop <shell>"
@@ -15,6 +17,13 @@
fi
nix develop self#"$1" -c "$SHELL"
}
_nix-develop-completion() {
local shells=(${devShells})
compadd -- $shells
}
compdef _nix-develop-completion nix-develop
'';
};
}

View File

@@ -2,7 +2,7 @@
user ? throw "user argument is required",
home ? throw "home argument is required",
}:
{ inputs, ... }:
{ lib, inputs, ... }:
{
home-manager.users.${user} = {
programs = {
@@ -16,7 +16,9 @@
zsh = {
shellAliases.nde = "nix-direnv";
initExtra = ''
initExtra = let
devShells = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells);
in ''
nix-direnv() {
if [ -z "$1" ]; then
echo "use flake" > .envrc
@@ -34,6 +36,13 @@
direnv allow
}
_nix-direnv-completion() {
local shells=(${devShells})
compadd -- $shells
}
compdef _nix-direnv-completion nix-direnv
'';
p10k.extraRightPromptElements = [ "direnv" ];

View File

@@ -23,7 +23,7 @@ in
wayland.windowManager.hyprland.settings.bind = [
"Ctrl_Alt, r, exec, ${themeBin}"
"Ctrl_Alt, t, exec, ${themeBin} toggle"
"Ctrl_Alt, t, exec, ${themeBin} -m toggle"
];
};
}

View File

@@ -532,5 +532,7 @@ in
}) cfg.template;
}
);
programs.zsh.initExtra = builtins.readFile ./theme.completion.sh;
};
}

View File

@@ -0,0 +1,11 @@
_theme-completion() {
local options=(
'-m[set mode: light, dark, or toggle]:mode:(light dark toggle)'
'-w[set wallpaper: specify file path]:file:_files'
)
local curcontext="$curcontext" state line
typeset -A opt_args
_arguments -s $options
}
compdef _theme-completion theme

View File

@@ -10,15 +10,6 @@ set_wallpaper() {
fi
}
set_mode() {
if [[ "$1" = "light" ]] || [[ "$1" = "dark" ]]; then
MODE="$1"
else
echo "Invalid mode: $1. Use 'light' or 'dark'."
exit 1
fi
}
toggle_mode() {
if [[ "$(cat "${CONFIG}"/mode)" = "light" ]]; then
MODE="dark"
@@ -28,7 +19,7 @@ toggle_mode() {
}
show_usage() {
echo "Usage: theme {toggle|light|dark|mode <mode>|wallpaper <file> [mode]}"
echo "Usage: theme [-m {light|dark|toggle}] [-w <file>]"
}
finish() {
@@ -39,56 +30,31 @@ finish() {
"${RELOAD}" > /dev/null
}
if [[ $# -eq 0 ]]; then
finish
else
case "$1" in
toggle)
if [[ $# -eq 1 ]]; then
toggle_mode
else
show_usage
exit 1
fi
# Parse arguments
while getopts "m:w:" opt; do
case "$opt" in
m)
case "$OPTARG" in
light|dark)
MODE="$OPTARG"
;;
toggle)
toggle_mode
;;
*)
show_usage
exit 1
;;
esac
;;
light)
if [[ $# -eq 1 ]]; then
set_mode "light"
else
show_usage
exit 1
fi
;;
dark)
if [[ $# -eq 1 ]]; then
set_mode "dark"
else
show_usage
exit 1
fi
;;
mode)
if [[ $# -eq 2 ]]; then
set_mode "$2"
else
show_usage
exit 1
fi
;;
wallpaper)
if [[ $# -ge 2 ]] && [[ $# -le 3 ]]; then
set_wallpaper "$2"
[[ $# -eq 3 ]] && set_mode "$3"
else
show_usage
exit 1
fi
w)
set_wallpaper "$OPTARG"
;;
*)
show_usage
exit 1
;;
esac
done
finish
fi
finish