2018-11-06 17:39:56 -06:00
|
|
|
#! /bin/sh
|
|
|
|
#
|
|
|
|
# .functions
|
|
|
|
# Functions for interactive shells
|
|
|
|
#
|
|
|
|
|
|
|
|
proj() {
|
|
|
|
# Ensure we have an argument
|
|
|
|
if [ -z ${1+x} ]; then
|
|
|
|
echo "proj: requires argument"
|
|
|
|
return 1
|
|
|
|
fi
|
2018-11-27 23:32:44 -06:00
|
|
|
# POSIX mandates this external call to sed
|
2018-11-27 23:34:24 -06:00
|
|
|
projname="$(echo "$1" | sed 's/[^ a-zA-Z0-9.]//g')"
|
2018-11-06 17:39:56 -06:00
|
|
|
projdir="$HOME/Projects/$projname"
|
|
|
|
# Ensure we have a ~/Projects directory
|
|
|
|
mkdir -p "$HOME/Projects" > /dev/null 2>&1
|
|
|
|
# cd into the project or make it if necessary
|
|
|
|
if [ -d "$projdir" ]; then
|
|
|
|
# It exists
|
2018-11-27 23:32:44 -06:00
|
|
|
cd "$projdir" || return 50
|
2018-11-06 17:39:56 -06:00
|
|
|
else
|
|
|
|
# It does not exist
|
|
|
|
echo "Creating new project \"$projname\""
|
|
|
|
mkdir -p "$projdir"
|
2018-11-27 23:32:44 -06:00
|
|
|
cd "$projdir" || return 51
|
|
|
|
if command -v git > /dev/null 2>&1; then
|
2018-11-06 17:39:56 -06:00
|
|
|
# Initialize git
|
|
|
|
echo "Initializing git with .gitignore"
|
|
|
|
git init > /dev/null 2>&1
|
|
|
|
echo '*.swp' > .gitignore
|
|
|
|
git add .gitignore > /dev/null 2>&1
|
|
|
|
git commit -am "Create gitignore" > /dev/null 2>&1
|
|
|
|
git status
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2018-11-06 18:20:37 -06:00
|
|
|
# Autocompletion for bash
|
2018-11-27 23:32:44 -06:00
|
|
|
# A note on the shellcheck disable: that's fine, because this is also a test
|
|
|
|
# If it fails, we don't even define a completion function
|
|
|
|
# shellcheck disable=2039
|
|
|
|
complete -F _proj proj > /dev/null 2>&1 && \
|
2018-11-06 18:20:37 -06:00
|
|
|
_proj() {
|
|
|
|
[ "${#COMP_WORDS[@]}" != "2" ] && return 0
|
2018-11-27 23:32:44 -06:00
|
|
|
for dir in "$HOME"/Projects/*; do
|
2018-11-06 18:20:37 -06:00
|
|
|
reply="$(basename "$dir")"
|
|
|
|
reply="${reply//[^ a-zA-Z0-9.]/}"
|
2018-11-27 23:32:44 -06:00
|
|
|
# shellcheck disable=2179
|
2018-11-06 18:20:37 -06:00
|
|
|
COMPREPLY+=" $reply"
|
|
|
|
done
|
2018-11-06 18:24:52 -06:00
|
|
|
unset reply
|
2018-11-27 23:32:44 -06:00
|
|
|
# shellcheck disable=2178
|
2018-11-06 18:20:37 -06:00
|
|
|
COMPREPLY=($(compgen -W "$COMPREPLY" "${COMP_WORDS[COMP_CWORD]}"))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
# Autocompletion for zsh
|
2018-11-27 23:32:44 -06:00
|
|
|
compdef _proj proj > /dev/null 2>&1 && \
|
2018-11-06 18:20:37 -06:00
|
|
|
_proj() {
|
2018-11-27 23:32:44 -06:00
|
|
|
#! /usr/bin/env zsh
|
|
|
|
# It's okay, shellcheck
|
|
|
|
# We zsh now
|
|
|
|
# shellcheck disable=2039
|
|
|
|
for dir in "$HOME"/Projects/*; do
|
2018-11-06 18:36:04 -06:00
|
|
|
temp="$(basename "$dir")"
|
|
|
|
temp="${reply//[^ a-zA-Z0-9.]/}"
|
|
|
|
reply+=" $temp"
|
|
|
|
done
|
2018-11-06 18:20:37 -06:00
|
|
|
return 0
|
|
|
|
}
|
2018-11-06 17:39:56 -06:00
|
|
|
|