zsh-workers
 help / color / mirror / code / Atom feed
* Completion for pgrep and pkill
@ 2009-05-24  5:29 Michael Hwang
  2009-05-24  7:42 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Hwang @ 2009-05-24  5:29 UTC (permalink / raw)
  To: zsh-workers

[-- 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

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

* Re: Completion for pgrep and pkill
  2009-05-24  5:29 Completion for pgrep and pkill Michael Hwang
@ 2009-05-24  7:42 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2009-05-24  7:42 UTC (permalink / raw)
  To: Michael Hwang, zsh-workers

On May 23, 10:29pm, Michael Hwang wrote:
} 
} 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.

This is typically addressed by using zstyle for the "command" style to
allow the user to specify the program that will be used.  Look at the
_killall and _pids completers for examples of the use of _call_program,
and the documentation for the command style and for the processes and
process-names tags.

In this case you might have several contexts -- one for each of the ps
commands needed to produce the sid, ppid, pgid, etc. -- or you might
construct a single "ps" command that returns all the needed information
and then parse the output with zsh to extract the subset for each case.


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

end of thread, other threads:[~2009-05-24  7:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-24  5:29 Completion for pgrep and pkill Michael Hwang
2009-05-24  7:42 ` Bart Schaefer

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).