1
0
Fork 0
dotfiles/common/.config/zsh/config/widgets.zsh

131 lines
3.4 KiB
Bash
Raw Normal View History

2024-11-11 18:41:26 +01:00
autoload -Uz add-zsh-hook
autoload -Uz colors && colors
autoload -Uz edit-command-line && zle -N edit-command-line
2024-06-10 01:45:17 +02:00
2024-11-12 01:41:57 +01:00
# function venv_prompt() {
# [[ -n ${VIRTUAL_ENV} ]] || return
# echo -e "%F{yellow}%f ${VIRTUAL_ENV:t:gs/%/%%}"
# }
function reset_prompt() {
for f in chpwd "${chpwd_functions[@]}" precmd "${(@)precmd_functions:#-z4h-direnv-hook}"; do
[[ "${+functions[$f]}" == 0 ]] || "$f" &>/dev/null || true
done
zle reset-prompt
2023-11-20 14:30:34 +01:00
}
2024-11-11 18:41:26 +01:00
# Cursor Shape
function zle-keymap-select() {
case $KEYMAP in
vicmd) echo -ne '\e[1 q';;
viins|main) echo -ne '\e[5 q';;
esac
}
zle -N zle-keymap-select
function zle-line-init() {
echo -ne "\e[5 q"
}
zle -N zle-line-init
2024-09-27 11:31:41 +02:00
function write_title_wd() {
2024-11-11 18:41:26 +01:00
echo -ne "\e]0;$(print -P "%n@%m [%3~]")\007"
2023-11-20 14:30:34 +01:00
}
2024-11-11 18:41:26 +01:00
add-zsh-hook precmd write_title_wd
2023-11-20 14:30:34 +01:00
2024-09-27 11:31:41 +02:00
function reset_beam() {
echo -ne '\e[5 q'
}
2024-11-11 18:41:26 +01:00
add-zsh-hook preexec reset_beam
2024-11-12 01:41:57 +01:00
function fancy-ctrl-z () {
2024-11-11 18:41:26 +01:00
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line -w
else
zle push-input -w
zle clear-screen -w
fi
}
zle -N fancy-ctrl-z
2024-11-12 01:41:57 +01:00
function sudo-command-line() {
2024-11-11 18:41:26 +01:00
[[ -z $BUFFER ]] && zle up-history
if [[ $BUFFER == sudo\ * ]]; then
LBUFFER="${LBUFFER#sudo }"
elif [[ $BUFFER == $EDITOR\ * ]]; then
LBUFFER="${LBUFFER#$EDITOR }"
LBUFFER="sudoedit $LBUFFER"
elif [[ $BUFFER == sudoedit\ * ]]; then
LBUFFER="${LBUFFER#sudoedit }"
LBUFFER="$EDITOR $LBUFFER"
else
LBUFFER="sudo $LBUFFER"
fi
}
zle -N sudo-command-line
2024-11-11 18:41:26 +01:00
function lf-wrap() {
2024-06-10 23:26:33 +02:00
LF_DIRFILE="$(mktemp -u)"
env lfX -last-dir-path="$LF_DIRFILE" \
2024-08-06 14:46:58 +02:00
-command "set promptfmt \"$(${HOME}/.local/libexec/lf_prompt)\""
2024-06-10 23:26:33 +02:00
2023-06-13 19:13:27 +02:00
dir="$(cat "$LF_DIRFILE")"
2024-06-10 23:26:33 +02:00
rm "$LF_DIRFILE" &>/dev/null
2023-06-13 19:13:27 +02:00
if [ -d "$dir" ]; then
if [ "$dir" != "$(pwd)" ]; then
cd "$dir"
fi
fi
2024-06-10 23:26:33 +02:00
2024-11-12 01:41:57 +01:00
reset_prompt
2023-11-20 14:30:34 +01:00
write_title_wd
reset_beam
2023-06-13 19:13:27 +02:00
}
2024-11-11 18:41:26 +01:00
zle -N lf-wrap
2023-06-13 19:13:27 +02:00
2024-11-11 18:41:26 +01:00
function lazygit-wrap() {
[ ! -d "$(pwd)/.git" ] && [[ $(read -erk "?Not in a git repository. Create a new git repository? (y/n): ") =~ ^[Yy]$ ]] && git init
[ -d "$(pwd)/.git" ] && lazygit -p "$(pwd)"
2024-11-12 01:41:57 +01:00
reset_prompt
2023-03-29 16:15:06 +02:00
}
2024-11-11 18:41:26 +01:00
zle -N lazygit-wrap
2024-10-16 12:09:42 +02:00
function workon_cwd() {
local PROJECT_ROOT="${PWD}"
while [ "$PROJECT_ROOT" != "/" ] && [ ! -e "$PROJECT_ROOT/.venv" ] && [ ! -d "$PROJECT_ROOT/.git" ]; do
local PROJECT_ROOT="$(dirname "$PROJECT_ROOT")"
done
if [ -r "$PROJECT_ROOT/.venv" ]; then
local ENV_NAME="$(cat $PROJECT_ROOT/.venv)"
else
local ENV_NAME="$(basename "$PROJECT_ROOT")"
fi
[ -n "$CD_VIRTUAL_ENV" ] && [ "$ENV_NAME" != "$CD_VIRTUAL_ENV" ] && {
[ -n "$VIRTUAL_ENV" ] && deactivate
unset CD_VIRTUAL_ENV
}
[ -n "$ENV_NAME" ] && [ ! "$(realpath "$VIRTUAL_ENV" 2>/dev/null)" = "$(realpath "$WORKON_HOME/$ENV_NAME")" ] && {
if [ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]; then
workon -n "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
elif [ -e "$ENV_NAME/bin/activate" ]; then
source "$ENV_NAME/bin/activate" && export CD_VIRTUAL_ENV="$ENV_NAME"
else
local ENV_NAME=""
fi
}
[ -z "$ENV_NAME" ] && [ -n "$CD_VIRTUAL_ENV" ] && [ -n "$VIRTUAL_ENV" ] && {
deactivate && unset CD_VIRTUAL_ENV
}
}
2024-11-11 18:41:26 +01:00
add-zsh-hook chpwd workon_cwd
2024-11-11 21:54:02 +01:00
2024-11-12 01:41:57 +01:00
command -v direnv &>/dev/null && emulate zsh -c "$(direnv hook zsh)"