#compdef htoprs

# htoprs — a faithful Rust port of htop. The options below mirror the getopt
# table in src/ported/commandline.rs (i.e. `htoprs --help`).
#
# Notes:
# - htoprs, like htop, accepts long options with a single dash; not modeled here.
# - The optional-argument options (-H, -t, -u) also grab the next word as their
#   optarg when it does not begin with '-', matching htop's parsing.
# - htoprs does NOT implement htop's Linux-only --drop-capabilities.
# - Some ranges/defaults below come from htop's documented behaviour.

local MATCH MBEGIN MEND ret=1
local -a context line state state_descr args tmp

args=(
  '(-C --no-color --no-colour)'{-C,--no-colo{,u}r}'[use a monochrome color scheme]'
  '(-d --delay)'{-d+,--delay=}'[set the delay between updates, in tenths of seconds]:delay (tenths of seconds) (1-100) [15]'
  '(-F --filter)'{-F+,--filter=}'[show only the commands matching the given filter]:case-insensitive command-line sub-string:_process_names -a'
  '(--no-function-bar --no-functionbar)'--no-function{-,}bar'[hide the function bar]'
  '(-)'{-h,--help}'[print the help screen]'
  '(-H --highlight-changes)'{-H+,--highlight-changes=}'[highlight new and old processes]::delay (seconds) (1-86400) [5]'
  '(-M --no-mouse)'{-M,--no-mouse}'[disable the mouse]'
  '--no-meters[hide the meters (header)]'
  '(-n --max-iterations)'{-n+,--max-iterations=}'[exit after the given number of frame updates]:iterations (positive integer)'
  \*{-p+,--pid=}'[show only the given PIDs]: : _sequence _pids'
  '--readonly[disable all system and process changing features]'
  '(-s --sort-key)'{-s+,--sort-key=}'[sort the process list by the given column]: :->sort-keys'
  '(-t --tree)'{-t-,--tree=-}'[show the tree view (can be combined with -s)]:: :->sort-keys'
  '(-u --user)'{-u+,--user=}'[show only processes for a given user (or $USER)]:: : _users'
  '(-U --no-unicode)'{-U,--no-unicode}'[do not use unicode but plain ASCII]'
  '(-)'{-V,--version}'[print version information]'
)

_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
