Question

J'ai une fonction comme ceci:

p() { cd ~/Clients/$1/Projects/$2; }

alors je peux taper:

p "Client here" "Project here"

Et cela me prend à:

~/Clients/Client here/Projects/Project here

rien de spécial en cours ici.Mais comment puis-je implémenter l'autocomplete pour cette fonction?J'ai réussi à obtenir un travail d'autocomplétion pour le premier argument (clients):

_p() { _files -W ~/Clients -/; }
compdef _p p

Mais comment puis-je apporter le deuxième argument (projets)?Il doit être autocompéqué du dossier basé sur le client:

~/Clients/$1/Projects

J'espère que quelqu'un peut aider!: -)

Était-ce utile?

La solution

Une personne intelligente (Mikachu) sur IRC a aidé:

p() { cd ~/Clients/$1/Projects/$2; }
_p() {
  _arguments '1: :->client' '2: :->project'
  case $state in
    client)
      _files -W ~/Clients
    ;;
    project)
      _files -W ~/Clients/$words[CURRENT-1]/Projects
    ;;
  esac 
}
compdef _p p

Mise à jour: Mots $ Mots [Courant-1] à $ {(q) mots [actuels-1]} pour le faire fonctionner avec des annuaires contenant des espaces:

p() { cd ~/Clients/$1/Projects/$2; }
_p() {
  _arguments '1: :->client' '2: :->project'
  case $state in
    client)
      _files -W ~/Clients
    ;;
    project)
      _files -W ~/Clients/${(Q)words[CURRENT-1]}/Projects
    ;;
  esac 
}
compdef _p p

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top