2020-05-06 11:54:21 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Use named bookmarks using symlinks
|
|
|
|
#
|
|
|
|
# Dependencies: fzf
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# 1. Create a $BOOKMARKS_DIR directory
|
|
|
|
# By default, $BOOKMARKS_DIR is set to: ${XDG_CACHE_HOME:-$HOME/.cache}/nnn/bookmarks
|
|
|
|
#
|
|
|
|
# 2. Create symlinks to directories
|
|
|
|
# `cd $BOOKMARKS_DIR`
|
|
|
|
# `ln -s /path/to/useful/directory bookmark_name`
|
|
|
|
# `ln -s $XDG_CONFIG_HOME/nnn/plugins nnn_plugins"
|
|
|
|
# `ln -s /path/to/documents docs`
|
|
|
|
# `ln -s /path/to/media media`
|
|
|
|
# `ln -s /path/to/movies movies`
|
|
|
|
#
|
|
|
|
# Bonus tip: Add `$BOOKMARKS_DIR` to your `$CDPATH`
|
2020-06-03 19:17:46 +00:00
|
|
|
# https://linux.101hacks.com/cd-command/cdpath/
|
2020-05-06 11:54:21 +00:00
|
|
|
#
|
|
|
|
# TODO:
|
|
|
|
# 1. Remove `fzf` dependency
|
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Author: Todd Yamakawa
|
|
|
|
|
2020-10-13 12:20:28 +00:00
|
|
|
if [ -z "$BOOKMARKS_DIR" ]; then
|
2020-10-15 02:38:50 +00:00
|
|
|
BOOKMARKS_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/nnn/bookmarks"
|
2020-10-13 12:20:28 +00:00
|
|
|
fi
|
2020-05-06 11:54:21 +00:00
|
|
|
|
|
|
|
# Check if NNN_PIPE is set
|
2020-06-03 19:17:46 +00:00
|
|
|
if [ -z "$NNN_PIPE" ]; then
|
2021-05-14 13:15:09 +00:00
|
|
|
printf 'ERROR: NNN_PIPE is not set!'
|
|
|
|
read -r _
|
2020-06-03 19:17:46 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
2020-05-06 11:54:21 +00:00
|
|
|
|
|
|
|
# Get all directory symlinks
|
|
|
|
get_links() {
|
|
|
|
for entry in "$1"/*; do
|
|
|
|
|
|
|
|
# Skip unless directory symlink
|
|
|
|
[ -h "$entry" ] || continue
|
|
|
|
[ -d "$entry" ] || continue
|
|
|
|
|
2020-06-03 19:17:46 +00:00
|
|
|
printf "%20s -> %s\n" "$(basename "$entry")" "$(readlink -f "$entry")"
|
2020-10-15 17:36:36 +00:00
|
|
|
done | fzf |
|
|
|
|
awk 'END {
|
|
|
|
if (length($1) == 0) { print "'"$PWD"'" }
|
|
|
|
else { print "'"$BOOKMARKS_DIR"'/"$1 }
|
|
|
|
}'
|
2020-05-06 11:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Choose symlink with fzf
|
2020-06-03 19:17:46 +00:00
|
|
|
cddir="$(get_links "$BOOKMARKS_DIR")"
|
2020-05-06 11:54:21 +00:00
|
|
|
|
|
|
|
# Writing result to NNN_PIPE will change nnn's active directory
|
|
|
|
# https://github.com/jarun/nnn/tree/master/plugins#send-data-to-nnn
|
|
|
|
context=0
|
|
|
|
printf "%s" "${context}c$(readlink -f "$cddir")" > "$NNN_PIPE"
|