From 1d093433f021030270c3c520e3c05e805db6ed4b Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Mon, 4 Jun 2018 08:18:40 -0700 Subject: [PATCH] Bookmarks for zsh with j --- zsh/.zshrc | 2 ++ zsh/j.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 zsh/j.sh diff --git a/zsh/.zshrc b/zsh/.zshrc index 8eb5e12..d4f360f 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -79,6 +79,8 @@ function precmd () { z --add "$(pwd -P)" } +source $HOME/.config/zsh/j.sh + # ls colors eval $(dircolors $HOME/.config/zsh/dircolors.256dark) diff --git a/zsh/j.sh b/zsh/j.sh new file mode 100644 index 0000000..22c8a42 --- /dev/null +++ b/zsh/j.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env zsh + +local STORE="${HOME}/.j" +touch $STORE + +# TODO: use a path-disallowed character as a separator (instead of a pipe) + +function _j_add() { + local name=$1; + local dir=${PWD} + echo "${name}|${dir}" >> "$STORE" + echo "${name}|${dir}" +} +function _j_delete() { + local name=$1; + grep -Ev "^${name}|" "$STORE" | sponge "$STORE" +} + +function _j_jump() { + local name=$1; + local dir=$(grep -E "^${name}\|" "$STORE" | cut -d '|' -f 2) + if [ -n "$dir" ]; then + cd "$dir" + else + echo "No match found for $name" + fi +} + +function _j_list() { + cat "$STORE" +} + +function j() { + local action='' + local action_arg='' + + while [ "$#@" -gt 0 ]; do + if [ -n "$action" ]; then + echo "Multiple actions specified" + return 1 + fi + + case "$1" in + --add|-a) + action='_j_add' + action_arg="$2" + shift 2 + ;; + --delete|-d) + action='_j_delete' + action_arg="$2" + shift 2 + ;; + --list|-l) + action='_j_list' + shift + continue + ;; + *) + action='_j_jump' + action_arg="$1" + shift + continue + ;; + esac + done + + if [ -z "$action" ]; then + action='_j_list' + fi + + "$action" "$action_arg" +}