From 552d490db2f95e001bd287cacf0feeb451c27cbd Mon Sep 17 00:00:00 2001 From: FR19 Date: Tue, 24 Feb 2026 15:20:27 +0100 Subject: [PATCH] ZSH: Add 'console' (symfony based) autocomplete --- console.sh | 52 ++++++++++++++++++ zsh/autocomplete_symfony-console.zsh | 82 ++++++++++++++++++++++++++++ zsh/zshrc | 4 +- 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100755 console.sh create mode 100644 zsh/autocomplete_symfony-console.zsh diff --git a/console.sh b/console.sh new file mode 100755 index 0000000..ccb96fc --- /dev/null +++ b/console.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +dir=$(pwd) +dirname=$(basename ${dir}) + +# Nous sommes dans "bin" directement => cherche un exécutable "console" (le fameux "bin/console") +if [ "$dirname" = "bin" ] ; then + script="${dir}/console" + if [ ! -e "$script" -o ! -x "$script" ] ; then + script="" + fi +fi + +# Un sous-répertoire "bin" existe => cherche l'exécutable "bin/console" +if [ -z "$script" -a -d "bin" ] ; then + script="${dir}/bin/console" + if [ ! -e "$script" -o ! -x "$script" ] ; then + script="" + fi +fi + +# Cherche "programs.php" +if [ -z "$script" ] ; then + script="${dir}/programs.php" + if [ ! -e "$script" -o ! -r "$script" ] ; then + script="" + else + script="php $script" + fi +fi + +# Nous sommes dans un sous-répertoire de "public_html" => ré-invoque "console", mais dans le répertoire "public_html" +if [ -z "$script" ] ; then + if [[ "$dir" == *"/public_html/"* ]] ; then + while [ -n "$dir" -a ! "$dirname" = "public_html" ] ; do + dir=$(dirname ${dir}) + dirname=$(basename $dir) + done + + cd $dir + script="console" + fi +fi + +# +# Exécute le script si trouvé, sinon erreur +# +if [ -n "$script" ] ; then + $script "$@" +else + echo -e "\033[0;31mAucune console trouvée\033[0m" >&2 +fi diff --git a/zsh/autocomplete_symfony-console.zsh b/zsh/autocomplete_symfony-console.zsh new file mode 100644 index 0000000..ed7dbe6 --- /dev/null +++ b/zsh/autocomplete_symfony-console.zsh @@ -0,0 +1,82 @@ +#compdef console + +# This file is part of the Symfony package. +# +# (c) Fabien Potencier +# +# For the full copyright and license information, please view +# https://symfony.com/doc/current/contributing/code/license.html + +# +# zsh completions for console +# +# References: +# - https://github.com/spf13/cobra/blob/master/zsh_completions.go +# - https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Console/Resources/completion.bash +# +_sf_console() { + local lastParam flagPrefix requestComp out comp + local -a completions + + # The user could have moved the cursor backwards on the command-line. + # We need to trigger completion from the $CURRENT location, so we need + # to truncate the command-line ($words) up to the $CURRENT location. + # (We cannot use $CURSOR as its value does not work when a command is an alias.) + words=("${=words[1,CURRENT]}") lastParam=${words[-1]} + + # For zsh, when completing a flag with an = (e.g., console -n=) + # completions must be prefixed with the flag + setopt local_options BASH_REMATCH + if [[ "${lastParam}" =~ '-.*=' ]]; then + # We are dealing with a flag with an = + flagPrefix="-P ${BASH_REMATCH}" + fi + + # Prepare the command to obtain completions + requestComp="${words[0]} ${words[1]} _complete --no-interaction -szsh -a1 -c$((CURRENT-1))" i="" + for w in ${words[@]}; do + w=$(printf -- '%b' "$w") + # remove quotes from typed values + quote="${w:0:1}" + if [ "$quote" = \' ]; then + w="${w%\'}" + w="${w#\'}" + elif [ "$quote" = \" ]; then + w="${w%\"}" + w="${w#\"}" + fi + # empty values are ignored + if [ ! -z "$w" ]; then + i="${i}-i${w} " + fi + done + + # Ensure at least 1 input + if [ "${i}" = "" ]; then + requestComp="${requestComp} -i\" \"" + else + requestComp="${requestComp} ${i}" + fi + + # Use eval to handle any environment variables and such + out=$(eval ${requestComp} 2>/dev/null) + + while IFS='\n' read -r comp; do + if [ -n "$comp" ]; then + # If requested, completions are returned with a description. + # The description is preceded by a TAB character. + # For zsh's _describe, we need to use a : instead of a TAB. + # We first need to escape any : as part of the completion itself. + comp=${comp//:/\\:} + local tab=$(printf '\t') + comp=${comp//$tab/:} + completions+=${comp} + fi + done < <(printf "%s\n" "${out[@]}") + + # Let inbuilt _describe handle completions + eval _describe "completions" completions $flagPrefix + return $? +} + +compdef _sf_console console diff --git a/zsh/zshrc b/zsh/zshrc index 97c6774..87a1433 100644 --- a/zsh/zshrc +++ b/zsh/zshrc @@ -46,8 +46,8 @@ zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd' # Include other files source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh # Coloration syntaxique à la volée - -source ~/.zsh/ssh_agent # Pour que ssh-add marche sans se plaindre de ssh-agent +source ~/.zsh/ssh_agent # Pour que ssh-add marche sans se plaindre de ssh-agent +source ~/.zsh/autocomplete_symfony-console.zsh # Auto-complétion pour les commandes "console" (aka Symfony et "programs.php" des sites web # Fichiers de config perso à la machine for conf_file in ~/.zsh/config/* ;