.functions: Make more appealing to shellcheck

Still doesn't load properly on sh, though
This commit is contained in:
Salt 2018-11-27 23:32:44 -06:00
parent 9c4a950385
commit 20d704e9f9

View File

@ -10,20 +10,21 @@ proj() {
echo "proj: requires argument" echo "proj: requires argument"
return 1 return 1
fi fi
projname="${1//[^ a-zA-Z0-9.]/}" # POSIX mandates this external call to sed
projname="echo \"$1\" | sed 's/[^ a-zA-Z0-9.]//g'"
projdir="$HOME/Projects/$projname" projdir="$HOME/Projects/$projname"
# Ensure we have a ~/Projects directory # Ensure we have a ~/Projects directory
mkdir -p "$HOME/Projects" > /dev/null 2>&1 mkdir -p "$HOME/Projects" > /dev/null 2>&1
# cd into the project or make it if necessary # cd into the project or make it if necessary
if [ -d "$projdir" ]; then if [ -d "$projdir" ]; then
# It exists # It exists
cd "$projdir" cd "$projdir" || return 50
else else
# It does not exist # It does not exist
echo "Creating new project \"$projname\"" echo "Creating new project \"$projname\""
mkdir -p "$projdir" mkdir -p "$projdir"
cd "$projdir" cd "$projdir" || return 51
if which git > /dev/null 2>&1; then if command -v git > /dev/null 2>&1; then
# Initialize git # Initialize git
echo "Initializing git with .gitignore" echo "Initializing git with .gitignore"
git init > /dev/null 2>&1 git init > /dev/null 2>&1
@ -35,22 +36,31 @@ proj() {
fi fi
} }
# Autocompletion for bash # Autocompletion for bash
complete -F _proj proj > /dev/null 2>&1 && # 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 && \
_proj() { _proj() {
[ "${#COMP_WORDS[@]}" != "2" ] && return 0 [ "${#COMP_WORDS[@]}" != "2" ] && return 0
for dir in $HOME/Projects/*; do for dir in "$HOME"/Projects/*; do
reply="$(basename "$dir")" reply="$(basename "$dir")"
reply="${reply//[^ a-zA-Z0-9.]/}" reply="${reply//[^ a-zA-Z0-9.]/}"
# shellcheck disable=2179
COMPREPLY+=" $reply" COMPREPLY+=" $reply"
done done
unset reply unset reply
# shellcheck disable=2178
COMPREPLY=($(compgen -W "$COMPREPLY" "${COMP_WORDS[COMP_CWORD]}")) COMPREPLY=($(compgen -W "$COMPREPLY" "${COMP_WORDS[COMP_CWORD]}"))
return 0 return 0
} }
# Autocompletion for zsh # Autocompletion for zsh
compdef _proj proj > /dev/null 2>&1 && compdef _proj proj > /dev/null 2>&1 && \
_proj() { _proj() {
for dir in $HOME/Projects/*; do #! /usr/bin/env zsh
# It's okay, shellcheck
# We zsh now
# shellcheck disable=2039
for dir in "$HOME"/Projects/*; do
temp="$(basename "$dir")" temp="$(basename "$dir")"
temp="${reply//[^ a-zA-Z0-9.]/}" temp="${reply//[^ a-zA-Z0-9.]/}"
reply+=" $temp" reply+=" $temp"