1
0
Fork 0
dotfiles/common/.config/zsh/configs/1-functions

114 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/bin/zsh
2024-06-10 01:45:17 +02:00
function venv_prompt() {
2024-01-13 15:10:31 +01:00
[[ -n ${VIRTUAL_ENV} ]] || return
echo -e "%F{yellow}%f ${VIRTUAL_ENV:t:gs/%/%%}"
2023-11-20 14:30:34 +01:00
}
2024-09-27 11:31:41 +02:00
function write_title_wd() {
echo -ne "\033]0;$(print -P "%n@%m [%3~]")\007"
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-02-05 17:32:24 +01:00
function lfwrap() {
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
2023-11-20 14:30:34 +01:00
zle reset-prompt
zle redisplay
write_title_wd
reset_beam
2023-06-13 19:13:27 +02:00
}
2023-11-20 14:30:34 +01:00
zle -N lfwrap
2023-03-29 16:15:06 +02:00
# Cursor Shape
2024-02-05 17:32:24 +01:00
function zle-keymap-select() {
2023-03-29 16:15:06 +02:00
case $KEYMAP in
vicmd) echo -ne '\e[1 q';; # block
viins|main) echo -ne '\e[5 q';; # beam
esac
}
zle -N zle-keymap-select
zle-line-init() {
zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere)
echo -ne "\e[5 q"
}
zle -N zle-line-init
# CMDLine Edit
zle -N edit-command-line
# Lazygit
2024-02-05 17:32:24 +01:00
function lg() {
[ ! -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)"
zle reset-prompt
2023-03-29 16:15:06 +02:00
}
2023-11-24 16:57:21 +01:00
zle -N lg
2024-02-05 17:32:24 +01:00
2024-02-28 10:29:08 +01:00
# Useful functions for usage in the shell
function xsi() {
xpkg -a | \
fzf -q ${1:-""} -m --preview \
"xq {1} | \grep -e pkgver -e license -e short_desc -e installed_size -e maintainer -e repository -e homepage" \
--preview-window=right:66%:wrap | xargs -ro xi
}
function ntfy() {
2024-02-28 15:47:35 +01:00
"$@" && { notify-send "$1 completed successfully"; return; }
notify-send -u critical "$1 failed! " "exited with code: $?"
2024-02-28 10:29:08 +01:00
}
2024-06-10 18:02:32 +02:00
function whed() {
2024-10-16 11:15:33 +02:00
script=$(which "$1")
[ -e "$script" ] && $EDITOR $(which "$script")
}
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
}
}