# Completions for dofi (A simple dotfile manager)

set -l commands add del apply list help
set -l profiles "(ls -- .dofi/ 2>/dev/null | string replace -- '.toml' '')"

function __dofi_complete_rules
    set -l profile (string match -r -- '-p\s+\S+' (commandline -cp) | string replace -r -- '-p\s+' '')
    if test $profile
        dofi list -- $profile 2>/dev/null
    else
        dofi list 2>/dev/null
    end
end

# Commands
complete -c dofi -n "not __fish_seen_subcommand_from $commands" -a add -d 'Add rule to profile'
complete -c dofi -n "not __fish_seen_subcommand_from $commands" -a del -d 'Delete rule from profile' -x
complete -c dofi -n "not __fish_seen_subcommand_from $commands" -a apply -d 'Apply profile rules' -x
complete -c dofi -n "not __fish_seen_subcommand_from $commands" -a list -d 'List rules of profile' -x
complete -c dofi -n "not __fish_seen_subcommand_from $commands" -a help -d 'Prints help information' -x

# Global Options
complete -c dofi -s h -l help -d 'Prints help information'
complete -c dofi -s V -l version -d 'Prints version information'

# Add Options
complete -c dofi -n '__fish_seen_subcommand_from add' -s m -l mode -d 'Rule mode [default: symlink]' -xa 'symlink copy'
complete -c dofi -n '__fish_seen_subcommand_from add' -s p -l profile -d 'Profile name [default: default]' -xa "$profiles"

# Del Options
complete -c dofi -n '__fish_seen_subcommand_from del' -s p -l profile -d 'Profile name [default: default]' -xa "$profiles"
complete -c dofi -n '__fish_seen_subcommand_from del' -xa '(__dofi_complete_rules)'

# Apply Options
complete -c dofi -n '__fish_seen_subcommand_from apply' -xa "$profiles"

# List Options
complete -c dofi -n '__fish_seen_subcommand_from list' -s f -l full -d 'Prints full infomations'
complete -c dofi -n '__fish_seen_subcommand_from list' -xa "$profiles"

# Help Options
complete -c dofi -n '__fish_seen_subcommand_from help' -xa "$commands"
