Skip to content

Commit

Permalink
Enable autocomplete for bash (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
҈҈҈Luiz Branco authored Nov 21, 2017
1 parent 352ff19 commit 622adae
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ Bare zip archives per release version are available on https://releases.manifold
For instructions on Windows, [click here](./.github/WINDOWS.md).


## Autocomplete

If you have bash and bash-completion installed, you can enable autocomplete with:
```
curl -o- https://raw.githubusercontent.com/manifoldco/manifold-cli/master/autocomplete.sh | sh
```

## Quickstart

First you must create an account.
Expand Down
143 changes: 143 additions & 0 deletions autocomplete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/bin/sh

{ # this ensures the entire script is downloaded #

is_installed() {
type "$1" > /dev/null 2>&1
}

error_exit() {
if is_installed tput; then
tput sgr0
tput setaf 1
echo "ERROR: $1"
tput sgr0
else
echo "ERROR: $1"
fi
exit 1
}

success_msg() {
if is_installed tput; then
command printf "["
tput sgr0
tput setaf 2
command printf "OK"
tput sgr0
command printf "] ${1}\\n"
else
echo "[OK]: $1"
fi
}

warning_msg() {
if is_installed tput; then
command printf "["
tput sgr0
tput setaf 3
command printf "!!"
tput sgr0
command printf "] ${1}\\n"
else
echo "[!!]: $1"
fi
}

try_profile() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
echo "${1}"
}

#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
detect_profile() {
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
echo "${PROFILE}"
return
fi

local DETECTED_PROFILE
DETECTED_PROFILE=''
local SHELLTYPE
SHELLTYPE="$(basename "/$SHELL")"

if [ "$SHELLTYPE" = "bash" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ "$SHELLTYPE" = "zsh" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
fi

if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"
do
if DETECTED_PROFILE="$(try_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done
fi

if [ ! -z "$DETECTED_PROFILE" ]; then
echo "$DETECTED_PROFILE"
fi
}

autocomplete() {
local DETECTED_PROFILE
AUTOCOMPLETE=''

local SHELLTYPE
SHELLTYPE="$(basename "/$SHELL")"

if [ "$SHELLTYPE" = "bash" ]; then
AUTOCOMPLETE=$(cat <<'EOF'
#! /bin/bash
export MANIFOLD_AUTOCOMPLETE=true
_manifold_bash_autocomplete() {
local cur opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _manifold_bash_autocomplete manifold
EOF
)
fi

if [ ! -z "$AUTOCOMPLETE" ]; then
echo "$AUTOCOMPLETE"
fi
}

PROFILE=`detect_profile`
AUTOCOMPLETE=`autocomplete`

if [ -z "$MANIFOLD_DIR" ]; then
MANIFOLD_DIR="${HOME}/.manifold/bin"
fi

mkdir -p $MANIFOLD_DIR

if [ "$PROFILE" = "" ]; then
error_exit "Unable to locate profile settings file (something like $HOME/.bashrc or $HOME/.bash_profile)"
elif [ "$MANIFOLD_AUTOCOMPLETE" = "" ]; then
echo "$AUTOCOMPLETE" > "$MANIFOLD_DIR/.manifold_completion"
echo "source $MANIFOLD_DIR/.manifold_completion" >> $PROFILE

success_msg "Your $PROFILE has changed to manifold autocomplete"
warning_msg "Please restart or re-source your terminal session."
fi

}
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
app.Version = config.Version
app.Commands = append(cmds, helpCommand)
app.Flags = append(app.Flags, cli.HelpFlag)
app.EnableBashCompletion = true

app.Action = func(cliCtx *cli.Context) error {
// Show help if no arguments passed
Expand Down
4 changes: 2 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@

rm $FILENAME

cd $CURRENT_DIR

if is_installed manifold; then
PREVIOUS_INSTALLATION=`which manifold`
if [ "$PREVIOUS_INSTALLATION" = "$MANIFOLD_DIR/manifold" ]; then
Expand Down Expand Up @@ -175,6 +173,8 @@
warning_msg "Please restart or re-source your terminal session."
fi

cd $CURRENT_DIR

echo
success_msg "All done. Happy manifold use! :)"
}

0 comments on commit 622adae

Please sign in to comment.