1
0
Fork 0
dotfiles/.local/bin/shortcuts
2024-03-12 22:41:46 +01:00

80 lines
1.8 KiB
Bash
Executable file

#!/bin/sh
# Inputs
bmdirs="$XDG_CONFIG_HOME/bookmarks/directories"
bmfiles="$XDG_CONFIG_HOME/bookmarks/files"
# Outputs
zsh_named_dirs="$ZDOTDIR/configs/hashes"
lf_files="$XDG_DATA_HOME/lf/shortcut-files"
lf_dirs="$XDG_DATA_HOME/lf/shortcut-dirs"
lf_shortcuts="$XDG_CONFIG_HOME/lf/shortcutrc"
find_marker() {
file=$1
for marker in \
"# @SHORTCUTS@" \
"-- @SHORTCUTS@" \
"// @SHORTCUTS@" \
"/* @SHORTCUTS@ */"
do
file=$(grep -xn "$marker" "$file")
if [ -n "$file" ]; then
echo "$file"
return
fi
done
}
zsh() {
xargs printf "hash -d %s=%s\n"
}
clean() {
rm -rf "/tmp/shortcuts"
}
write_dirs_tmp() {
mkdir -p /tmp/shortcuts
while IFS= read -r line; do
shortcut=$(echo "$line" | cut -d' ' -f1)
path=$(echo "$line" | cut -d' ' -f2); path=$(eval "echo $path")
printf "hash -d %s=%s\n" "$shortcut" "$path" >>"/tmp/shortcuts/zsh_named_dirs"
printf "map g%s cd \"%s\"\n" "$shortcut" "$path" >>"/tmp/shortcuts/lf_shortcuts"
printf "map %s" "$path" >>"/tmp/shortcuts/lf_dirs"
done
}
write_files_tmp() {
read -r contents
mkdir -p /tmp/shortcuts
shortcut=$(echo "$contents" | cut -d' ' -f1)
path=$(echo "$contents" | cut -d' ' -f2);
printf "hash -d %s=%s\n" "$shortcut" "$path" >>"/tmp/shortcuts/zsh_named_dirs"
printf "map %s" "$path" >>"/tmp/shortcuts/lf_files"
}
apply_tmp() {
cat /tmp/shortcuts/zsh_named_dirs >"$zsh_named_dirs"
cat /tmp/shortcuts/lf_shortcuts >"$lf_shortcuts"
cat /tmp/shortcuts/lf_dirs >"$lf_dirs"
cat /tmp/shortcuts/lf_files >"$lf_files"
}
read_file() {
in_file=$1
sed 's/#.*//;/^$/d' "$in_file" | tr -s ' '
}
clean
read_file "$bmdirs" | write_dirs_tmp
read_file "$bmfiles" | write_files_tmp
apply_tmp
clean