rice/users/buffet/bash.nix
buffet 180152ba4a feat: add fzf functions to interact with git worktrees
These have to be functions sadly because `cd` doesn't work otherwise.
This just means there is more reason to have subcommand aliases on a shell level. :)

Heavily based on nerdypeppers stuff[https://peppe.rs/posts/curing_a_case_of_git-UX/].
2024-05-20 12:06:16 +02:00

132 lines
3.5 KiB
Nix

{pkgs, ...}: let
inputrc = pkgs.writeText "inputrc" ''
set completion-ignore-case on
'';
in {
home-manager.users.buffet = {
home.sessionVariables = {
INPUTRC = inputrc;
};
programs = {
direnv.enableBashIntegration = true;
fzf.enableBashIntegration = true;
bash = {
enable = true;
historyControl = ["erasedups" "ignorespace"];
shellAliases = {
e = "\"$EDITOR\"";
mkdir = "mkdir -p";
rg = "rg -S";
update = let
change-message = pkgs.writeScript "change-commit-message" ''
#!/bin/sh
sed -i '1s/.*/chore: update/' "$1"
'';
in "nix flake update --commit-lock-file && EDITOR=${change-message} git commit --amend";
};
shellOptions = [
"cdspell"
"checkjobs"
"extglob"
"globstar"
"histappend"
"nocaseglob"
];
bashrcExtra = ''
[[ -f ~/.profile ]] && . ~/.profile
'';
initExtra = ''
gwa() {
[[ "$1" ]] || {
echo "required branch name argument" >&2
return 1
}
local repo="''${PWD##*/}"
local uuid="$(< /proc/sys/kernel/random/uuid)"
local basepath="$HOME/worktrees"
local path="$basepath/$repo-$uuid"
mkdir -p "$basepath"
git worktree add "$path" -b "$1" && cd "$path"
}
gwd() {
local res="$(
set -o pipefail
git worktree list | \
${pkgs.fzf}/bin/fzf --preview 'git log --color --decorate --oneline' \
--preview-window=up \
--cycle \
--query "$1" |\
cut -d' ' -f1
)"
[[ $? -eq 0 ]] && echo "$res" && git worktree remove "$res"
}
gwg() {
local res="$(
set -o pipefail
local trees="$(git worktree list | ${pkgs.fzf}/bin/fzf --filter "$1" --no-sort)"
if [[ "$(wc -l <<<"$trees")" -eq 1 ]]; then
printf '%s\n' "$trees"
else
printf '%s\n' "$trees" | \
${pkgs.fzf}/bin/fzf --preview='git log --color --decorate --oneline' \
--preview-window=up \
--cycle \
--query "$1"
fi | cut -d' ' -f1
)"
[[ $? -eq 0 ]] && echo "$res" && cd "$res"
}
__prompt() {
local status="$?"
local row
local col
IFS=';' read -rs -dR -p $'\e[6n' row col >/dev/tty
if [[ $col != 1 ]]; then
printf '%s' $'\e[0;7m%\n\e[0m'
fi
PS1='\[\e[0;1m\]['
case $status in
0) PS1+='\[\e[32m\]' ;;
*) PS1+='\[\e[31m\]' ;;
esac
if [[ "$PWD" == "$HOME" ]]; then
PS1+="~"
elif [[ "$PWD" == / ]]; then
PS1+=/
else
PS1+="''${PWD##*/}"
fi
PS1+='\[\e[0;1m\]]\[\e[0m\]'
[[ $CONTAINER_ID ]] && PS1+="'"
PS1+=' '
}
PROMPT_COMMAND=__prompt
bind '"\C-o": "\C-a\C-k fg; if [[ $? == 1 ]]; then nvim; fi\n"'
bind '"\e\C-m": "\C-e | nvim\C-m"'
'';
};
};
};
}