From: Jun T <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: [PATCH] new function to complete names of running process (and three example usage)
Date: Wed, 20 Jun 2018 21:30:25 +0900 [thread overview]
Message-ID: <40B3D22E-DAC5-4FC9-BFA6-2E9CEDA4AAA2@kba.biglobe.ne.jp> (raw)
_process_names completes names of running processes.
I wrote this for using it from three new completions for macOS-specific commands
dtruss, fs_usage and sc_usage (also included in the patch).
_process_names is tested only on macOS, Linux and {Free,Net,Open}BSD.
Please test on other OS (Solaris?) if possible.
diff --git a/Completion/Darwin/Command/_dtruss b/Completion/Darwin/Command/_dtruss
new file mode 100644
index 000000000..bd1ae8bc5
--- /dev/null
+++ b/Completion/Darwin/Command/_dtruss
@@ -0,0 +1,18 @@
+#compdef dtruss
+
+_arguments -s : \
+ '-a[print all details]' \
+ '-b+[specify dynamic variable buffer size]:buffer size (default 4m)' \
+ '-c[print system call counts]' \
+ '-d[print relative timestamps]' \
+ '-e[print elapsed times]' \
+ '-f[follow children as they are forked]' \
+ '-l[force printing of pid/lwpid per line]' \
+ "-L[don't print pid/lwpid per line]" \
+ '(-p :)-n+[examine processes with the specified name]: : _process_names -a' \
+ '-o[print on-cpu times]' \
+ '-s[print stack backtraces]' \
+ '(-n :)-p+[examine process with the specified pid]: : _pids' \
+ '-t+[examine only the specified syscall]: : _sys_calls' \
+ '1: : _command_names -e' \
+ '*:: : _normal'
diff --git a/Completion/Darwin/Command/_fs_usage b/Completion/Darwin/Command/_fs_usage
new file mode 100644
index 000000000..956816169
--- /dev/null
+++ b/Completion/Darwin/Command/_fs_usage
@@ -0,0 +1,28 @@
+#compdef fs_usage
+
+local curcontext="$curcontext" state state_descr line ret=1
+typeset -A opt_args
+
+_arguments -s -C -A '-*' : \
+ '-e[exclude fs_usage and the specified processes from sampling]' \
+ '-w[use wider output]' \
+ '*-f+[specify output filtering mode]:mode:(nework filesys pathname exec diskio cachehit)' \
+ '-b[annotate disk I/O events with BootCache info]' \
+ '(-R -S -E)-t+[specify run timeout]:seconds' \
+ '(-t)-R+[specify raw trace file to process]:raw trace file:_files' \
+ '(-t)-S+[specify time to begin processing the trace file]:seconds' \
+ '(-t)-E+[specify time to stop processing the trace file]:seconds' \
+ '*: :->pid-or-pname' && ret=0
+
+case $state in
+ (pid-or-pname)
+ if [[ -z $opt_args[-R] ]]; then
+ _alternative "processes:: _pids" \
+ "processes-names:: _process_names -a" && ret=0
+ else
+ _message 'pid or process name in the trace file' && ret=0
+ fi
+ ;;
+esac
+
+return ret
diff --git a/Completion/Darwin/Command/_sc_usage b/Completion/Darwin/Command/_sc_usage
new file mode 100644
index 000000000..3a11a1bff
--- /dev/null
+++ b/Completion/Darwin/Command/_sc_usage
@@ -0,0 +1,10 @@
+#compdef sc_usage
+
+_arguments -s -A '-*' : \
+ '-c+[specify code file to use]:code file:_files' \
+ '-e[sort output by call count]' \
+ '-l[use scrolling output style instead of window updating style]' \
+ '-s+[specify sampling interval]:seconds' \
+ '(- :)-E[specify command path and args to excute]: :_absolute_command_paths:*:: :_normal' \
+ '1: : _alternative "processes:: _pids"
+ "processes-names:: _process_names -a"'
diff --git a/Completion/Unix/Type/_process_names b/Completion/Unix/Type/_process_names
new file mode 100644
index 000000000..ef3a95379
--- /dev/null
+++ b/Completion/Unix/Type/_process_names
@@ -0,0 +1,30 @@
+#autoload
+#
+# completes names of running processes
+#
+# option -a: include all processes (owned by others, no tty, etc.)
+#
+typeset -a expl names all
+local opts tagname
+tagname='processes-names' # or 'process-names' ?
+
+zparseopts -E -D 'a=all'
+(( $#all )) && opts='ax'
+
+case $OSTYPE in
+ (linux*|*bsd*|dragonfly*)
+ # On these systems, 'ps o comm' (or 'ps co args') truncates long process
+ # names. To avoid this, we get the whole command line by 'ps o args' and
+ # remove arguments, but this does not work if proess names contain spaces.
+ opts+='o args='
+ names=( ${(f)"$(_call_program $tagname ps $opts 2>/dev/null )"} )
+ names=( ${${${${${names:#\[*]}%% *}%:}#-}:t} ${${${(M)names:#\[*]}#\[}%]} )
+ ;;
+ (*)
+ # tested only on macOS
+ opts+='o comm='
+ names=( ${${${(f)"$(_call_program $tagname ps $opts 2>/dev/null)"}#-}:t} )
+ ;;
+esac
+
+_wanted $tagname expl 'process name' compadd "$@" -F '(ps)' -a - names
next reply other threads:[~2018-06-20 13:14 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-20 12:30 Jun T [this message]
2018-06-21 2:23 ` dana
2018-06-21 11:41 ` Jun T
2018-06-21 13:46 ` dana
2018-06-22 12:11 ` Jun T
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=40B3D22E-DAC5-4FC9-BFA6-2E9CEDA4AAA2@kba.biglobe.ne.jp \
--to=takimoto-j@kba.biglobe.ne.jp \
--cc=zsh-workers@zsh.org \
/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).