written on 31/01/2015
Projects. Projects. Projects. After 5 semesters, cooped up in the same place, SO-VERY-FAR away from home, i finally realize what "monotonous" means. Projects are all that remain, that are even remotely fun(..that is, apart from watching TV Shows :D ). Anyways, there's this project i wanted to do. It's been done by the awesome people over at YHatHQ. It's a Fuzzy Matcher and it uses the FuzzyWuzzy Python Library developed by the people at SeatGeek to handle their ticket stubs duplication and etc etc. Now, in the DataScience, OSEMN(Obtain Data, Scrub Data, Exploratory Analysis, Modeling and iNterpreting results) approach, the more perfect the data is, the better the classifier accuracy and stuff. So, the project seemed a good approach to remove duplicates and clean the dataset. Before moving to Python web-apps i wanted to create a fuzzy-finder for my Terminal. Also, when you're developing something, why not just go full-in, with whatever features you can muster. The only rule "It has to be done completely in BASH/SHELL". No, Python scripts to make certain tasks easier. Finally i come up with: goto: A 'cd' tool on steroids. :)
goto is designed to be a complete replacement for cd, ie. the change directory tool in Linux. With, extra features. A Fuzzy-File Finder and a Directory Shortcut Creator. Something that can make switching between different folders/directories quite easy.
So, how does it work?
Usage:
goto -h||-help||h||help : Prints the Usage
goto <location_address> : Takes you to the <location_address>
goto s||-s <shortcut>   : Saves the Current Directory location as <shortcut>
goto l||-l              : Lists all the Bookmarks saved.
goto d||-d <shortcut>   : Deletes the <shortcut> saved.
goto p||-p <shortcut>   : Prints the GOTO directory for <shortcut>
goto f||-f||find||-find <term>     : Fuzzy-Finds all the file/folder matching REGEX in the working directory.
usage.help, list or goto <location_address>.save_shortcut, delete_shortcut, print_shortcut and fuzzy_find.Usage.~$ goto s <shortcut>~/.GOTO as a text file <shortcut>.skt with the current working directory inside.~$ goto ~/downloads
~$ goto s d
Shortcut Created.!
d.skt with the location of downloads in it. Let's see:
~$ cat ~/.GOTO/d.skt
/home/ankitvad/downloads~$ goto d~$ goto <location_address>if [ -e "$savefile$first_parameter.skt" ]; then
	x="$savefile$first_parameter.skt"
	y="$(cat $x)"
	cd $y
fi~$ goto dd.skt exists or not. If it does, then we move to the value inside the file, else, we try moving directly to the value. This helps to replicate cd's normal features also.
if [ -e "$savefile$first_parameter.skt" ]; then
	x="$savefile$first_parameter.skt"
	y="$(cat $x)"
	cd $y
	#If not, just CD to that location.
elif [ -e "$first_parameter" ]; then
	cd "$first_parameter"
else
	echo -e "The Location/File/Folder $first_parameter does not exist."
fi~$ goto ~/tmp/home/ankitvad/tmp.goto l function that lists all the bookmarks saved till now.
if [ "$first_parameter" = "l" ] || [ "$first_parameter" = "-l" ] ; then
	if [ ! -e "$savefile" ]; then
	echo -e "You have not added any directory yet."
	mkdir $savefile
	usage
elif [ ! "$(ls $savefile)" ]; then
	echo -e "You have not added any directory yet."
	usage
else
	#Reverse the path. Delete everything after / and before .
	for file in $savefile*.skt; do
		x="$(echo "$file" | rev)"
		y=${x%%/*}
		x="$(echo -n ${y##*.} | rev)"
		echo -n "$x" 
		echo " :  $(cat $file)"
		echo -e " "
		done
fi~/.GOTO/ If it does then a simple loop runs to chose all .skt files. Here is the place where the first complication arose. Technically,
for file in $savefile*.skt; do~/.GOTO called hello.skt which has the home directory saved in it. Then the variable file gives us:
/home/ankitvad/.GOTO/hello.skt~$ goto d||p||find <shortcut>regex_value="[A-Za-z0-9]*"
length=${#term}
x="$regex_value"
for (( i=0; i<$length; i++ )); do
	y="$x${term:$i:1}"
	x="$y$regex_value"
done 
regex_pattern="$x"
#i just need: [A-Za-z0-9]* after each literal, or before.
ls -R $pwd | grep -i $regex_patternst.cs.
Now, technically this should return us: style.css file. For this we port it into the REGEX search able term:
[A-Za-z0-9]*s[A-Za-z0-9]*t[A-Za-z0-9]*.[A-Za-z0-9]*c[A-Za-z0-9]*s[A-Za-z0-9]*ls -R $pwd | grep -i $regex_pattern~$ goto find bl.ht
blog2.html
finallyupwithablog.html
blog.html
blog.html
~$ find -name blog.html
./caffeinepost/blog.html
./ankitvad.github.io/blog/blog.html
~$ goto <location_address> command should suffice. And that's it. That's the whole program.
~$ cd command. The logic behind that is the fact that the 'cd' runs as a child process of the bash program execution. So, even though the code changes the directory, it dies as the goto execution finishes and hence it reverts back to the original directory. To avoid this, we either need to create functions for the 'cd' command and add that in the .bashrc or instead of running goto as: bash goto we can run it as: . goto. This gives it independent-parent process priority. Jumping to the Makefile:
bold=`tput bold`
normal=`tput sgr0`
all:
	@echo "Run ${bold}'make install'${normal}."
install:
	bash install.sh
	
.PHONY: all install
#!/bin/bash
INSTALL=~/.local/bin
BASH_FILE=~/.bashrc
bold=`tput bold`
normal=`tput sgr0`
mkdir -p "$INSTALL"
cp goto "$INSTALL"
echo 'export PATH=$PATH:~/.local/bin/' >> "$BASH_FILE"
echo 'alias goto=". goto"' >> "$BASH_FILE"
echo -e "Added Stuff in .bashrc"
. ~/.bashrc
exit 0export PATH=$PATH:~/.local/bin/
alias goto=". goto".bashrc file. The alias is required so that we can run goto as ~$ . goto~$ bash goto~$ git clone "https://github.com/ankitvad/goto"
~$ cd goto
~$ make install~$ wget -O goto.zip "https://github.com/ankitvad/goto/archive/master.zip"
~$ unzip goto.zip
~$ cd goto-master
~$ make install