Last active
September 27, 2015 23:37
-
-
Save meyer/1349579 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export CD_PATH='' | |
# Source a .cd file in a directory upon cd. OBVI. | |
function fancy_cd(){ | |
# Use the FOR REAL cd | |
command cd "$@" | |
NEW_CD_PATH=`pwd` | |
# Did we source this already? | |
if [[ $NEW_CD_PATH != $CD_PATH ]]; then | |
export CD_PATH=$NEW_CD_PATH | |
MAX_WIDTH=200 | |
FORMAT_TITLE="\033[0;37m\033[40m%-${MAX_WIDTH}s \033[00m\n" | |
FORMAT_ERROR="\n\033[0;31m %-${MAX_WIDTH}s \033[00m\n" | |
# Check if .cd exists | |
if [ -f ./.cd ]; then | |
IFS=$'\n' # Redefine the line separator | |
tput rmam # Disable line wrapping | |
printf "$FORMAT_TITLE" ".cd file located in `pwd`/" | |
tput smam # Re-enable line wrapping | |
for i in `cat ./.cd` | |
do | |
# Ignore cd | |
if [[ ${i:0:3} == 'cd ' ]]; then | |
IGNORED_COMMANDS="$IGNORED_COMMANDS\n ${i:0:$MAX_WIDTH}" | |
# Ignore bash-style comments | |
elif [[ ${i:0:1} == '#' ]]; then | |
COMMENTED_COMMANDS="$COMMENTED_COMMANDS\n ${i:1:$MAX_WIDTH}" | |
# Eval everything else | |
else | |
RUN_COMMANDS="$RUN_COMMANDS\n ${i:0:$MAX_WIDTH}" | |
# Not totally sold on this yet. | |
tput sc # save cursor position | |
echo -n "Running: $i..." | |
eval $i # run the line | |
tput el1 # clear line | |
tput rc # return to saved position | |
echo "Running: $i... Done!" # update | |
fi | |
done | |
unset IFS # Reset to default | |
# Print the results | |
if ! [ "${RUN_COMMANDS}" ]; then | |
# printf "$FORMAT_TITLE" 'This just happened:' | |
# echo -e $RUN_COMMANDS | |
printf "$FORMAT_ERROR" "Empty .cd file found in `pwd`" | |
fi | |
# if [ "${IGNORED_COMMANDS}" ]; then | |
# printf "$FORMAT_TITLE" 'Ignored commands:' | |
# echo -e $IGNORED_COMMANDS | |
# fi | |
# if [ "${COMMENTED_COMMANDS}" ]; then | |
# printf "$FORMAT_TITLE" 'Commented-out commands:' | |
# echo -e $COMMENTED_COMMANDS | |
# fi | |
# echo # GIMME SOME SPACE BRO | |
fi | |
fi | |
# Unset everything we don't need | |
unset NEW_CD_PATH RUN_COMMANDS IGNORED_COMMANDS COMMENTED_COMMANDS MAX_WIDTH FORMAT_TITLE FORMAT_ERROR | |
} | |
alias cd="fancy_cd" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WAT
Run the contents of a
.cd
file when changing directories. Useful for single-line environment-changing stuff, like activating a project-specific virtual environment uponcd
. THAT IS ALL.