107 lines
3.1 KiB
Bash
107 lines
3.1 KiB
Bash
|
local 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
|
||
|
}
|
||
|
|
||
|
local function get_entries() {
|
||
|
file_type=$1
|
||
|
if [ ! -d "$XDG_CONFIG_HOME/fzgo/entries" ]; then
|
||
|
return
|
||
|
fi
|
||
|
for entry in "$XDG_CONFIG_HOME/fzgo/entries/$file_type"/*; do
|
||
|
# If the file is a symlink to a directory, we want to list the directory
|
||
|
if [ -L "$entry" ]; then
|
||
|
target=$(realpath "$entry")
|
||
|
if [ -d "$target" ]; then
|
||
|
fd . "$target" -t "$file_type"
|
||
|
fi
|
||
|
else
|
||
|
cat "$entry"
|
||
|
fi
|
||
|
done
|
||
|
}
|
||
|
|
||
|
function fzgo_update_cache() {
|
||
|
for file_type_dir in "$XDG_CONFIG_HOME/fzgo/entries"/*; do
|
||
|
mkdir -p "$XDG_CACHE_HOME/fzgo/entries"
|
||
|
for entry in "$file_type_dir"/*; do
|
||
|
file_type=$(basename "$file_type_dir")
|
||
|
mkdir -p "$XDG_CACHE_HOME/fzgo/entries/$file_type"
|
||
|
# If the file is a symlink to a directory, we want to list the directory
|
||
|
if [ -L "$entry" ]; then
|
||
|
target=$(realpath "$entry")
|
||
|
if [ -d "$target" ]; then
|
||
|
out_file="$(echo "$target" | sed 's\/\|\g')"
|
||
|
out_file="$XDG_CACHE_HOME/fzgo/entries/$file_type/$out_file"
|
||
|
mkdir -p "$(dirname \"$out_file\")"
|
||
|
fd . "$target" -t "$file_type" > "$out_file"
|
||
|
fi
|
||
|
fi
|
||
|
done
|
||
|
done
|
||
|
}
|
||
|
|
||
|
|
||
|
local function find_global() {
|
||
|
file_type=$1
|
||
|
FZGO_RECENTS=$XDG_CACHE_HOME/fzgo/recent_paths_$file_type
|
||
|
mkdir -p $(dirname $FZGO_RECENTS)
|
||
|
touch $FZGO_RECENTS
|
||
|
FZGO_RECENT_ENTRIES=$(cat $FZGO_RECENTS)
|
||
|
FZGO_ENTRIES=$(get_entries $file_type)
|
||
|
file="$(echo $FZGO_RECENT_ENTRIES'\n'$FZGO_ENTRIES | 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
|
||
|
echo "$file"
|
||
|
}
|
||
|
|
||
|
local function find_local() {
|
||
|
fd . -t $1 | fzgo
|
||
|
}
|
||
|
|
||
|
local 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"
|
||
|
$EDITOR "$selection"
|
||
|
fi
|
||
|
else
|
||
|
emulate -L zsh
|
||
|
zle -I
|
||
|
LBUFFER="${LBUFFER}\"$selection\""
|
||
|
zle reset-prompt
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
function fzgo_global_d() { open_path d find_global }
|
||
|
function fzgo_global_f() { open_path f find_global }
|
||
|
function fzgo_local_f() { open_path f find_local }
|
||
|
|
||
|
function fzgo_register_key() {
|
||
|
zle -N $2
|
||
|
bindkey "^$1" $2
|
||
|
bindkey -M emacs "^$1" $2
|
||
|
bindkey -M vicmd "^$1" $2
|
||
|
bindkey -M viins "^$1" $2
|
||
|
}
|