-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autogenerate ZSH completions base on trurl.md
- Loading branch information
1 parent
b6aef35
commit c794484
Showing
4 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#compdef trurl | ||
|
||
# This file is generated from trurls generate_completions.sh | ||
|
||
# standalone flags - things that have now follow on | ||
standalone_flags=(@TRURL_STANDALONE_FLAGS@) | ||
|
||
#component options - flags that expected to come after them | ||
component_options=(@TRURL_COMPONENT_OPTIONS@) | ||
|
||
# components that take *something* as a param but we can't | ||
# be sure what | ||
random_options=(@TRURL_RANDOM_OPTIONS@) | ||
|
||
# Components are specific URL parts that are only completed | ||
# after a component_options appears | ||
component_list=( @TRURL_COMPONENT_LIST@) | ||
|
||
if (( "${component_options[(Ie)${words[$CURRENT-1]}]}" )); then | ||
compadd -S "=" "${component_list[@]}" | ||
return 0 | ||
fi | ||
|
||
# if we expect another parameter that trurl doesn't define then | ||
# we should (i.e. a component) then fall back on ZSH _path_file | ||
if (( "${random_options[(Ie)${words[$CURRENT-1]}]}" )); then | ||
_path_files | ||
return 0 | ||
fi | ||
|
||
# calling compadd directly allows us the let the flags be | ||
# repeatable so we can recall --set, --get etc. | ||
repeatable=( "${component_options[@]}" "${random_options[@]}" ) | ||
args=( "${repeatable[@]}" ) | ||
# only apply single completions which haven't been used. | ||
for sf in "${standalone_flags[@]}"; do | ||
if ! (( "${words[(Ie)$sf]}" )); then | ||
args+=("$sf") | ||
fi | ||
done | ||
|
||
compadd "${args[@]}" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/bash | ||
|
||
if [ -z "$1" ]; then | ||
echo "expected a trurl.1 file to be passed in..." | ||
exit 1 | ||
fi | ||
|
||
TRURL_1_FILE=$1 | ||
|
||
|
||
|
||
ALL_FLAGS="$(sed -n \ | ||
-e 's/"//g' \ | ||
-e '/\# URL COMPONENTS/q;p' \ | ||
< "${TRURL_1_FILE}" \ | ||
| grep "##" \ | ||
| awk '{printf "%s%s%s%s ", $2, $3, $4, $5}')" | ||
|
||
|
||
TRURL_COMPONENT_OPTIONS="" | ||
TRURL_STANDALONE_FLAGS="" | ||
TRURL_RANDOM_OPTIONS="" | ||
TRURL_COMPONENT_LIST="$(sed -n \ | ||
-e 's/"//g' \ | ||
-e '1,/\# URL COMPONENTS/ d' \ | ||
-e '/\# JSON output format/q;p' \ | ||
< "${TRURL_1_FILE}" \ | ||
| grep "##" \ | ||
| awk '{printf "\"%s\" ", $2}')" | ||
|
||
for flag in $ALL_FLAGS; do | ||
# these are now TRURL_STANDALONE | ||
if echo "$flag" | grep -q "="; then | ||
TRURL_COMPONENT_OPTIONS+="$(echo "$flag" \ | ||
| awk '{split($0, a, ","); for(i in a) {printf "%s ", a[i]}}' \ | ||
| cut -f1 -d '[' \ | ||
| awk '{printf "\"%s\" ", $1}')" | ||
elif echo "$flag" | grep -q "\["; then | ||
TRURL_RANDOM_OPTIONS+="$(echo "$flag" \ | ||
| awk '{split($0, a, ","); for(i in a) {printf "%s ", a[i]}}' \ | ||
| cut -f1 -d '[' \ | ||
| awk '{printf "\"%s\" ", $1}')" | ||
else | ||
TRURL_STANDALONE_FLAGS+="$(echo "$flag" \ | ||
| awk '{split($0, a, ","); for(i in a) {printf "\"%s\" ", a[i]}}')" | ||
fi | ||
done | ||
|
||
function generate_zsh() { | ||
|
||
sed -e "s/@TRURL_RANDOM_OPTIONS@/${TRURL_RANDOM_OPTIONS}/g" \ | ||
-e "s/@TRURL_STANDALONE_FLAGS@/${TRURL_STANDALONE_FLAGS}/g" \ | ||
-e "s/@TRURL_COMPONENT_OPTIONS@/${TRURL_COMPONENT_OPTIONS}/g" \ | ||
-e "s/@TRURL_COMPONENT_LIST@/${TRURL_COMPONENT_LIST}/g" \ | ||
./completions/_trurl.zsh.in > ./completions/_trurl.zsh | ||
|
||
} | ||
|
||
generate_zsh "$TRURL_RANDOM_OPTIONS" |