Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] new function to complete names of running process (and three example usage)



_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




Messages sorted by: Reverse Date, Date, Thread, Author