Skip to content

Instantly share code, notes, and snippets.

@yomybaby
Last active March 1, 2016 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yomybaby/6c1ca0a32cf038159932 to your computer and use it in GitHub Desktop.
Save yomybaby/6c1ca0a32cf038159932 to your computer and use it in GitHub Desktop.
TiNy CLI auto-competition script

This i a TiNy CLI auto-competition bash script. I'm not sure this code works on Windows and Linux.

How to use

Download tn_completion.bash. Copy this file to your home directory, and add this to your .bashrc file:

source ~/tn_completion.bash

or

Just past script to .bashrc or .bash_profile

###-begin-tiny-completion-###
if complete &>/dev/null; then
_tiny(){
local cur prev opts base user_recipes buildin_recipes project_recipes available_recipes
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# prev_prev="${COMP_WORDS[COMP_CWORD-2]}"
#
# The basic options we'll complete.
#
opts="list recipes project save rename remove reset generate"
#
# build recipes list
#
# get recipes from `tn list`, but this is too slow :( so parsing several tn.json..
# available_recipes=$(tn list | sed '1,5d' | sed 's/:.*//g' | sed 's/^[ \t]*//;s/[ \t]*$//' | tr '\n' ' ')
user_recipes=$(cat ~/.tn.json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,"],"); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:.*\"//g' | sed 's/[\,]//g' | sed 's/\"//g' | sed 's/\]//g' | tr '\n' ' ')
buildin_recipes=$(cat /usr/local/lib/node_modules/tn/tn.json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,"],"); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:.*\"//g' | sed 's/[\,]//g' | sed 's/\"//g' | sed 's/\]//g' | tr '\n' ' ')
if [ -e "./tn.json" ]; then
project_recipes=$(cat ./tn.json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,"],"); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:.*\"//g' | sed 's/[\,]//g' | sed 's/\"//g' | sed 's/\]//g' | tr '\n' ' ')
available_recipes="${user_recipes} ${buildin_recipes} ${project_recipes}"
else
available_recipes="${user_recipes} ${buildin_recipes}"
fi
#
# Complete the arguments to some of the basic commands.
#
case "${prev}" in
project)
local args_project="save rename remove reset"
COMPREPLY=( $(compgen -W "${args_project}" -- ${cur}) )
return 0
;;
list | recipes | generate | save | reset) # do nothing more
return 0
;;
rename | remove) # do recipt name completion
COMPREPLY=( $(compgen -W "${available_recipes}" -- ${cur}) )
return 0
;;
*)
#COMPREPLY=( $(compgen -W "${recipe_list}" -- ${cur}) )
# return 0
;;
esac
if [[ ${cur} == -* ]] ; then
local dash_recipes arrIN
arrIN=(${available_recipes// / })
# add prefix --
for i in ${arrIN[@]/#/--}
do
dash_recipes=$dash_recipes" "$i
done
# # join
# dash_recipes=$(IFS= ; echo "${arrIN[*]}")
COMPREPLY=($(compgen -W "--help --verbose --version ${dash_recipes}" -- ${cur}))
return 0
else
COMPREPLY=($(compgen -W "${opts} ${available_recipes}" -- ${cur}))
return 0
fi
}
complete -F _tiny tn
fi
###-end-tiny-completion-###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment