|
| 1 | +#!/bin/bash |
| 2 | +#GOTO-BASH |
| 3 | +#A simple script to save Directory locations so as to make |
| 4 | +#moving between directory's easy. |
| 5 | +#Also, contains a fuzzy directory finder to make searching for folder |
| 6 | +#names easier. |
| 7 | +#A replacement for "cd". |
| 8 | +#By: Ankit Vadehra |
| 9 | +#Contact: ankitvad[at]gmail[dot]com |
| 10 | +#License: Standard MIT License. |
| 11 | + |
| 12 | +#Initialise the File to Save all Directory locations |
| 13 | +#Saving the Folder into the User folder. |
| 14 | +#This is necessary the first time and will |
| 15 | +#be omitted after that. |
| 16 | + |
| 17 | +#This is for bold and normal printing : |
| 18 | +bold=`tput bold` |
| 19 | +normal=`tput sgr0` |
| 20 | + |
| 21 | +#The saving directory. |
| 22 | +savefile=~/.GOTO/ |
| 23 | +if [ ! -e "$savefile" ]; then |
| 24 | + mkdir $savefile |
| 25 | +fi |
| 26 | + |
| 27 | +number_of_parameter="$#" |
| 28 | +first_parameter="$1" |
| 29 | +second_parameter="$2" |
| 30 | +#Cases depending on the number of Parmeters passed. |
| 31 | + |
| 32 | + |
| 33 | +usage(){ |
| 34 | + echo -e "${bold}'goto'${normal} is a replacement for the ${bold}cd${normal} tool. It has better functionalities than cd, like a ${bold}FuzzyFinder${normal} and a ${bold}Directory Location Saver${normal}." |
| 35 | + echo -e "Usage:\n" |
| 36 | + echo -n "${bold}goto -h||-help||h||help" |
| 37 | + echo -e "${normal} : Prints the Usage\n" |
| 38 | + echo -n "${bold}goto <location_address>" |
| 39 | + echo -e "${normal} : Takes you to the ${bold}<location_address>\n" |
| 40 | + echo -n "${bold}goto s||-s <shortcut>${normal}" |
| 41 | + echo -e "${normal} : Saves the Current Directory location as ${bold}<shortcut>\n" |
| 42 | + echo -n "${bold}goto l||-l${normal}" |
| 43 | + echo -e " : Lists all the Bookmarks saved.\n" |
| 44 | + echo -n "${bold}goto d||-d <shortcut>${normal}" |
| 45 | + echo -e " : Deletes the ${bold}<shortcut> ${normal}saved.\n" |
| 46 | + echo -n "${bold}goto p||-p <shortcut>${normal}" |
| 47 | + echo -e " : Prints the GOTO directory for ${bold}<shortcut>\n" |
| 48 | + echo -n "${bold}goto f||-f||find||-find <term>${normal}" |
| 49 | + echo -e " : Fuzzy-Finds all the file/folder matching REGEX in the working directory\n" |
| 50 | + |
| 51 | +} |
| 52 | +#To throw to Specific Functions |
| 53 | +one_parameter(){ |
| 54 | + #Passing all possible functions.. |
| 55 | + if [ "$first_parameter" = "-h" ] || [ "$first_parameter" = "-help" ] || [ "$first_parameter" = "help" ] || [ "$first_parameter" = "h" ] ; then |
| 56 | + #Printing the Usage Help. |
| 57 | + usage |
| 58 | + #For Printing the List |
| 59 | + elif [ "$first_parameter" = "l" ] || [ "$first_parameter" = "-l" ] ; then |
| 60 | + if [ ! -e "$savefile" ]; then |
| 61 | + echo -e "You have not added any directory yet." |
| 62 | + mkdir $savefile |
| 63 | + usage |
| 64 | + elif [ ! "$(ls $savefile)" ]; then |
| 65 | + echo -e "You have not added any directory yet." |
| 66 | + usage |
| 67 | + else |
| 68 | + #Reverse the path. Delete everything after / and before . |
| 69 | + for file in $savefile*.skt; do |
| 70 | + x="$(echo "$file" | rev)" |
| 71 | + y=${x%%/*} |
| 72 | + x="$(echo -n ${y##*.} | rev)" |
| 73 | + echo -n "$x" |
| 74 | + echo " : $(cat $file)" |
| 75 | + echo -e " " |
| 76 | + done |
| 77 | + fi |
| 78 | + else |
| 79 | + #Change the Directory to target. |
| 80 | + #here check if shortcut exists |
| 81 | + if [ -e "$savefile$first_parameter.skt" ]; then |
| 82 | + x="$savefile$first_parameter.skt" |
| 83 | + y="$(cat $x)" |
| 84 | + cd $y |
| 85 | + #If not, just CD to that location. |
| 86 | + elif [ -e "$first_parameter" ]; then |
| 87 | + cd "$first_parameter" |
| 88 | + else |
| 89 | + echo -e "The Location/File/Folder $first_parameter does not exist." |
| 90 | + fi |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +two_parameter(){ |
| 95 | + #For saving. |
| 96 | + if [ "$first_parameter" = "s" ] || [ "$first_parameter" = "-s" ] ; then |
| 97 | + save_shortcut |
| 98 | + elif [ "$first_parameter" = "p" ] || [ "$first_parameter" = "-p" ] ; then |
| 99 | + print_shortcut |
| 100 | + elif [ "$first_parameter" = "d" ] || [ "$first_parameter" = "-d" ] ; then |
| 101 | + delete_shortcut |
| 102 | + elif [ "$first_parameter" = "f" ] || [ "$first_parameter" = "-f" ] || [ "$first_parameter" = "find" ] || [ "$first_parameter" = "-find" ] ; then |
| 103 | + working_dir_finder |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +save_shortcut(){ |
| 108 | + if [ -e "$savefile$second_parameter.skt" ]; then |
| 109 | + x="$savefile$second_parameter.skt" |
| 110 | + echo -e "Existing Shortcut:" |
| 111 | + echo -n "$second_parameter : " |
| 112 | + echo -e "$(cat $x)" |
| 113 | + echo -e "\nThis Shortcut allready exists. Chose another one or delete the existing one." |
| 114 | + else |
| 115 | + pwd > "$savefile$second_parameter.skt" |
| 116 | + echo -e "Shortcut Created." |
| 117 | + fi |
| 118 | +} |
| 119 | + |
| 120 | +print_shortcut(){ |
| 121 | + if [ ! -e "$savefile$second_parameter.skt" ]; then |
| 122 | + echo -e "The Shortcut you're trying to find does not exist." |
| 123 | + echo -e "Please see the Shortcut Links for a complete list." |
| 124 | + else |
| 125 | + x="$savefile$second_parameter.skt" |
| 126 | + echo -n "$second_parameter : " |
| 127 | + echo -e "$(cat $x)" |
| 128 | + fi |
| 129 | +} |
| 130 | + |
| 131 | +delete_shortcut(){ |
| 132 | + if [ ! -e "$savefile$second_parameter.skt" ]; then |
| 133 | + echo -e "The Shortcut you're trying to find does not exist." |
| 134 | + echo -e "Please see the Shortcut Links for a complete list." |
| 135 | + else |
| 136 | + echo -e "Are you sure you want to delete: " |
| 137 | + x="$savefile$second_parameter.skt" |
| 138 | + echo -n "$second_parameter : " |
| 139 | + echo -e "$(cat $x)" |
| 140 | + echo -e "(yes/no)" |
| 141 | + read choice |
| 142 | + if [ "$choice" = "yes" ] || [ "$choice" = "y" ] ; then |
| 143 | + rm "$x" |
| 144 | + echo -e "Shortcut Deleted." |
| 145 | + fi |
| 146 | + fi |
| 147 | +} |
| 148 | + |
| 149 | +working_dir_finder(){ |
| 150 | + #This function is used for finding a particular file in the working directory. |
| 151 | + term="$second_parameter" |
| 152 | + regex_value="[A-Za-z0-9]*" |
| 153 | + length=${#term} |
| 154 | + x="$regex_value" |
| 155 | + for (( i=0; i<$length; i++ )); do |
| 156 | + y="$x${term:$i:1}" |
| 157 | + x="$y$regex_value" |
| 158 | + done |
| 159 | + regex_pattern="$x" |
| 160 | + #echo -e "\nThe REGEX Pattern is : $regex_pattern" |
| 161 | + #i just need: [A-Za-z0-9]* after each literal, or before. no idea. |
| 162 | + ls -R $pwd | grep -i $regex_pattern |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +case "$number_of_parameter" in |
| 167 | + # 0 parameters, ie. general tool definition printing. |
| 168 | + 0) usage |
| 169 | + ;; |
| 170 | + #1-Parameter for: help, list or cd feauture. |
| 171 | + 1) one_parameter |
| 172 | + ;; |
| 173 | + #2-Parameter for: save, print, delete, find and findall |
| 174 | + #feauture. |
| 175 | + 2) two_parameter |
| 176 | + ;; |
| 177 | + #More than 2 is wrong, and should throw a usage error. |
| 178 | + *) usage |
| 179 | + ;; |
| 180 | +esac |
| 181 | + |
| 182 | + |
| 183 | + |
0 commit comments