zsh-workers
 help / color / mirror / code / Atom feed
From: Michael Hwang <nomex45@yahoo.com>
To: zsh-workers@sunsite.dk
Subject: Completion for pgrep and pkill
Date: Sat, 23 May 2009 22:29:49 -0700 (PDT)	[thread overview]
Message-ID: <153994.78930.qm@web37305.mail.mud.yahoo.com> (raw)

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

I was surprised that this hadn't been written yet, so I used it as an opportunity to learn the completion system. A few things, though. The completion relies on ps, which I know takes arguments differently on different platforms. Without any cross-platform experience, I can't say whether or not the completion will work properly on a non-GNU system. Someone might need to go through and change it to be more portable.

Also, completions for IDs (group or user) are converted to their symbolic names if completed through menu selection. I'm not sure if this should be kept or not, as tab completion for a non-ambiguous numeric ID will no longer work. I suspect someone will want it removed.

Completion for pgrep's -d option (output delimiter) defaults to each character of IFS. If you tab through the completion menu, it displays space, null, tab, and carriage return properly as \  , $'\0', $'\t', and $'\r', but a newline is not $'\n' as expected. Instead, it is two single quotes with a newline in between. It's a minor annoyance to have the completion menu jump around while cycling through the list. The completion behaves even more strangely if you start with a quote.

Enjoy. :-)

Michael Hwang



      

[-- Attachment #2: _pgrep --]
[-- Type: application/octet-stream, Size: 2182 bytes --]

#compdef pgrep pkill 

local context state line
typeset -A opt_args
typeset -a arguments

arguments=('-P[parent process id]:parent process id:->ppid' 
	   '-g[match only in process group ids]:group:->pgid' 
	   '-G[match only real group id]:group:->group' 
	   '-s[match only session id]:session id:->sid' 
	   '-t[match only controlled by terminal]:terminal device:->tty'
	   '-u[match only effective user id]:user:->user' 
	   '-U[match only real user id]:user:->user' 
           '(-n)-o[oldest process]' 
	   '(-o)-n[newest process]' 
	   '-f[match against full command line]' 
	   '-v[negate matching]' 
	   '-x[match exactly]' 
	   '*:process name:->pname')

if [[ $service == 'pkill' ]]
then
	arguments+=('-'${^signals}'[signal]')
elif [[ $service == 'pgrep' ]]
then
	arguments+=('-d[output delimiter]:delimiter:compadd ${(s\:\:)IFS}'
		    '-l[list name in addition to id]')
fi

_arguments -s -w $arguments

case $state in
	(tty)
		compset -P '*,'

		local -a used
		used=(${(s:,:)IPREFIX})

		compadd -S ',' -q -F used /dev/tty*(:t)
		;;
		
	(sid)
		compset -P '*,'

		local -a used sid
		used=(${(s:,:)IPREFIX})
		sid=(${(uon)$(ps -A o sid=)})

		compadd -S ',' -q -F used $sid
		;;
	
	(ppid)
		compset -P '*,'

		local -a used ppid
		used=(${(s:,:)IPREFIX})
		ppid=(${(uon)$(ps -A o ppid=)})

		compadd -S ',' -q -F used $ppid
		;;

	(pgid)
		compset -P '*,'

		local -a used pgid
		used=(${(s:,:)IPREFIX})
		pgid=(${(uon)$(ps -A o pgid=)})

		compadd -S ',' -q -F used $pgid
		;;
	
	(pname)
		if (( ${+opt_args[-x]} )) && (( ${+opt_args[-f]} ))
		then
			compadd ${(u)${(f)"$(ps -A o cmd=)"}}
		else
			compadd ${(u)${(f)"$(ps -A co cmd=)"}}
		fi
		;;
	
	(group)
		compset -P '*,'

		local group
		group=$(getent group)

		local -a groups ids
		groups=(${${(f)group}%%:*})
		ids=(${${${(f)group}#*:*:}%%:*})

		local -a used
		used=(${(s:,:)IPREFIX})

		compadd -S ',' -q -F used -d ids $groups $groups
		;;

	(user)
		compset -P '*,'

		local passwd
		passwd=$(getent passwd)

		local -a users ids
		users=(${${(f)passwd}%%:*})
		ids=(${${${(f)passwd}#*:*:}%%:*})
		
		local -a used
		used=(${(s:,:)IPREFIX})

		compadd -S ',' -q -F used -d ids $users $users
		;;
esac

             reply	other threads:[~2009-05-24  5:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-24  5:29 Michael Hwang [this message]
2009-05-24  7:42 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=153994.78930.qm@web37305.mail.mud.yahoo.com \
    --to=nomex45@yahoo.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).