From 6b52fa5aea0e3bc5160b63172952d6792003fa98 Mon Sep 17 00:00:00 2001 From: Jacob Mealey Date: Mon, 9 Dec 2024 21:51:01 -0500 Subject: [PATCH] Autogenerate ZSH completions based on trurl.md --- .gitignore | 5 ++ Makefile | 12 ++++- completions/_trurl.zsh.in | 66 +++++++++++++++++++++++ completions/generate_completions.sh | 81 +++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 completions/_trurl.zsh.in create mode 100755 completions/generate_completions.sh diff --git a/.gitignore b/.gitignore index 37e8aaeb..b0d7be3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # compiled program /trurl +/trurl.1 +_trurl.zsh # Prerequisites *.d @@ -60,3 +62,6 @@ winbuild/obj/ # Dependencies for msvc from vcpkg winbuild/vcpkg_installed/ + +# vim +*.sw* diff --git a/Makefile b/Makefile index 84d941e8..67acb5ce 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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) @@ -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 $^ diff --git a/completions/_trurl.zsh.in b/completions/_trurl.zsh.in new file mode 100644 index 00000000..a063ceaa --- /dev/null +++ b/completions/_trurl.zsh.in @@ -0,0 +1,66 @@ +#compdef trurl +########################################################################## +# _ _ +# Project | |_ _ __ _ _ _ __| | +# | __| '__| | | | '__| | +# | |_| | | |_| | | | | +# \__|_| \__,_|_| |_| +# +# Copyright (C) Daniel Stenberg, , et al. +# +# This software is licensed as described in the file COPYING, which +# you should have received as part of this distribution. The terms +# are also available at https://curl.se/docs/copyright.html. +# +# You may opt to use, copy, modify, merge, publish, distribute and/or sell +# copies of the Software, and permit persons to whom the Software is +# furnished to do so, under the terms of the COPYING file. +# +# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +# KIND, either express or implied. +# +# SPDX-License-Identifier: curl +# +########################################################################## + + +# 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[@]}" diff --git a/completions/generate_completions.sh b/completions/generate_completions.sh new file mode 100755 index 00000000..bd2170ae --- /dev/null +++ b/completions/generate_completions.sh @@ -0,0 +1,81 @@ +#!/bin/bash +########################################################################## +# _ _ +# Project | |_ _ __ _ _ _ __| | +# | __| '__| | | | '__| | +# | |_| | | |_| | | | | +# \__|_| \__,_|_| |_| +# +# Copyright (C) Daniel Stenberg, , et al. +# +# This software is licensed as described in the file COPYING, which +# you should have received as part of this distribution. The terms +# are also available at https://curl.se/docs/copyright.html. +# +# You may opt to use, copy, modify, merge, publish, distribute and/or sell +# copies of the Software, and permit persons to whom the Software is +# furnished to do so, under the terms of the COPYING file. +# +# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +# KIND, either express or implied. +# +# SPDX-License-Identifier: curl +# +########################################################################## + + +if [ -z "$1" ]; then + echo "expected a trurl.md file to be passed in..." + exit 1 +fi + +TRURL_MD_FILE=$1 + + + +ALL_FLAGS="$(sed -n \ + -e 's/"//g' \ + -e '/\# URL COMPONENTS/q;p' \ + < "${TRURL_MD_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_MD_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"