davmac314 / dinit

Service monitoring / "init" system
Apache License 2.0
576 stars 45 forks source link

Add Bash completion #111

Closed ghost closed 1 year ago

ghost commented 1 year ago

Could you please support Bash completion with commands such as dinitctl, dinit-init, etc.?

mobin-2008 commented 1 year ago

Hello. I think that's a good idea. thats allows users to more easily work with dinitctl (and similar tools). For example, I want to restart the dnscrypt-proxy service via dinitctl. instead of type dinitctl restart dnscrypt-proxy; I do like this: dinitctl res<tab> dnsc<tab> Of course, its implementation requires some discussion and dinit currently have some major issues that need to be resolved before working on that. @carbongreat13 Can you give some references about bash completion? I found something but i so happy if you can give some refrences.

davmac314 commented 1 year ago

This (and the zsh issue) are feature requests without a patch - as per the readme, generally not accepted. @mobin-2008 are you offering to implement this?

mobin-2008 commented 1 year ago

This (and the zsh issue) are feature requests without a patch - as per the readme, generally not accepted. @mobin-2008 are you offering to implement this?

I know about that; yes.

ghost commented 1 year ago

@carbongreat13 Can you give some references about bash completion? I found something but i so happy if you can give some refrences.

Hmmm, is it around here? https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html

mobin-2008 commented 1 year ago

I worked on this thing but result is not acceptable and my implementation have some serious problems:

Due to above things, There is not my high-priority ToDo but it's finally will be added to Dinit.

My bash completion:

#!/bin/bash

_etc_dinit_services_path=""
_lib_dinit_services_path=""
_usr_dinit_services_path=""
_dinit_user_services_path=""
if [ -d /etc/dinit.d/ ]; then
    _etc_dinit_services_path="/etc/dinit.d/"
fi
if [ -d /lib/dinit.d/ ]; then
    _lib_dinit_services_path="/lib/dinit.d/"
fi
if [ -d /usr/local/lib/dinit.d/ ]; then
    _usr_dinit_services_path="/usr/local/lib/dinit.d/"
fi
if [ -d $HOME/.config/dinit.d/ ]; then
    _dinit_user_services_path="$HOME/.config/dinit.d/"
fi

_dinit_all_services() {
    for path in $_etc_dinit_services_path $_usr_dinit_services_path $_lib_dinit_services_path; do
        ls $path
    done
    ls $_dinit_user_services_path
}

_dinit_services() {
    local cur=${COMP_WORDS[COMP_CWORD]}
    local thr=1
    local flags=()
    for word in "${COMP_WORDS[@]}"; do
        if [[ $word == -* || $word == --* ]]; then
            flags+=("$word")
        fi
    done
    ((thr+=${#flags[@]}))

    if [ ${COMP_CWORD} -gt $(($thr + 1)) ]; then
        return
    fi

    if [ ${COMP_CWORD} -eq $thr ]; then
        COMPREPLY=($(compgen -W "status list start stop restart enable disable" -- "$cur"))
        return
    fi

    local services
    case "${COMP_WORDS[$thr]}" in
        "start")
            services=$(_dinit_all_services)
        ;;
        "stop")
            services=$(_dinit_all_services)
        ;;
        "restart")
            services=$(_dinit_all_services)
        ;;
        "status")
            services=$(_dinit_all_services)
        ;;
        "disable")
            services=$(_dinit_all_services)
        ;;
        "enable")
            services=$(_dinit_all_services)
        ;;
    esac
    if [ -z "$services" ]; then
        return
    fi
    COMPREPLY=($(compgen -W "$services" -- "$cur"))
}

complete -F _dinit_services dinitctl

See also: https://forum.artixlinux.org/index.php/topic,4996.0.html Regards

davmac314 commented 1 year ago

Hey @mobin-2008 , please close this if it is going nowhere. There is a contrib area that I added in 2f783d167333898d41f3a1b88a4af9e93bb6ea9f with some completion scripts for zsh/fish so if desired you can open a PR to drop the above script into that area as well.