Skip to content

Commit

Permalink
feat: custom scripts with zsh completion
Browse files Browse the repository at this point in the history
  • Loading branch information
tiborpilz committed Dec 29, 2023
1 parent 8655c7b commit f8056bd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
5 changes: 0 additions & 5 deletions home/config/zsh/.zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ npr() {
fi
}

# Interactive cd to repo in ~/Code
ccd() {
cd $(find ~/Code/* -type d -maxdepth 0 | fzf-tmux --preview 'if [ -f {}/README.md ]; then glow {}/README.md --style=dark; else ls {}; fi')
}

source <(kubectl completion zsh)
alias k=kubectl

Expand Down
2 changes: 2 additions & 0 deletions home/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ with mylib;

programs.man.enable = true;

modules.scripts.enable = true;

modules.shell.zsh.enable = true;
modules.shell.zsh.aliases.ungron = "gron --ungron";

Expand Down
30 changes: 30 additions & 0 deletions home/modules/scripts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ inputs, config, lib, pkgs, ... }:

with lib;
let
cfg = config.modules.scripts;
mylib = import ../../lib { inherit inputs lib pkgs; };
in
{
options.modules.scripts = {
enable = mylib.mkBoolOpt false;
};

# TODO: Package scripts correctly
config = mkIf cfg.enable {
home.file."bin" = {
source = ../scripts;
recursive = true;
};

# Add $HOME/bin to $PATH
modules.shell.zsh.envInit = ''
export PATH="$HOME/bin:$PATH"
'';

# Add $HOME/bin/completions to $fpath
modules.shell.zsh.rcInit = ''
fpath+=($HOME/bin/completions)
'';
};
}
2 changes: 1 addition & 1 deletion home/modules/syncthing.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ inputs, config, options, lib, pkgs, ... }:
{ inputs, config, lib, pkgs, ... }:

with lib;
let
Expand Down
17 changes: 17 additions & 0 deletions home/scripts/ccd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# Interactive cd to repo in ~/Code

PROJECTS_DIR=${PROJECTS_DIR:-~/Code}

# Test
project_dirs() {
find "$PROJECTS_DIR" -maxdepth 1 -type d -not -path "$PROJECTS_DIR" | sort | xargs --max-lines=1 basename
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
query=${1:-''}
new_dir=$(project_dirs | fzf-tmux --query "$1" --preview "if [ -f \"${PROJECTS_DIR}\"/{}/README.md ]; then glow \"${PROJECTS_DIR}\"/{}/README.md --style=dark; else ls \"${PROJECTS_DIR}\"/{}; fi")
if [[ -n $new_dir ]]; then
cd $PROJECTS_DIR/$new_dir
fi
fi
10 changes: 10 additions & 0 deletions home/scripts/completions/_ccd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#compdef ccd


PROJECTS_DIR=${PROJECTS_DIR:-~/Code}

local -a subdirs
subdirs=($(find "$PROJECTS_DIR" -maxdepth 1 -type d -not -path "$PROJECTS_DIR" -exec basename {} \;))
_describe -t directories "project directory" subdirs -S/

return 0

0 comments on commit f8056bd

Please sign in to comment.