diff --git a/README.md b/README.md index 9d652cf..e7a61f8 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,13 @@ __HyperJump__ - simple and quick bookmark tool for bash and zsh shells. I found ## How To Use HyperJump consists of 3 command line commands (functions). -* __jr__ - Remember Jump. Bookmarks current directory. Run ``jr nickname`` to add current directory, or just run ``jr`` and use the interactive mode. -* __jf__ - Forget Jump. Deletes the current directory from the bookmarks. Run ``jf`` while in a directory you want forgotten or ``jf nickname`` to forget a specific nickname. -* __jj__ - Jump to a bookmark location. Run ``jj nickname`` to jump to a location or just ``jj`` to get a list of all bookmarks. You can also run ``jj nickname command`` to jump to a location and than run the command specified with "./" as the first argument. So, for instance, you can run ``jj myProject open subl`` on OSX to jump to the myProject directory and open the myProject directory in Finder and Sublime Text. +* __jr__ - Jump Remember. Bookmarks current directory. Run ``jr nickname`` to add current directory, or just run ``jr`` and use the interactive mode. +* __jf__ - Jump Forget. Deletes the current directory from the bookmarks. Run ``jf`` while in a directory you want forgotten or ``jf nickname`` to forget a specific nickname. +* __jj__ - Jump Jump. Jumps to a bookmark location. Run ``jj nickname`` to jump to a location or just ``jj`` to get a list of all bookmarks. You can also run ``jj nickname command`` to jump to a location and than run the command specified with "./" as the first argument. So, for instance, you can run ``jj myProject open subl`` on OSX to jump to the myProject directory and open the myProject directory in Finder and Sublime Text. +* __jp__ - Jump Push. Jumps to a bookmark location and saves that location to the directory stack. Usage is like ``jj`` but uses ``pushd`` instead of ``cd``. +* __hjsort__ - Sort the hyperjump list (Note that running a "jr" will also sort the list) -All of the commands have autocomplete. Both __jj__ and __jf__ will autocomplete with nicknames of bookmarked locations. The __jr__ command will autocomplete with the basename of the current directory. After the first argument, __jj__ will autocomplete with list of available system commands (programs). +All of the commands have autocomplete. __jj__, __jp__, and __jf__ will autocomplete with nicknames of bookmarked locations. The __jr__ command will autocomplete with the basename of the current directory. After the first argument, __jj__ and __jp__ will autocomplete with list of available system commands (programs). **Examples:** @@ -23,13 +25,21 @@ $ jf # Forget another directory $ jf AnotherDir -# Jump to a Directory +# Jump to a directory $ jj $ jj MyDir + +# Jump to a directory and push to dirs +$ jp +$ jp MyDir # Jump to a directory and open the directory in another program(s) $ jj MyDir open $ jj MyDir open subl tm +$ jp MyDir open + +# Sort the list of bookmarks (not normally necessary as running jr sorts the list) +$ hjsort ``` ## How To Install diff --git a/hyperjump b/hyperjump index 4306e23..1d6855d 100644 --- a/hyperjump +++ b/hyperjump @@ -1,202 +1,294 @@ # Set Up the Database if it does not exist yet function _hyperjumpdatabase() { - local dbdir="$HOME"/.local/lib - local db="$dbdir"/hyperjumpdb - local db_old="$HOME"/.hyperjumpdb - if [[ ! -f "$db" ]]; then - # Create ~/.local/lib directory if it fors not exists - if [[ ! -d "$dbdir" ]]; then - mkdir -p "$dbdir" >> /dev/null - fi - # If old DB exists, move it to the new location - if [[ -f "$db_old" ]]; then - mv "$db_old" "$db" - else - touch "$db" - echo "home:$HOME" >> "$db" - fi - fi - echo "$db" + local dbdir="$HOME"/.local/lib + local db="$dbdir"/hyperjumpdb + local db_old="$HOME"/.hyperjumpdb + if [[ ! -f "$db" ]]; then + # Create ~/.local/lib directory if it fors not exists + if [[ ! -d "$dbdir" ]]; then + mkdir -p "$dbdir" >> /dev/null + fi + # If old DB exists, move it to the new location + if [[ -f "$db_old" ]]; then + mv "$db_old" "$db" + else + touch "$db" + echo "home:$HOME" >> "$db" + fi + fi + echo "$db" } -# Jump Remamber - Adds a jump to the database +# Sort Hyperjump Database +function hjsort() { + local dbdir="$HOME"/.local/lib + local db="$dbdir"/hyperjumpdb + if [ -x "$(command -v sort)" ] ; then + if [[ -f $db ]] ; then + sort -t: -o $db $db + else + echo "Can't sort hyperjump database: db does not exist" + fi + else + echo "Can't sort hyperjump database: sort command does not exist" + fi +} + +# Jump Remember - Adds a jump to the database function jr() { - local db=$(_hyperjumpdatabase) - local wd=$(pwd) - local nick=${wd##*/} - local nick=${nick// /_} - - if grep -q "$wd$" "$db"; then - echo "This directory is already added to the database. Run 'jf' to forget it." - else - if [[ -z "$1" ]]; then - echo "We need a nickname for this directory. Use jr or specify it now." - read -p "[C]ancel, [U]se \"$nick\", Enter [N]ickname [C/U/N]: " -n 1 -e choice - case "$choice" in - "U" | "u" ) - echo "We are going to use $nick as the nickname for this directory." - ;; - "N" | "n" ) - local nick= - while [[ "$nick" = "" ]]; do - read -p "Enter a Nickname for this Directory and Press [Enter]: " -e nick - done - ;; - * ) - echo "Nothing was added to the database. Quitting." - return - ;; - esac - else - local nick=$1 - fi - - local nick=${nick// /_} - - if grep -q "^$nick:" "$db"; then - echo "Oops, the nickname '$nick' is already in use :( Try again...." - else - echo "$nick:$wd" >> "$db" - echo "Added $wd with Nickname \"$nick\" to the database." - fi - fi + local db=$(_hyperjumpdatabase) + local wd=$(pwd) + local nick=${wd##*/} + local nick=${nick// /_} + + if grep -q ":$wd$" "$db"; then + + local dbline=$(grep ":$wd$" "$db" | head -n 1) + local nickname=${dbline%:*} + + echo "This directory is already added as '$nickname': run 'jf' to forget it." + else + if [[ -z "$1" ]]; then + echo "We need a nickname for this directory. Use jr or specify it now." + read -p "[C]ancel, [U]se \"$nick\", Enter [N]ickname [C/U/N]: " -n 1 -e choice + case "$choice" in + "U" | "u" ) + echo "We are going to use $nick as the nickname for this directory." + ;; + "N" | "n" ) + local nick= + while [[ "$nick" = "" ]]; do + read -p "Enter a Nickname for this Directory and Press [Enter]: " -e nick + done + ;; + * ) + echo "Nothing was added to the database. Quitting." + return + ;; + esac + else + local nick=$1 + fi + + local nick=${nick// /_} + + local colon=: + + if grep -q "^$nick:" "$db"; then + echo "Oops, the nickname '$nick' is already in use :( Try again...." + elif grep -q ":" <<<"$nick" ; then # [ -z "${nick##*$colon*}" ] ; then + echo "Oops, the nickname '$nick' contains a colon. Try again...." + + else + echo "$nick:$wd" >> "$db" + echo "Added $wd with Nickname \"$nick\" to the database." + if [ -x "$(command -v sort)" ] ; then + sort -t: -o $db $db + fi + fi + fi } # Jump Forget - Removes a jump location from the database function jf() { - local db=$(_hyperjumpdatabase) - - if [[ -z "$1" ]]; then - local wd=$(pwd) - if grep -q ":$wd$" "$db"; then - local dbline=$(grep ":$wd$" "$db" | head -n 1) - local nickname=${dbline%:*} - else - echo "This directory is not in the database!" - return - fi - else - local nickname=$1 - if grep -q "^$nickname:" "$db"; then - local line=$(grep "^$nickname:" "$db" | head -n 1) - local wd=${line#*:} - else - echo "This nickname is not in the database!" - return - fi - fi - - echo "$nickname : $wd" - read -p "Forget It? [Y/N]: " -n 1 -e choice - case "$choice" in - "Y" | "y" ) - local tempfile=$(mktemp -t "XXXjumpdb") - grep -v ":$wd$" "$db" > "$tempfile" - cat "$tempfile" > "$db" - rm "$tempfile" - echo "$nickname is forgotten!" - ;; - * ) - echo "Nothing was deleted from the database. Quitting." - return - ;; - esac + local db=$(_hyperjumpdatabase) + + if [[ -z "$1" ]]; then + local wd=$(pwd) + if grep -q ":$wd$" "$db"; then + local dbline=$(grep ":$wd$" "$db" | head -n 1) + local nickname=${dbline%:*} + else + echo "This directory is not in the database!" + return + fi + else + local nickname=$1 + if grep -q "^$nickname:" "$db"; then + local line=$(grep "^$nickname:" "$db" | head -n 1) + local wd=${line#*:} + else + echo "This nickname is not in the database!" + return + fi + fi + + echo "$nickname : $wd" + read -p "Forget It? [Y/N]: " -n 1 -e choice + case "$choice" in + "Y" | "y" ) + local tempfile=$(mktemp -t "XXXjumpdb") + grep -v ":$wd$" "$db" > "$tempfile" + cat "$tempfile" > "$db" + rm "$tempfile" + echo "$nickname is forgotten!" + ;; + * ) + echo "Nothing was deleted from the database. Quitting." + return + ;; + esac } # Jump to Nickname - Shows the Dialog Menu with all of the jumps in the db function jj() { - local db=$(_hyperjumpdatabase) - local foundDialog=0 - - # If no name on the prompt, than pop up the dialog, else use $1 - if [[ -z "$1" ]]; then - if ! type dialog > /dev/null 2>&1; then - # Dialog Utility NOT found, so show alternative - echo Dialog utility NOT found. Install Dialog to get a nice menu of jump locations. - echo List of Saved Locations: - while read line - do - printf " %-25s %s \n" "${line%:*}" "${line#*:}" - done < <(cat "$db") - else - local foundDialog=1 - local list="" - while read line - do - line="'${line%:*}' '${line#*:}' " - list+=$line - done < <(cat "$db") - - if [[ "$list" == "" ]]; then - echo The HyperJump Database is Empty. Bookmark a directory with the jr command to get started. - else - local cmd="dialog --menu 'Where do you want to jump to?' 22 76 16 $list" - local choice=$(eval "$cmd" 2>&1 >/dev/tty) - clear - fi - fi - else - local choice=$1 - fi - - # Check if the Jump is legit, and jump - if grep -q "^$choice:" "$db"; then - local line=$(grep "^$choice:" "$db" | head -n 1) - local target=${line#*:} - echo Navigating to "$choice" at "$target" - cd "$target" - # Run Additional Commands If Specified - local param - for param in ${@:2} - do - if [[ ! -z "$param" && "$param" != "" ]]; then - local cmd="$param ./" - echo Running \"$cmd\" inside $choice - eval "$cmd" - fi - done - else - if [[ -z "$choice" ]]; then - # Do not show the message if Dialog was not found - if [[ "$foundDialog" -eq 1 ]]; then - echo "Jump Cancelled" - fi - else - echo "Jump Nickname isn't in the Database" - fi - fi + local db=$(_hyperjumpdatabase) + local foundDialog=0 + + # If no name on the prompt, than pop up the dialog, else use $1 + if [[ -z "$1" ]]; then + if ! type dialog > /dev/null 2>&1; then + # Dialog Utility NOT found, so show alternative + echo Dialog utility NOT found. Install Dialog to get a nice menu of jump locations. + echo List of Saved Locations: + while read line + do + printf " %-25s %s \n" "${line%:*}" "${line#*:}" + done < <(cat "$db") + else + local foundDialog=1 + local list="" + while read line + do + line="'${line%:*}' '${line#*:}' " + list+=$line + done < <(cat "$db") + + if [[ "$list" == "" ]]; then + echo The HyperJump Database is Empty. Bookmark a directory with the jr command to get started. + else + local cmd="dialog --menu 'Where do you want to jump to?' 22 76 16 $list" + local choice=$(eval "$cmd" 2>&1 >/dev/tty) + clear + fi + fi + else + local choice=$1 + fi + + # Check if the Jump is legit, and jump + if grep -q "^$choice:" "$db"; then + local line=$(grep "^$choice:" "$db" | head -n 1) + local target=${line#*:} + echo Navigating to "$choice" at "$target" + cd "$target" + # Run Additional Commands If Specified + local param + for param in ${@:2} + do + if [[ ! -z "$param" && "$param" != "" ]]; then + local cmd="$param ./" + echo Running \"$cmd\" inside $choice + eval "$cmd" + fi + done + else + if [[ -z "$choice" ]]; then + # Do not show the message if Dialog was not found + if [[ "$foundDialog" -eq 1 ]]; then + echo "Jump Cancelled" + fi + else + echo "Jump Nickname isn't in the Database" + fi + fi +} + +# Jump Push to Nickname - Shows the Dialog Menu with all of the jumps in the db +function jp() { + local db=$(_hyperjumpdatabase) + local foundDialog=0 + + # If no name on the prompt, than pop up the dialog, else use $1 + if [[ -z "$1" ]]; then + if ! type dialog > /dev/null 2>&1; then + # Dialog Utility NOT found, so show alternative + echo Dialog utility NOT found. Install Dialog to get a nice menu of jump locations. + echo List of Saved Locations: + while read line + do + printf " %-25s %s \n" "${line%:*}" "${line#*:}" + done < <(cat "$db") + else + local foundDialog=1 + local list="" + while read line + do + line="'${line%:*}' '${line#*:}' " + list+=$line + done < <(cat "$db") + + if [[ "$list" == "" ]]; then + echo The HyperJump Database is Empty. Bookmark a directory with the jr command to get started. + else + local cmd="dialog --menu 'Where do you want to jump push to?' 22 76 16 $list" + local choice=$(eval "$cmd" 2>&1 >/dev/tty) + clear + fi + fi + else + local choice=$1 + fi + + # Check if the Jump is legit, and jump + if grep -q "^$choice:" "$db"; then + local line=$(grep "^$choice:" "$db" | head -n 1) + local target=${line#*:} + echo Navigating to "$choice" at "$target" and adding to dirs + pushd "$target" + # Run Additional Commands If Specified + local param + for param in ${@:2} + do + if [[ ! -z "$param" && "$param" != "" ]]; then + local cmd="$param ./" + echo Running \"$cmd\" inside $choice + eval "$cmd" + fi + done + else + if [[ -z "$choice" ]]; then + # Do not show the message if Dialog was not found + if [[ "$foundDialog" -eq 1 ]]; then + echo "Jump Cancelled" + fi + else + echo "Jump Nickname isn't in the Database" + fi + fi } # Autocomplete for Jump to Nickname _jj() { - local db=$(_hyperjumpdatabase) - - local list="" - while read line - do - local list+=" ${line%:*}" - done < <(cat "$db") - - local cur=${COMP_WORDS[COMP_CWORD]} - if [[ "$COMP_CWORD" -eq 1 ]]; then - COMPREPLY=( $(compgen -W "$list" -- "$cur") ) - else - COMPREPLY=( $(compgen -c "$cur") ) - fi + local db=$(_hyperjumpdatabase) + + local list="" + while read line + do + local list+=" ${line%:*}" + done < <(cat "$db") + + local cur=${COMP_WORDS[COMP_CWORD]} + if [[ "$COMP_CWORD" -eq 1 ]]; then + COMPREPLY=( $(compgen -W "$list" -- "$cur") ) + else + COMPREPLY=( $(compgen -c "$cur") ) + fi } _jr() { - local wd=$(pwd) - local nick=${wd##*/} - local nick=${nick// /_} - local cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=( $(compgen -W "$nick" -- "$cur") ) + local wd=$(pwd) + local nick=${wd##*/} + local nick=${nick// /_} + local cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=( $(compgen -W "$nick" -- "$cur") ) } if [[ -n "${ZSH_VERSION-}" ]]; then - autoload -U +X bashcompinit && bashcompinit + autoload -U +X bashcompinit && bashcompinit fi complete -F _jj jj +complete -F _jj jp complete -F _jj jf -complete -F _jr jr \ No newline at end of file +complete -F _jr jr