Skip to content

Commit

Permalink
Autogenerate ZSH completions base on trurl.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmealey committed Dec 13, 2024
1 parent b6aef35 commit c794484
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# compiled program
/trurl
/trurl.1
_trurl.zsh

# Prerequisites
*.d
Expand Down Expand Up @@ -60,3 +62,6 @@ winbuild/obj/

# Dependencies for msvc from vcpkg
winbuild/vcpkg_installed/

# vim
*.sw*
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LDLIBS += $$(curl-config --libs)
CFLAGS += $$(curl-config --cflags)
endif
CFLAGS += -W -Wall -Wshadow -pedantic
CFLAGS += -Wconversion -Wmissing-prototypes -Wwrite-strings -Wsign-compare -Wno-sign-conversion
CFLAGS += -Wconversion -Wmissing-prototypes -Wwrite-strings -Wsign-compare -Wno-sign-conversion -fno-lto
ifndef NDEBUG
CFLAGS += -Werror -g
endif
Expand All @@ -38,6 +38,8 @@ MANUAL = trurl.1
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man/man1
ZSH_COMPLETIONSDIR ?= $(PREFIX)/share/zsh/site-functions
COMPLETION_FILES=completions/_trurl.zsh

INSTALL ?= install
PYTHON3 ?= python3
Expand All @@ -55,10 +57,14 @@ install:
(if test -f $(MANUAL); then \
$(INSTALL) -m 0644 $(MANUAL) $(DESTDIR)$(MANDIR); \
fi)
(if test -f $(COMPLETION_FILES); then \
$(INSTALL) -d $(DESTDIR)$(ZSH_COMPLETIONSDIR); \
$(INSTALL) -m 0755 $(COMPLETION_FILES) $(ZSH_COMPLETIONSDIR)/_trurl; \
fi)

.PHONY: clean
clean:
rm -f $(OBJS) $(TARGET)
rm -f $(OBJS) $(TARGET) $(COMPLETION_FILES)

.PHONY: test
test: $(TARGET)
Expand All @@ -71,3 +77,7 @@ test-memory: $(TARGET)
.PHONY: checksrc
checksrc:
./checksrc.pl trurl.c version.h

.PHONY: completions
completions: trurl.md
./completions/generate_completions.sh $^
42 changes: 42 additions & 0 deletions completions/_trurl.zsh.in
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[@]}"
59 changes: 59 additions & 0 deletions completions/generate_completions.sh
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"

0 comments on commit c794484

Please sign in to comment.