dotfiles/home/zsh/files/functions.zsh

96 lines
2.5 KiB
Bash
Raw Normal View History

2024-06-23 22:25:35 +02:00
function cd_to() {
setopt localoptions pipefail no_aliases 2> /dev/null
local dir=$1
if [[ -z "$dir" ]]; then
zle redisplay
return 0
fi
zle push-line # Clear buffer. Auto-restored on next prompt.
cd $dir
zle accept-line
local ret=$?
unset dir # ensure this doesn't end up appearing in prompt expansion
zle reset-prompt
}
function find_local() {
2024-08-01 20:44:47 +02:00
fd . -t $1 | fzgo
2024-06-23 22:25:35 +02:00
}
function find_global() {
file_type=$1
2024-09-14 17:37:13 +02:00
mkdir -p ~/.cache/fzgo_paths
FZGO_CACHE=~/.cache/fzgo_paths/$file_type
FZGO_RECENTS=~/.cache/fzgo_paths/recents_$file_type
if [ -f $FZGO_CACHE ]; then
if [ $(stat -c %Y $FZGO_CACHE) -lt $(date +%s -d '-1 day') ]; then
rm $FZGO_CACHE
2024-06-23 22:25:35 +02:00
fi
fi
2024-09-14 17:37:13 +02:00
if [ ! -f $FZGO_CACHE ]; then
2024-06-23 22:25:35 +02:00
PATHS=""
for i in "$LOCAL_SEARCH_DIRS[@]"; do
2024-09-14 17:37:13 +02:00
if [ -d "$i" ]; then
2024-08-01 16:12:43 +02:00
PATHS="$PATHS\n$i\n$(fd . $i -t $file_type)"
2024-06-23 22:25:35 +02:00
fi
done
for i in "$REMOTE_SEARCH_DIRS[@]"; do
2024-09-14 17:37:13 +02:00
if [ -d "$i" ]; then
2024-06-23 22:25:35 +02:00
PATHS="$PATHS\n$i\n$(ssh mallorea fd . \"$i\" -t $file_type -d 8)"
fi
done
2024-09-14 17:37:13 +02:00
echo "$PATHS" > $FZGO_CACHE
2024-06-23 22:25:35 +02:00
fi
2024-09-14 17:37:13 +02:00
touch $FZGO_RECENTS
file="$(cat $FZGO_RECENTS $FZGO_CACHE | awk '!x[$0]++' | fzgo)"
echo "$file" | cat - $FZGO_RECENTS | awk '!x[$0]++' > /tmp/fzgo_paths_recents_$file_type && mv /tmp/fzgo_paths_recents_$file_type $FZGO_RECENTS
2024-06-23 22:25:35 +02:00
echo "$file"
}
function open_path() {
file_type=$1
search_fn=$2
selection="$($search_fn $file_type)"
if [ "$selection" = "" ]; then
zle reset-prompt
return
fi
if [ "$LBUFFER" = "" ]; then
if [ "$file_type" = "d" ]; then
cd_to "$selection"
else
selection=$(realpath "$selection")
parent=$(dirname "$selection")
cd_to "$parent"
nvim "$selection"
fi
else
emulate -L zsh
zle -I
LBUFFER="${LBUFFER}\"$selection\""
zle reset-prompt
fi
}
function open_global_path_d() { open_path d find_global }
function open_global_path_f() { open_path f find_global }
function open_local() { open_path f find_local }
function register_key() {
zle -N $2
bindkey "^$1" $2
bindkey -M emacs "^$1" $2
bindkey -M vicmd "^$1" $2
bindkey -M viins "^$1" $2
}
function push() {
git add -A
2024-09-14 17:53:40 +02:00
if [ -z "$*" ]; then
git commit -m "$(date)"
else
2024-09-14 17:53:40 +02:00
git commit -m "$*"
fi
git push
}