zsh-users
 help / Atom feed
* completion for function arguments?
@ 2019-08-08 13:06 TJ Luoma
  2019-08-08 13:49 ` Jérémie Roquet
  2019-08-08 15:42 ` Mikael Magnusson
  0 siblings, 2 replies; 3+ messages in thread
From: TJ Luoma @ 2019-08-08 13:06 UTC (permalink / raw)
  To: Zsh MailingList

Let me start by saying that I don't understand completion at all, so
this may be a) simple or b) impossible or c) have to be done a
completely different way.

I have a function called 'jump' which I will include below.

The function takes one (1) argument from a specific list of arguments.
I have defined these arguments as part of the function, although they
don't have to be defined that way if there's another way of doing it
that would work better for completion.

Here's what the function looks like now, and then I'll continue my
question below:

## Begin
JDIR="$HOME/Library/Mobile
Documents/2HCKV38EEC~com~p5sys~jump~servers/Documents/Servers"

for ARGS in "$@"
do
     case "$ARGS" in
          m1|macbook)

                    'Computer - m1 - 6F333BF4-CF2B-4E98-8281-A79667878D40.jump'
          ;;

          imac)

                    FILE='Computer - iMac -
4D391A42-B26E-460E-BDDD-88C9CCA882CF.jump'
          ;;

          tyrion)

                    FILE='Computer - Tyrion -
2709D3BB-41BE-4410-8606-6B4DEB92F104.jump'
          ;;

          mba|macbookair|air)

                    FILE='Computer - MBA -
4690FEF3-F057-4529-998A-9349F616C1FE.jump'
          ;;


          mini|mmcolo|macminicolo)

                    FILE='Computer - MacMiniColo -
13033BED-C6CA-4A5E-970E-47D7869EE09E.jump'
          ;;

          *)
                    echo "     [jump]: Fatal Error! Don't know what to
do with arg: $1"
                    return 1
          ;;

     esac

done # for args

if [[ -f "$JDIR/$FILE" ]]
then

     open -a 'Jump Desktop' "$JDIR/$FILE"

else

     echo "[jump] No file found at '$JDIR/$FILE'."
     return 1
fi

# End

As you can see, these files are obscurely named in an obscure folder
on my Mac, but I _could_ create links to all of them with the
preferred names, such as

~/jump/m1
~/jump/mini
~/jump/mba
~/jump/tyrion
~/jump/imac

and then all the function would need to do is open the file that
matches the argument.

I'd like to be able to type "jump [tab]" and have all of the options
appear (m1, mini, mba, tyrion, imac) or type "jump m[tab]" and have
just the ones that start with 'm' appear, etc. I think that's all
fairly standard completion stuff.

Any help would be appreciated.

Also, anyone has or knows of a "beginners' step-by-step guide to
understanding completion" please let me know.

Thanks, as always!

TjL

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: completion for function arguments?
  2019-08-08 13:06 completion for function arguments? TJ Luoma
@ 2019-08-08 13:49 ` Jérémie Roquet
  2019-08-08 15:42 ` Mikael Magnusson
  1 sibling, 0 replies; 3+ messages in thread
From: Jérémie Roquet @ 2019-08-08 13:49 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh MailingList

Le jeu. 8 août 2019 à 15:08, TJ Luoma <luomat@gmail.com> a écrit :
> Let me start by saying that I don't understand completion at all,

So, let's start by saying that I don't understand them much either,
and that someone will probably suggest something much simpler using
zstyle.

> I have a function called 'jump' which I will include below.
> The function takes one (1) argument from a specific list of arguments.

The following should work:

_jump() {
  local targets=(macbook imac tyrion)
  _wanted jump_target expl 'Jump target' compadd -a targets
}
compdef  _jump jump

Note that you can achieve the same effect by just putting the
following in a file named _jump somewhere in your $fpath:

#compdef jump
local targets=(macbook imac tyrion)
_wanted jump_target expl 'Jump target' compadd -a targets

As I said, it should work, but there are probably much nicer ways to
achieve the same result.

Best regards,

-- 
Jérémie

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: completion for function arguments?
  2019-08-08 13:06 completion for function arguments? TJ Luoma
  2019-08-08 13:49 ` Jérémie Roquet
@ 2019-08-08 15:42 ` Mikael Magnusson
  1 sibling, 0 replies; 3+ messages in thread
From: Mikael Magnusson @ 2019-08-08 15:42 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh MailingList

On 8/8/19, TJ Luoma <luomat@gmail.com> wrote:
> Let me start by saying that I don't understand completion at all, so
> this may be a) simple or b) impossible or c) have to be done a
> completely different way.
>
> I have a function called 'jump' which I will include below.
>
> The function takes one (1) argument from a specific list of arguments.
> I have defined these arguments as part of the function, although they
> don't have to be defined that way if there's another way of doing it
> that would work better for completion.
[snip]
> As you can see, these files are obscurely named in an obscure folder
> on my Mac, but I _could_ create links to all of them with the
> preferred names, such as
>
> ~/jump/m1
> ~/jump/mini
> ~/jump/mba
> ~/jump/tyrion
> ~/jump/imac
>
> and then all the function would need to do is open the file that
> matches the argument.
>
> I'd like to be able to type "jump [tab]" and have all of the options
> appear (m1, mini, mba, tyrion, imac) or type "jump m[tab]" and have
> just the ones that start with 'm' appear, etc. I think that's all
> fairly standard completion stuff.
>
> Any help would be appreciated.

For a very quick and dirty solution, dump this in your .zshrc
compdef -e '() { local expl; _wanted jump-targets expl "jump target"
compadd m1 mini mba tyrion imac }' jump

The next step up is to create a file called _jump, put it in your
$fpath and ensure fpath is set correctly before calling compinit, and
then the contents of that file would be:
#compdef jump
local expl
_wanted jump-targets expl "jump target" compadd m1 mini mba tyrion imac

And of course from here you can get more fancy with generating the
list of targets.

For any more complex handling, ie if jump were to support some
--options and multiple arguments, you would probably want to start
using _arguments instead.

-- 
Mikael Magnusson

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-08 13:06 completion for function arguments? TJ Luoma
2019-08-08 13:49 ` Jérémie Roquet
2019-08-08 15:42 ` Mikael Magnusson

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/ public-inbox