<commit_before>#compdef hk

# hk Autocomplete plugin for Oh-My-Zsh. To install it, drop this plugin into a
# file called `_hk` within /usr/local/share/zsh/site-functions or another
# directory in your $fpath.
#
# You'll also need to enable compinit. Oh-my-zsh does this for you by default:
# https://github.com/robbyrussell/oh-my-zsh/blob/master/oh-my-zsh.sh#L51-L53
#
# Finally, completion of arguments like app names works best if completion
# caching is enabled. Oh-my-zsh also does this for you:
# https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/completion.zsh#L34-L36
#
# Requires: The hk Heroku client (https://hk.heroku.com)
# Author: <NAME> (https://bgentry.io)

_hk_is_default_cloud() {
  ( [[ -z $HEROKU_API_URL ]] || [[ "https://api.heroku.com" == $HEROKU_API_URL ]] )
}

_hkcmdnames() {
  print -l ${(f)"$(hk help commands | cut -f 2 -d ' ')"}
}

_hkcmdnames_needing_app() {
  print -l ${(f)"$(hk help commands | grep -F '[-a <app>]' | cut -f 2 -d ' ')"}
}

_hkrawcmds() {
  print -l ${(f)"$(hk help commands)"}
}

__args_for_command() {
  for i in _hkrawcmds; do
    if [[ "${${=i}[2]}" == $line[1] ]]; then
      local arglist; arglist=()
      #for j in (${^${i#*${line}[1] }}); do
      # print -l ${=${i#*${${line}[1]} }}
      for j in ${=${${i#*${${line}[1]} }%*\#*}}; do
        arglist+=($j)
      done
      _describe -t opts 'opts' arglist && ret=0

      # _arguments \
      #   '-a:appname'
      # ret=0
      return
    fi
  done
}

###########################################################
##       Functions to get data from hk and cache it      ##
###########################################################

__hk_complete_addon_service_and_plan() {
  local -a suf
  if compset -P '*:'; then
    __hk_addon_plans ${IPREFIX%%\:*} # strip : from $IPREFIX to get provider
  else
    __hk_addon_services
  fi
}

__hk_addon_services() {
  # set a local curcontext to use for caching
  local curcontext=${curcontext%:*:*}:hk-__hk_addon_services: state line cache_policy ret=1
  local cache_name=":completion:${curcontext}:"

  # See if a cache-policy is already set up, and set one if not
  zstyle -s $cache_name cache-policy cache_policy
  [[ -z "$cache_policy" ]] && zstyle $cache_name cache-policy _hk_addon_services_caching_policy

  # If _addon_services isn't populated or the cache is invalid, and we fail to
  # retrieve the cache:
  if ( ((${#_addon_services} == 0)) || _cache_invalid $cache_name ) \
    && ! _retrieve_cache $cache_name; then
    # If we've gotten to this point, the app names aren't cached. Fetch them.
    _addon_services=(${(f)"$(hk addon-services)"})
    # Store _addon_services in the cache if this is a default cloud
    ( _hk_is_default_cloud ) && _store_cache $cache_name _addon_services
  fi

  # the -S ':' gives us a : suffix after completion
  compadd -S ':' $* - $_addon_services
  # don't let this var persist in non-default clouds
  ( ! _hk_is_default_cloud ) && unset _addon_services
}

_hk_addon_services_caching_policy() {
  local -a oldp
  if ( ! _hk_is_default_cloud ); then
    return 0 # don't cache data in non-default clouds
  fi
  # Rebuild if cache is older than 1 week
  oldp=( "$1"(Nmw+1) )
  (( $#oldp ))
}

__hk_addon_plans() {
  # set a local curcontext to use for caching. Add service provider name ($1)
  # as a a suffix for caching.
  service=$1
  local curcontext=${curcontext%:*:*}:hk-__hk_addon_plans_$service: state line cache_policy ret=1
  local cache_name=":completion:${curcontext}:"
  local varname="_addon_plans_${service//-/_}" # replace - in service name w/ _
  plans_for_service=${(P)varname}

  # See if a cache-policy is already set up, and set one if not
  zstyle -s $cache_name cache-policy cache_policy
  [[ -z "$cache_policy" ]] && zstyle $cache_name cache-policy _hk_addon_plans_caching_policy

  # If $varname isn't populated or the cache is invalid, and we fail to retrieve
  # the cache:
  if ( ((${#plans_for_service} == 0)) || _cache_invalid $cache_name ) \
    && ! _retrieve_cache $cache_name; then
    # If we've gotten to this point, the plan names aren't cached. Fetch them.
    plans_for_service=(${(f)"$(hk addon-plans $service | cut -f 1 -d ' ')"})
    # Store _addon_plans in the cache if this is a default cloud
    ( _hk_is_default_cloud ) && _store_cache $cache_name plans_for_service
  fi

  compadd $* $plans_for_service
  # don't let this var persist in non-default clouds
  ( ! _hk_is_default_cloud ) && unset $varname
}

_hk_addon_plans_caching_policy() {
  local -a oldp<commit_msg>remove accidental service name from plan name completion list<commit_after>#compdef hk

# hk Autocomplete plugin for Oh-My-Zsh. To install it, drop this plugin into a
# file called `_hk` within /usr/local/share/zsh/site-functions or another
# directory in your $fpath.
#
# You'll also need to enable compinit. Oh-my-zsh does this for you by default:
# https://github.com/robbyrussell/oh-my-zsh/blob/master/oh-my-zsh.sh#L51-L53
#
# Finally, completion of arguments like app names works best if completion
# caching is enabled. Oh-my-zsh also does this for you:
# https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/completion.zsh#L34-L36
#
# Requires: The hk Heroku client (https://hk.heroku.com)
# Author: <NAME> (https://bgentry.io)

_hk_is_default_cloud() {
  ( [[ -z $HEROKU_API_URL ]] || [[ "https://api.heroku.com" == $HEROKU_API_URL ]] )
}

_hkcmdnames() {
  print -l ${(f)"$(hk help commands | cut -f 2 -d ' ')"}
}

_hkcmdnames_needing_app() {
  print -l ${(f)"$(hk help commands | grep -F '[-a <app>]' | cut -f 2 -d ' ')"}
}

_hkrawcmds() {
  print -l ${(f)"$(hk help commands)"}
}

__args_for_command() {
  for i in _hkrawcmds; do
    if [[ "${${=i}[2]}" == $line[1] ]]; then
      local arglist; arglist=()
      #for j in (${^${i#*${line}[1] }}); do
      # print -l ${=${i#*${${line}[1]} }}
      for j in ${=${${i#*${${line}[1]} }%*\#*}}; do
        arglist+=($j)
      done
      _describe -t opts 'opts' arglist && ret=0

      # _arguments \
      #   '-a:appname'
      # ret=0
      return
    fi
  done
}

###########################################################
##       Functions to get data from hk and cache it      ##
###########################################################

__hk_complete_addon_service_and_plan() {
  local -a suf
  if compset -P '*:'; then
    __hk_addon_plans ${IPREFIX%%\:*} # strip : from $IPREFIX to get provider
  else
    __hk_addon_services
  fi
}

__hk_addon_services() {
  # set a local curcontext to use for caching
  local curcontext=${curcontext%:*:*}:hk-__hk_addon_services: state line cache_policy ret=1
  local cache_name=":completion:${curcontext}:"

  # See if a cache-policy is already set up, and set one if not
  zstyle -s $cache_name cache-policy cache_policy
  [[ -z "$cache_policy" ]] && zstyle $cache_name cache-policy _hk_addon_services_caching_policy

  # If _addon_services isn't populated or the cache is invalid, and we fail to
  # retrieve the cache:
  if ( ((${#_addon_services} == 0)) || _cache_invalid $cache_name ) \
    && ! _retrieve_cache $cache_name; then
    # If we've gotten to this point, the app names aren't cached. Fetch them.
    _addon_services=(${(f)"$(hk addon-services)"})
    # Store _addon_services in the cache if this is a default cloud
    ( _hk_is_default_cloud ) && _store_cache $cache_name _addon_services
  fi

  # the -S ':' gives us a : suffix after completion
  compadd -S ':' $* - $_addon_services
  # don't let this var persist in non-default clouds
  ( ! _hk_is_default_cloud ) && unset _addon_services
}

_hk_addon_services_caching_policy() {
  local -a oldp
  if ( ! _hk_is_default_cloud ); then
    return 0 # don't cache data in non-default clouds
  fi
  # Rebuild if cache is older than 1 week
  oldp=( "$1"(Nmw+1) )
  (( $#oldp ))
}

__hk_addon_plans() {
  # set a local curcontext to use for caching. Add service provider name ($1)
  # as a a suffix for caching.
  service=$1
  local curcontext=${curcontext%:*:*}:hk-__hk_addon_plans_$service: state line cache_policy ret=1
  local cache_name=":completion:${curcontext}:"
  local varname="_addon_plans_${service//-/_}" # replace - in service name w/ _
  plans_for_service=${(P)varname}

  # See if a cache-policy is already set up, and set one if not
  zstyle -s $cache_name cache-policy cache_policy
  [[ -z "$cache_policy" ]] && zstyle $cache_name cache-policy _hk_addon_plans_caching_policy

  # If $varname isn't populated or the cache is invalid, and we fail to retrieve
  # the cache:
  if ( ((${#plans_for_service} == 0)) || _cache_invalid $cache_name ) \
    && ! _retrieve_cache $cache_name; then
    # If we've gotten to this point, the plan names aren't cached. Fetch them.
    plans_for_service=(${(f)"$(hk addon-plans $service | cut -f 1 -d ' ')"})
    # Store _addon_plans in the cache if this is a default cloud
    ( _hk_is_default_cloud ) && _store_cache $cache_name plans_for_service
  fi

  compadd $plans_for_service
  # don't let this var persist in non-default clouds
  ( ! _hk_is_default_cloud ) && unset $varname
}

_hk_addon_plans_caching_policy() {
  local -a oldp
{'fraction_non_alphanumeric': 0.1610738255033557, 'fraction_numerical': 0.006161293871713059, 'mean_word_length': 3.729448491155047, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2461003', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3212, 'n_words': 919}