#!/bin/zsh

### List cloned bundles.
#
# usage: antidote list [-h|--help] [-s|--short] [-d|--dirs] [-u|--url]
#
#function antidote-list {
  emulate -L zsh; setopt local_options $_adote_funcopts

  local o_help o_short o_url o_dirs
  zparseopts $_adote_zparopt_flags -- \
    h=o_help  -help=h  \
    s=o_short -short=s \
    u=o_url   -url=u   \
    d=o_dirs  -dirs=d  ||
    return 1

  if (( $#o_help )); then
    antidote-help list
    return
  fi

  if [[ $# -ne 0 ]]; then
    print -ru2 "antidote: error: unexpected $1, try --help"
    return 1
  fi

  local bundledir
  local output=()
  local bundles=($(antidote-home)/**/.git(/N))

  for bundledir in $bundles; do
    bundledir=${bundledir:h}
    local url=$(git -C "$bundledir" config remote.origin.url)
    if (( $#o_dirs )); then
      output+=($bundledir)
    elif (( $#o_url )); then
      output+=($url)
    elif (( $#o_short )); then
      url=${url%.git}
      url=${url#https://github.com/}
      output+=($url)
    else
      output+=("$(printf '%-64s %s\n' $url $bundledir)")
    fi
  done
  (( $#output )) && printf '%s\n' ${(o)output}
#}
