# ----------------------------------------------------------------------
# rainbow, a terminal colorizer - https://github.com/nicoulaj/rainbow
# copyright (c) 2010-2018 rainbow contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------

_rainbow()
{
  local offset=0 i
  for (( i=1; i <= COMP_CWORD; i++ )); do
    case ${COMP_WORDS[$i]} in
      --version|-h|--help|--disable-stderr-filtering|--verbose|-f|--config|-r|-g|-y|-b|-m|-c|--red|--lightred|--green|--lightgreen|--yellow|--lightyellow|--blue|--lightblue|--magenta|--cyan|--lightcyan|--red-before|--lightred-before|--green-before|--lightgreen-before|--yellow-before|--lightyellow-before|--blue-before|--lightblue-before|--magenta-before|--cyan-before|--lightcyan-before|--reset-before|--red-after|--lightred-after|--green-after|--lightgreen-after|--yellow-after|--lightyellow-after|--blue-after|--lightblue-after|--magenta-after|--cyan-after|--lightcyan-after|--reset-after|--background-red|--background-lightred|--background-green|--background-lightgreen|--background-yellow|--background-lightyellow|--background-blue|--background-lightblue|--background-magenta|--background-cyan|--background-lightcyan|--background-red-before|--background-lightred-before|--background-green-before|--background-lightgreen-before|--background-yellow-before|--background-lightyellow-before|--background-blue-before|--background-lightblue-before|--background-magenta-before|--background-cyan-before|--background-lightcyan-before|--background-reset-before|--background-red-after|--background-lightred-after|--background-green-after|--background-lightgreen-after|--background-yellow-after|--background-lightyellow-after|--background-blue-after|--background-lightblue-after|--background-magenta-after|--background-cyan-after|--background-lightcyan-after|--background-reset-after|--bold|--bold-before|--bold-after|--faint|--faint-before|--faint-after|--intensity-reset-before|--intensity-reset-after|--italic|--italic-before|--italic-after|--italic-reset-before|--italic-reset-after|--underline|--underline-before|--underline-after|--underline-double|--underline-double-before|--underline-double-after|--underline-reset-before|--underline-reset-after|--blink|--blink-before|--blink-after|--blink-rapid|--blink-rapid-before|--blink-rapid-after|--blink-reset-before|--blink-reset-after|--negative|--negative-before|--negative-after|--negative-reset-before|--negative-reset-after|--hide|--hide-before|--hide-after|--hide-reset-before|--hide-reset-after|--reset-all|--reset-all-before|--reset-all-after)
        i=$((i+1))
        continue
        ;;
      -*)
        continue
        ;;
    esac
    offset=$i
    break
  done
  if [[ $offset -gt 0 ]]; then
    _command_offset $offset
  else
  cur=${COMP_WORDS[COMP_CWORD]}
    if [ $COMP_CWORD -eq 1 ]; then
    COMPREPLY=( $( compgen -W "--version -h --help --print-path --print-config-names --disable-stderr-filtering --verbose -f --config -r -g -y -b -m -c --red --lightred --green --lightgreen --yellow --lightyellow --blue --lightblue --magenta --cyan --lightcyan --red-before --lightred-before --green-before --lightgreen-before --yellow-before --lightyellow-before --blue-before --lightblue-before --magenta-before --cyan-before --lightcyan-before --reset-before --red-after --lightred-after --green-after --lightgreen-after --yellow-after --lightyellow-after --blue-after --lightblue-after --magenta-after --cyan-after --lightcyan-after --reset-after --background-red --background-lightred --background-green --background-lightgreen --background-yellow --background-lightyellow --background-blue --background-lightblue --background-magenta --background-cyan --background-lightcyan --background-red-before --background-lightred-before --background-green-before --background-lightgreen-before --background-yellow-before --background-lightyellow-before --background-blue-before --background-lightblue-before --background-magenta-before --background-cyan-before --background-lightcyan-before --background-reset-before --background-red-after --background-lightred-after --background-green-after --background-lightgreen-after --background-yellow-after --background-lightyellow-after --background-blue-after --background-lightblue-after --background-magenta-after --background-cyan-after --background-lightcyan-after --background-reset-after --bold --bold-before --bold-after --faint --faint-before --faint-after --intensity-reset-before --intensity-reset-after --italic --italic-before --italic-after --italic-reset-before --italic-reset-after --underline --underline-before --underline-after --underline-double --underline-double-before --underline-double-after --underline-reset-before --underline-reset-after --blink --blink-before --blink-after --blink-rapid --blink-rapid-before --blink-rapid-after --blink-reset-before --blink-reset-after --negative --negative-before --negative-after --negative-reset-before --negative-reset-after --hide --hide-before --hide-after --hide-reset-before --hide-reset-after --reset-all --reset-all-before --reset-all-after" -- $cur ) )
    else
      first=${COMP_WORDS[1]}
      case "$first" in
        --version|-h|--help|--print-path|--print-config-names)
          COMPREPLY=()
          ;;
        *)
          prev=${COMP_WORDS[COMP_CWORD-1]}
          case "$prev" in
            -r|-g|-y|-b|-m|-c|--red|--lightred|--green|--lightgreen|--yellow|--lightyellow|--blue|--lightblue|--magenta|--cyan|--lightcyan|--red-before|--lightred-before|--green-before|--lightgreen-before|--yellow-before|--lightyellow-before|--blue-before|--lightblue-before|--magenta-before|--cyan-before|--lightcyan-before|--reset-before|--red-after|--lightred-after|--green-after|--lightgreen-after|--yellow-after|--lightyellow-after|--blue-after|--lightblue-after|--magenta-after|--cyan-after|--lightcyan-after|--reset-after|--background-red|--background-lightred|--background-green|--background-lightgreen|--background-yellow|--background-lightyellow|--background-blue|--background-lightblue|--background-magenta|--background-cyan|--background-lightcyan|--background-red-before|--background-lightred-before|--background-green-before|--background-lightgreen-before|--background-yellow-before|--background-lightyellow-before|--background-blue-before|--background-lightblue-before|--background-magenta-before|--background-cyan-before|--background-lightcyan-before|--background-reset-before|--background-red-after|--background-lightred-after|--background-green-after|--background-lightgreen-after|--background-yellow-after|--background-lightyellow-after|--background-blue-after|--background-lightblue-after|--background-magenta-after|--background-cyan-after|--background-lightcyan-after|--background-reset-after|--bold|--bold-before|--bold-after|--faint|--faint-before|--faint-after|--intensity-reset-before|--intensity-reset-after|--italic|--italic-before|--italic-after|--italic-reset-before|--italic-reset-after|--underline|--underline-before|--underline-after|--underline-double|--underline-double-before|--underline-double-after|--underline-reset-before|--underline-reset-after|--blink|--blink-before|--blink-after|--blink-rapid|--blink-rapid-before|--blink-rapid-after|--blink-reset-before|--blink-reset-after|--negative|--negative-before|--negative-after|--negative-reset-before|--negative-reset-after|--hide|--hide-before|--hide-after|--hide-reset-before|--hide-reset-after|--reset-all|--reset-all-before|--reset-all-after)
              COMPREPLY=()
              ;;
            -f|--config)
              COMPREPLY=( $(rainbow --print-config-names) \
                          $(compgen -d $cur ) \
                          $(compgen -f -X "!*.cfg" $cur ) )
              ;;
            *)
              COMPREPLY=( $( compgen -W "--disable-stderr-filtering --verbose -f --config -r -g -y -b -m -c --red --lightred --green --lightgreen --yellow --lightyellow --blue --lightblue --magenta --cyan --lightcyan --red-before --lightred-before --green-before --lightgreen-before --yellow-before --lightyellow-before --blue-before --lightblue-before --magenta-before --cyan-before --lightcyan-before --reset-before --red-after --lightred-after --green-after --lightgreen-after --yellow-after --lightyellow-after --blue-after --lightblue-after --magenta-after --cyan-after --lightcyan-after --reset-after --background-red --background-lightred --background-green --background-lightgreen --background-yellow --background-lightyellow --background-blue --background-lightblue --background-magenta --background-cyan --background-lightcyan --background-red-before --background-lightred-before --background-green-before --background-lightgreen-before --background-yellow-before --background-lightyellow-before --background-blue-before --background-lightblue-before --background-magenta-before --background-cyan-before --background-lightcyan-before --background-reset-before --background-red-after --background-lightred-after --background-green-after --background-lightgreen-after --background-yellow-after --background-lightyellow-after --background-blue-after --background-lightblue-after --background-magenta-after --background-cyan-after --background-lightcyan-after --background-reset-after --bold --bold-before --bold-after --faint --faint-before --faint-after --intensity-reset-before --intensity-reset-after --italic --italic-before --italic-after --italic-reset-before --italic-reset-after --underline --underline-before --underline-after --underline-double --underline-double-before --underline-double-after --underline-reset-before --underline-reset-after --blink --blink-before --blink-after --blink-rapid --blink-rapid-before --blink-rapid-after --blink-reset-before --blink-reset-after --negative --negative-before --negative-after --negative-reset-before --negative-reset-after --hide --hide-before --hide-after --hide-reset-before --hide-reset-after --reset-all --reset-all-before --reset-all-after" -- $cur ) )
              ;;
          esac
          ;;
      esac
    fi
  fi
}

complete -F _rainbow rainbow
