zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: "Zsh hackers list" <zsh-workers@zsh.org>
Subject: [PATCH] Improve _htop
Date: Fri, 06 May 2022 21:25:24 -0500	[thread overview]
Message-ID: <77cc388c-7acc-40ad-bd59-40b3d2bc4f29@www.fastmail.com> (raw)

[PATCH] Improve _htop

Someone submitted a PR on GitHub (#89) to fix htop's --version short-hand
amongst other things. There were a few trivial issues with the change as
well as some existing bugs/omissions that it didn't account for, so i said
i'd use the PR as a basis for a more elaborate improvement. Here's that
finally. With their permission i'll probably commit it in the original
author's name

Are we holding back completion improvements like this until after 5.9 is
released? (See also Marlon's patch from workers/49954)

Changes:

* Corrected -v to -V
* Made -u argument optional
* Enabled option stacking
* Improved descriptions (added ranges+defaults, &c.)
* Fixed broken sort keys with htop 3.x and added new key descriptions
* Removed _sequence limit on -p (it was incorrect on non-Linux systems,
  and making it correct seems like too much work for a limit that probably
  nobody will ever hit)
* Added several missing options

dana


diff --git a/Completion/Linux/Command/_htop b/Completion/Linux/Command/_htop
index 28c7512bf..6635181bc 100644
--- a/Completion/Linux/Command/_htop
+++ b/Completion/Linux/Command/_htop
@@ -1,11 +1,56 @@
-#compdef htop
-
-_arguments -S : \
-  '(-d --delay)'{-d+,--delay=}'[update frequency]:duration (tenths of seconds)' \
-  '(-C --no-color --no-colour)'{-C,--no-colo{,u}r}'[monochrome mode]' \
-  '(-)'{-h,--help}'[display usage information]' \
-  \*{-p+,--pid=}'[show given pids]: : _sequence -n ${$(</proc/sys/kernel/pid_max)\:-32768} _pids' \
-  '(-s --sort-key)'{-s+,--sort-key=}'[sort by key]:key:( ${(f)"$(_call_program sort-keys $words[1] --sort-key help)"} )' \
-  '(-t --tree)'{-t,--tree}'[show tree view of processes]' \
-  '(-u --user)'{-u+,--user=}'[show processes of user]: : _users' \
-  '(-)'{-v,--version}'[display version information]'
+#compdef htop pcp-htop
+
+# Notes:
+# - htop allows long options to be passed with a single dash; we don't account
+#   for this
+# - htop parses optional arguments to -H and -u 'cleverly' by allowing the next
+#   word to be the optarg if it doesn't begin with a '-'; we don't fully account
+#   for this
+# - There is a special version of htop designed to be used with PCP (Performance
+#   CoPilot); we don't fully account for this
+# - Some of the ranges and defaults listed here had to be found in the source
+
+local MATCH MBEGIN MEND ret=1
+local -a context line state state_descr args tmp
+
+args=(
+  '(-d --delay)'{-d+,--delay=}'[specify update frequency]:delay (tenths of seconds) (1-100) [15]'
+  '(-C --no-color --no-colour)'{-C,--no-colo{,u}r}'[use monochrome colour scheme]'
+  '(-F --filter)'{-F+,--filter=}'[show only commands matching specified filter]:case-insensitive command-line sub-string:_process_names -a'
+  '(-)'{-h,--help}'[display usage information]'
+  '(-H --highlight-changes)'{-H+,--highlight-changes=}'[highlight new and old processes (optionally specify delay)]::delay (seconds) (1-86400) [5]'
+  '(-M --no-mouse)'{-M,--no-mouse}'[disable mouse]'
+  \*{-p+,--pid=}'[show only specified PIDs]: : _sequence _pids'
+  '--readonly[disable all system and process changing features]'
+  '(-s --sort-key)'{-s+,--sort-key=}'[sort by specified column]: :->sort-keys'
+  '(-t --tree)'{-t,--tree}'[show tree view of processes]'
+  '(-u --user)'{-u+,--user=}'[show only processes of current or specified user]:: : _users'
+  '(-U --no-unicode)'{-U,--no-unicode}'[disable Unicode]'
+  '(-)'{-V,--version}'[display version information]'
+)
+
+[[ $OSTYPE == linux* ]] &&
+(( ! EUID || $+_comp_priv_prefix )) &&
+_pick_variant libcap=drop-capabilities $OSTYPE --help &&
+args+=(
+  '--drop-capabilities=-[drop specified capabilties]::mode [basic]:((
+    off\:"do not drop capabilities"
+    basic\:"drop capabilities not needed for standard functionality (retains kill, renice, etc.)"
+    strict\:"drop capabilities not needed for core functionality"
+  ))'
+)
+
+_arguments -s -S : $args && ret=0
+
+case $state in
+  sort-keys)
+    tmp=( ${(f)"$(_call_program sort-keys $words[1] --sort-key help)"} )
+    tmp=( ${tmp/#[[:space:]]##} )
+    tmp=( ${tmp//:/\\:} )
+    tmp=( ${tmp/[[:space:]]##/:} )
+    tmp=( ${tmp/(#m):[A-Z]/${(L)MATCH}} )
+    _describe -t sort-keys 'column (key)' tmp && ret=0
+    ;;
+esac
+
+return ret


             reply	other threads:[~2022-05-07  2:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-07  2:25 dana [this message]
2022-05-08  0:07 ` Bart Schaefer
2022-05-08  3:54   ` dana

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=77cc388c-7acc-40ad-bd59-40b3d2bc4f29@www.fastmail.com \
    --to=dana@dana.is \
    --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).