]> Sergey Matveev's repositories - nnn.git/commitdiff
Add a bookmarks plugin that supports names (#558)
authorTodd Yamakawa <toddyamakawa@users.noreply.github.com>
Wed, 6 May 2020 11:54:21 +0000 (06:54 -0500)
committerGitHub <noreply@github.com>
Wed, 6 May 2020 11:54:21 +0000 (17:24 +0530)
Co-authored-by: Todd Yamakawa <todd.yamakawa@arm.com>
plugins/README.md
plugins/bookmarks [new file with mode: 0755]

index b6384d3fb350dd82d4988f3fbcfd75e72d496e4e..ff168a2db294ac0eaf31dd6f3d1f141505ad0334 100644 (file)
@@ -27,6 +27,7 @@ Plugins are installed to `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins`.
 | Plugin (a-z) | Description | Lang | Dependencies |
 | --- | --- | --- | --- |
 | autojump | Navigate to dir/path | sh | autojump |
+| bookmarks | Use named bookmarks managed with symlinks | sh | fzf |
 | boom | Play random music from dir | sh | [moc](http://moc.daper.net/) |
 | dups | List non-empty duplicate files in current dir | sh | find, md5sum,<br>sort uniq xargs |
 | chksum | Create and verify checksums | sh | md5sum,<br>sha256sum |
diff --git a/plugins/bookmarks b/plugins/bookmarks
new file mode 100755 (executable)
index 0000000..c17d9e2
--- /dev/null
@@ -0,0 +1,51 @@
+#!/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`
+#
+# TODO:
+#   1. Remove `fzf` dependency
+#
+# Shell: POSIX compliant
+# Author: Todd Yamakawa
+
+BOOKMARKS_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/nnn/bookmarks"
+
+# Check if NNN_PIPE is set
+[ -z "$NNN_PIPE" ] && { echo 'NNN_PIPE is not set'; exit 2; }
+
+# Get all directory symlinks
+get_links() {
+    for entry in "$1"/*; do
+
+        # Skip unless directory symlink
+        [ -h "$entry" ] || continue
+        [ -d "$entry" ] || continue
+
+        echo "$(basename "$entry") ->  $(readlink -f "$entry")"
+    done | column -t
+}
+
+# Choose symlink with fzf
+cddir="$(get_links "$BOOKMARKS_DIR" | fzf | awk 'END { print "'"$BOOKMARKS_DIR"'/"$1 }')"
+
+# 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"
+