aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/bookmarks
blob: 65670cd5c4f8d9d5d7924911690566476f56e9ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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`
# https://linux.101hacks.com/cd-command/cdpath/
#
# TODO:
#   1. Remove `fzf` dependency
#
# Shell: POSIX compliant
# Author: Todd Yamakawa

if [ -z "$BOOKMARKS_DIR" ]; then
  BOOKMARKS_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/nnn/bookmarks"
fi

# Check if NNN_PIPE is set
if [ -z "$NNN_PIPE" ]; then
    echo 'ERROR: NNN_PIPE is not set' | ${PAGER:-less}
    exit 2
fi

# Get all directory symlinks
get_links() {
    for entry in "$1"/*; do

        # Skip unless directory symlink
        [ -h "$entry" ] || continue
        [ -d "$entry" ] || continue

        printf "%20s -> %s\n" "$(basename "$entry")" "$(readlink -f "$entry")"
    done | fzf | awk 'END { print "'"$BOOKMARKS_DIR"'/"$1 }'
}

# Choose symlink with fzf
cddir="$(get_links "$BOOKMARKS_DIR")"

# 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"