Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zsh completion #43

Merged
merged 2 commits into from Jan 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -8,6 +8,8 @@ MANDIR=$(DESTDIR)/usr/share/man/man1/
TARGET_PATH=$(DESTDIR)/usr/bin
DISTFILES=jshon
MANFILE=jshon.1
ZSHSRC=jshon_zsh_completion
ZSHCOMP=$(DESTDIR)/usr/share/zsh/site-functions/_pbpst

#VERSION=$(shell date +%Y%m%d)
VERSION=$(shell git show -s --format="%ci" HEAD | cut -d ' ' -f 1 | tr -d '-')
Expand All @@ -27,6 +29,7 @@ clean:
install:
$(INSTALL) -D $(DISTFILES) $(TARGET_PATH)/$(DISTFILES)
$(INSTALL) -D $(MANFILE) $(MANDIR)/$(MANFILE)
$(INSTALL) -D $(ZSHSRC) $(ZSHCOMP)

dist: clean
sed -i "s/#define JSHONVER .*/#define JSHONVER ${VERSION}/" jshon.c
Expand Down
58 changes: 58 additions & 0 deletions jshon_zsh_completion
@@ -0,0 +1,58 @@
#compdef jshon

typeset -A opt_args
setopt extendedglob

# general operations
_jshon_opts_operations=(
-t'[returns type]'
-l'[returns length (integer)]'
-k'[returns newline seperated list of keys]'
-p'[pops the last manipulation from the stack]'
-a'[maps the remaining actions across the selected element]'
-j'[returns encoded json]'
-u'[returns decoded string]'
-n'[returns a json element to be inserted into a structure]'
-s'[returns a json encoded string]'
)

_jshon_opts_index=(
-e'[returns json value at index]'
-i'[insert item into array at index]'
-d'[removes item in array or object]'
)

# options for passing to _arguments: options common to all operations
_jshon_opts_common=(
-P'[strips a jsonp callback]'
-S'[returns output sorted by key]'
-Q'[disables error reporting on stderr]'
-V'[enables pass by value on the edit stack]'
-F'[<path> read from a file instead of stdin]:Path to file:_files -./'
-I'[In place editing (only works with -F)]'
-C'[continue on potentially recoverable errors]'
-0'[null delimiters - changes delimiter of -u from newline to null]' #only works for -u
--version'[returns a YYYYMMDD timestamp and exits]'
)

_jshon_action_none() {
_arguments -s : \
"$_jshon_opts_operations[@]" \
"$_jshon_opts_common[@]" \
"$_jshon_opts_index[@]" \
}

# main dispatcher
_jshon() {
local -a args cmds;
local tmp
args=( ${${${(M)words:#-*}#-}:#-*} )
for tmp in $words; do
cmds+=("${${_jshon_opts_operations[(r)*$tmp\[*]%%\[*}#*\)}")
done
case $args in #$words[2] in
*) _jshon_action_none ;;
esac
}

_jshon "$@"