zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] new completion for "sv"
@ 2014-10-05 20:01 fREW Schmidt
  2014-10-05 20:04 ` fREW Schmidt
  2014-10-06 19:37 ` Oliver Kiddle
  0 siblings, 2 replies; 7+ messages in thread
From: fREW Schmidt @ 2014-10-05 20:01 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1.1: Type: text/plain, Size: 260 bytes --]

See attached a patch to add completion for sv
(http://smarden.org/runit/sv.8.html)

If anything is wrong just let me know, this would be my first
contribution to zsh so mistakes wouldn't surprise me.
-- 
fREW Schmidt
https://blog.afoolishmanifesto.com

[-- Attachment #1.2: 0001-new-completion-for-sv.patch --]
[-- Type: text/plain, Size: 3002 bytes --]

From a9b5d69371034e276548a8044c7dc8a021180ffb Mon Sep 17 00:00:00 2001
From: Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
Date: Sun, 5 Oct 2014 14:52:42 -0500
Subject: [PATCH] new completion for "sv"

See http://smarden.org/runit/sv.8.html
---
 Completion/Unix/Command/_sv | 60 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 Completion/Unix/Command/_sv

diff --git a/Completion/Unix/Command/_sv b/Completion/Unix/Command/_sv
new file mode 100644
index 0000000..9a55be1
--- /dev/null
+++ b/Completion/Unix/Command/_sv
@@ -0,0 +1,60 @@
+#compdef sv
+
+_arguments \
+  '-v[verbose]' \
+  '-w:wait time' \
+  ':command:->command' \
+  '*::options:->options'
+
+case $state in
+  (command)
+    local -a sv_ary
+    sv_ary=(
+           'status':'Get status of service (and log service if available)'
+               'up':'start if service is running. If service stops, restart'
+             'down':'send SIGTERM and SIGCONT if service is running. After it stops, do not restart'
+             'once':'start if service is not running. Do not restart if it stops'
+            'pause':'send SIGSTOP if service is running'
+             'cont':'send SIGCONT if service is running'
+              'hup':'send SIGHUP if service is running'
+            'alarm':'send SIGALRM if service is running'
+        'interrupt':'send SIGINT if service is running'
+             'quit':'send SIGQUIT if service is running'
+                '1':'send SIGUSR1 if service is running'
+                '2':'send SIGUSR2 if service is running'
+             'term':'send SIGTERM if service is running'
+             'kill':'send SIGKILL if service is running'
+             'exit':'send SIGTERM and SIGCONT if service is running. Do not restart service.'
+      )
+
+      local -a sv_lsb_ary
+      sv_lsb_ary=(
+                  'start':'up with check/timeout'
+                   'stop':'down with check/timeout'
+                 'reload':'hup with check'
+                'restart':'down and up with check'
+               'shutdown':'exit with check/timeout'
+             'force-stop':'stop with kill on timeout'
+           'force-reload':'reload with kill on timeout'
+          'force-restart':'restart with kill on timeout'
+         'force-shutdown':'shutdown with kill on timeout'
+            'try-restart':'restart if service is already running'
+      )
+
+      _describe -t commands "sv commands" sv_ary -V sv_commands
+      _describe -t commands "sv LSM init compat" sv_lsb_ary -V sv_init_compat
+      _describe -t commands "sv additional commands" '("check:check status of service")' -V sv_addl_comm
+      return
+  ;;
+
+  (options)
+    local -a sv_services
+    sv_services=(
+      $SVDIR/*(N)
+      $SVDIR/*/log(N)
+    )
+
+    sv_services=( ${sv_services#$SVDIR/} )
+    _describe -t services "sv services" sv_services
+  ;;
+esac
-- 
1.9.3+fc2~572~g5a814b4


[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH] new completion for "sv"
  2014-10-05 20:01 [PATCH] new completion for "sv" fREW Schmidt
@ 2014-10-05 20:04 ` fREW Schmidt
  2014-10-06  9:54   ` Christian Neukirchen
  2014-10-06 19:37 ` Oliver Kiddle
  1 sibling, 1 reply; 7+ messages in thread
From: fREW Schmidt @ 2014-10-05 20:04 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 135 bytes --]

Also, I forgot to mention, please CC me on replies as I am not on the
list.

-- 
fREW Schmidt
https://blog.afoolishmanifesto.com

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH] new completion for "sv"
  2014-10-05 20:04 ` fREW Schmidt
@ 2014-10-06  9:54   ` Christian Neukirchen
  0 siblings, 0 replies; 7+ messages in thread
From: Christian Neukirchen @ 2014-10-06  9:54 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 450 bytes --]

fREW Schmidt <frioux@gmail.com> writes:

> Also, I forgot to mention, please CC me on replies as I am not on the
> list.

For comparising, I attached the version I wrote for myself.

I think only the first command should parse as a command.
Also I support first-char completion, perhaps that could be merged.

I'm not sure where $SVDIR is from, perhaps it should have a default
value.  Also, I complete any service directory if it contains a slash.


[-- Attachment #2: _sv --]
[-- Type: application/octet-stream, Size: 972 bytes --]

#compdef sv

# completion for runit sv(1)
# Christian Neukirchen <chneukirchen@gmail.com>
# runit 2.1.1, services in /var/service are completed by default

local ret=1 services

_arguments \
    '-v[verbose]' \
    '-w[wait]:seconds' \
    '1: :->cmds' \
    '*:: :->args' && ret=0

case $state in
cmds)
    _values "sv command" \
        status \
        up \
        down \
        once \
        pause cont hup alarm interrupt quit 1 2 term kill \
        exit
    # above allow one-character shortcuts, complete these
    [[ $words[CURRENT] = [sudopchaiq12tke] ]] ||
    _values "sv command" \
        start \
        stop \
        restart \
        shutdown \
        force-stop \
        force-reload \
        force-restart \
        force-shutdown \
        check
    ret=0;;
args)
    services=( /var/service/*(-/N:t) )
    (( $#services )) && _values services $services && ret=0
    [[ $words[CURRENT] = */* ]] && _directories && ret=0
    ;;
esac

return $ret

[-- Attachment #3: Type: text/plain, Size: 77 bytes --]


-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org

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

* Re: [PATCH] new completion for "sv"
  2014-10-05 20:01 [PATCH] new completion for "sv" fREW Schmidt
  2014-10-05 20:04 ` fREW Schmidt
@ 2014-10-06 19:37 ` Oliver Kiddle
  2014-10-07 13:20   ` fREW Schmidt
  1 sibling, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2014-10-06 19:37 UTC (permalink / raw)
  To: fREW Schmidt; +Cc: zsh-workers

fREW Schmidt wrote:
> 
> If anything is wrong just let me know, this would be my first
> contribution to zsh so mistakes wouldn't surprise me.

I've got a few comments.

> +#compdef sv
> +
> +_arguments \
> +  '-v[verbose]' \
> +  '-w:wait time' \

What's the units for the time? How about something like:
  '-w[specify wait time]:time (seconds)'

> +  ':command:->command' \
> +  '*::options:->options'

If you use states with _arguments, you should declare a few array
variables local: context, state and line. You then need to take care to
make use of the context variable when calling other functions. In most
cases, including the one above, only one state is possible at a time. So
in this case, you should declare:
  local curcontext="$curcontext" state line
and pass the -C option to _arguments.

> +    sv_ary=(
> +           'status':'Get status of service (and log service if available)'

The usual convention, which you have done elsewhere, is to start
descriptions with a lowercase letter.

> +      _describe -t commands "sv commands" sv_ary -V sv_commands
> +      _describe -t commands "sv LSM init compat" sv_lsb_ary -V sv_init_compat
> +      _describe -t commands "sv additional commands" '("check:check status of service")' -V sv_addl_comm
> +      return

This is ignoring the return status of the first two calls to _describe.
The return value of a completion function determines whether the
completion system carries on trying to find more matches, perhaps
approximate matching if you have that configured. It's common to use a
variable named ret for the return status: look for some examples.

Perhaps also consider using three different tags for the matches and using
_alternative or _tags with a loop.

> +      $SVDIR/*(N)

Is SVDIR really always set or is there a default value that it could
fall back on, e.g: ${SVDIR:-/service}

Oliver


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

* Re: [PATCH] new completion for "sv"
  2014-10-06 19:37 ` Oliver Kiddle
@ 2014-10-07 13:20   ` fREW Schmidt
  2014-10-07 15:28     ` Oliver Kiddle
  0 siblings, 1 reply; 7+ messages in thread
From: fREW Schmidt @ 2014-10-07 13:20 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-workers


[-- Attachment #1.1: Type: text/plain, Size: 3018 bytes --]

On Mon, Oct 06, 2014 at 09:37:20PM +0200, Oliver Kiddle wrote:
> fREW Schmidt wrote:
> > 
> > If anything is wrong just let me know, this would be my first
> > contribution to zsh so mistakes wouldn't surprise me.
> 
> I've got a few comments.
> 
> > +#compdef sv
> > +
> > +_arguments \
> > +  '-v[verbose]' \
> > +  '-w:wait time' \
> 
> What's the units for the time? How about something like:
>   '-w[specify wait time]:time (seconds)'

Good idea

> > +  ':command:->command' \
> > +  '*::options:->options'
> 
> If you use states with _arguments, you should declare a few array
> variables local: context, state and line. You then need to take care to
> make use of the context variable when calling other functions. In most
> cases, including the one above, only one state is possible at a time. So
> in this case, you should declare:
>   local curcontext="$curcontext" state line
> and pass the -C option to _arguments.

Ok, I think I did this right this time.

> > +    sv_ary=(
> > +           'status':'Get status of service (and log service if available)'
> 
> The usual convention, which you have done elsewhere, is to start
> descriptions with a lowercase letter.

Woops! Fixed.

> > +      _describe -t commands "sv commands" sv_ary -V sv_commands
> > +      _describe -t commands "sv LSM init compat" sv_lsb_ary -V sv_init_compat
> > +      _describe -t commands "sv additional commands" '("check:check status of service")' -V sv_addl_comm
> > +      return
> 
> This is ignoring the return status of the first two calls to _describe.
> The return value of a completion function determines whether the
> completion system carries on trying to find more matches, perhaps
> approximate matching if you have that configured. It's common to use a
> variable named ret for the return status: look for some examples.
> 
> Perhaps also consider using three different tags for the matches and using
> _alternative or _tags with a loop.

ok, I switched it to use _alternative and 3 functions as seems to be
somewhat common in other compltion scripts in the codebase.

> > +      $SVDIR/*(N)
> 
> Is SVDIR really always set or is there a default value that it could
> fall back on, e.g: ${SVDIR:-/service}

This raises an interesting question.  I did what you said, but as
Christian Neukirchen pointed out, on his system the default SVDIR is
/var/service, and on ubuntu the default is /etc/service.  I guess it's
up to the packager to tweak the script or something?

Additionally, I like Christian Neukirchen's single-character shortcut
completion.  For majority of the commands they work already since
nothing else starts with that letter, but s and c both have multiple
options.  If I understood what I was doing more I'd take his idea, but
I don't.

Anyway, see a new version attached.  Let me know what else is (still?)
wrong.

Thanks!

(Again, please CC me in responses.)

-- 
fREW Schmidt
https://blog.afoolishmanifesto.com

[-- Attachment #1.2: 0001-new-completion-for-sv.patch --]
[-- Type: text/plain, Size: 3231 bytes --]

From 05f0fab3a7ddc666c8c0b89fd48b8c05f41b7b52 Mon Sep 17 00:00:00 2001
From: Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
Date: Sun, 5 Oct 2014 14:52:42 -0500
Subject: [PATCH] new completion for "sv"

See http://smarden.org/runit/sv.8.html
---
 Completion/Unix/Command/_sv | 73 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 Completion/Unix/Command/_sv

diff --git a/Completion/Unix/Command/_sv b/Completion/Unix/Command/_sv
new file mode 100644
index 0000000..b2fedc5
--- /dev/null
+++ b/Completion/Unix/Command/_sv
@@ -0,0 +1,73 @@
+#compdef sv
+
+_sv_commands() {
+    local -a sv_ary
+    sv_ary=(
+          'status':'get status of service (and log service if available)'
+              'up':'start if service is running. If service stops, restart'
+            'down':'send SIGTERM and SIGCONT if service is running. After it stops, do not restart'
+            'once':'start if service is not running. Do not restart if it stops'
+           'pause':'send SIGSTOP if service is running'
+            'cont':'send SIGCONT if service is running'
+             'hup':'send SIGHUP if service is running'
+           'alarm':'send SIGALRM if service is running'
+       'interrupt':'send SIGINT if service is running'
+            'quit':'send SIGQUIT if service is running'
+               '1':'send SIGUSR1 if service is running'
+               '2':'send SIGUSR2 if service is running'
+            'term':'send SIGTERM if service is running'
+            'kill':'send SIGKILL if service is running'
+            'exit':'send SIGTERM and SIGCONT if service is running. Do not restart service.'
+    )
+    _describe -t commands "sv commands" sv_ary -V sv_commands
+}
+
+_sv_lsb() {
+    local -a sv_lsb_ary
+    sv_lsb_ary=(
+                'start':'up with check/timeout'
+                 'stop':'down with check/timeout'
+               'reload':'hup with check'
+              'restart':'down and up with check'
+             'shutdown':'exit with check/timeout'
+           'force-stop':'stop with kill on timeout'
+         'force-reload':'reload with kill on timeout'
+        'force-restart':'restart with kill on timeout'
+       'force-shutdown':'shutdown with kill on timeout'
+          'try-restart':'restart if service is already running'
+    )
+
+    _describe -t lsb-commands "sv LSM init compat" sv_lsb_ary -V sv_init_compat
+}
+
+_sv_additional() {
+    _describe -t additional-commands "sv additional commands" '("check:check status of service")' -V sv_addl_comm
+}
+
+local curcontext="$curcontext" state line
+_arguments -C \
+    '-v[verbose]' \
+    '-w[wait time]:time (seconds)' \
+    '1: :->command' \
+    '*:: :->options'
+
+case $state in
+  (command)
+      _alternative \
+         _sv_commands \
+         _sv_lsb \
+         _sv_additional
+  ;;
+
+  (options)
+      local -a sv_services
+      local svdir=${SVDIR:-/service}
+      sv_services=(
+         $svdir/*(N)
+         $svdir/*/log(N)
+      )
+
+      sv_services=( ${sv_services#$svdir/} )
+     _describe -t services "sv services" sv_services
+  ;;
+esac
-- 
2.0.0.390.gcb682f8


[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH] new completion for "sv"
  2014-10-07 13:20   ` fREW Schmidt
@ 2014-10-07 15:28     ` Oliver Kiddle
  2014-10-07 16:03       ` fREW Schmidt
  0 siblings, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2014-10-07 15:28 UTC (permalink / raw)
  To: fREW Schmidt; +Cc: zsh-workers

fREW Schmidt wrote:
> Ok, I think I did this right this time.

Looks good.

> ok, I switched it to use _alternative and 3 functions as seems to be
> somewhat common in other compltion scripts in the codebase.

At least based on the documentation, _alternative is supposed to take
_arguments like specifications. Apparently it works with a list of
functions as you have done. An example would be:

_alternative \
  'additional-commands:additional command:((check\:check\ status\ of\ service))' \
  'lsb-commands: : _describe -t lsb-commands "LSB command" sv_lsb_ary' \
  'sv-commands:sv command:_sv_commands'

> This raises an interesting question.  I did what you said, but as
> Christian Neukirchen pointed out, on his system the default SVDIR is
> /var/service, and on ubuntu the default is /etc/service.  I guess it's
> up to the packager to tweak the script or something?

I guess it could try to find out what distribution you have by checking
for release files in /etc and choosing a suitable default accordingly.

> Additionally, I like Christian Neukirchen's single-character shortcut
> completion.  For majority of the commands they work already since
> nothing else starts with that letter, but s and c both have multiple
> options.  If I understood what I was doing more I'd take his idea, but
> I don't.

I'm not sure that I do like it because it is a departure from the way
completion normally works: "s" will complete to status but not
start or stop while "st" will offer all three. It is possible to
configure this using tag-order and ignored-patterns styles.

>  Completion/Unix/Command/_sv | 73 +++++++++++++++++++++++++++++++++++++++++++++

One thing I forgot to mention is that it should be named _runit:
functions are normally named after the software rather than the command,
e.g. _subversion for svn.

Oliver


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

* Re: [PATCH] new completion for "sv"
  2014-10-07 15:28     ` Oliver Kiddle
@ 2014-10-07 16:03       ` fREW Schmidt
  0 siblings, 0 replies; 7+ messages in thread
From: fREW Schmidt @ 2014-10-07 16:03 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1650 bytes --]

On Tue, Oct 07, 2014 at 05:28:52PM +0200, Oliver Kiddle wrote:

[snip]

> 
> At least based on the documentation, _alternative is supposed to take
> _arguments like specifications. Apparently it works with a list of
> functions as you have done. An example would be:
> 
> _alternative \
>   'additional-commands:additional command:((check\:check\ status\ of\ service))' \
>   'lsb-commands: : _describe -t lsb-commands "LSB command" sv_lsb_ary' \
>   'sv-commands:sv command:_sv_commands'

So when I tested this it made me realize something.  The way I had it
initially was so that the commands would come first, and then the lsb
commands would be listed, and then finally the additional command.  I
tried passing _alternative -V foo like you can with _describe to
preserve order byt I couldn't get that to work.  Any ideas?

> > This raises an interesting question.  I did what you said, but as
> > Christian Neukirchen pointed out, on his system the default SVDIR is
> > /var/service, and on ubuntu the default is /etc/service.  I guess it's
> > up to the packager to tweak the script or something?
> 
> I guess it could try to find out what distribution you have by checking
> for release files in /etc and choosing a suitable default accordingly.
> 

I'll look into that.

[snip]

> >  Completion/Unix/Command/_sv | 73 +++++++++++++++++++++++++++++++++++++++++++++
> 
> One thing I forgot to mention is that it should be named _runit:
> functions are normally named after the software rather than the command,
> e.g. _subversion for svn.
> 

Done.

-- 
fREW Schmidt
https://blog.afoolishmanifesto.com

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

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

end of thread, other threads:[~2014-10-07 16:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-05 20:01 [PATCH] new completion for "sv" fREW Schmidt
2014-10-05 20:04 ` fREW Schmidt
2014-10-06  9:54   ` Christian Neukirchen
2014-10-06 19:37 ` Oliver Kiddle
2014-10-07 13:20   ` fREW Schmidt
2014-10-07 15:28     ` Oliver Kiddle
2014-10-07 16:03       ` fREW Schmidt

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).