diff --git a/scripts/sbs_autocomplete b/scripts/sbs_autocomplete index 3ebad7a973c339042a32223a5bd20a3c78d4ef1f..4610ff722328a612d321a589f520ccaa69314e60 100755 --- a/scripts/sbs_autocomplete +++ b/scripts/sbs_autocomplete @@ -20,25 +20,33 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -source "$(cd -- "$(dirname "$0")" > /dev/null 2>&1 && pwd -P)/sbs_implementation" +current_path="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd -P)" + +# Target directory name +target_dir="skyward-boardcore" + +# Cut the path to the target directory +while [[ "$current_path" != "/" && "$(basename "$current_path")" != "$target_dir" ]]; do + current_path="$(dirname "$current_path")" +done + +source "$current_path/scripts/sbs_implementation" function _sbs_autocomplete() { - local curr_arg="${COMP_WORDS[COMP_CWORD]}" - local prev_arg="${COMP_WORDS[COMP_CWORD-1]}" + declare curr_arg="${COMP_WORDS[COMP_CWORD]}" + declare prev_arg="${COMP_WORDS[COMP_CWORD - 1]}" case "$prev_arg" in - "-b"|"--build") - ;& - "-f"|"--flash") - ;& - "-t"|"--test") - # Autocomplete options for build, flash and test - init_dirs - COMPREPLY=( $(compgen -W "$(list --only-list)" -- "$curr_arg") ) - ;; - *) - # Autocomplete options for general options - COMPREPLY=( $(compgen -W "\ + "-b" | "--build") ;& + "-f" | "--flash") ;& + "-t" | "--test") + # Autocomplete options for build, flash and test + init_dirs + COMPREPLY=($(compgen -W "$(list --only-list)" -- "$curr_arg")) + ;; + *) + # Autocomplete options for general options + COMPREPLY=($(compgen -W "\ -h --help \ -j --jobs \ -l --list \ @@ -52,8 +60,8 @@ function _sbs_autocomplete() { -v --verbose \ -n --lint \ -e --edit \ - --clang-tidy" -- "$curr_arg") ) - ;; + --clang-tidy" -- "$curr_arg")) + ;; esac } diff --git a/scripts/sbs_implementation b/scripts/sbs_implementation index f0e6a3b87315ed70ecfdb7e78b7a752940fba324..5792c46b249d89e902c29a775070b14b6585f30d 100755 --- a/scripts/sbs_implementation +++ b/scripts/sbs_implementation @@ -126,6 +126,39 @@ configure() { { [ "$config_debug" = true ] && touch "$build_dir/$DEBUG_FILENAME"; } || rm -f "$build_dir/$DEBUG_FILENAME" { [ "$config_verbose" = true ] && touch "$build_dir/$VERBOSE_FILENAME"; } || rm -f "$build_dir/$VERBOSE_FILENAME" + + install_autocompletion +} + +install_autocompletion() { + declare autocomplete_script="$sbs_base/scripts/sbs_autocomplete" + declare -A shell_files=( + ["bash"]="$HOME/.bashrc" + ["zsh"]="$HOME/.zshrc" + ["fish"]="$HOME/.config/fish/config.fish" + ) + + declare -a installed_shells + + for shell in "${!shell_files[@]}"; do + local shell_file="${shell_files[$shell]}" + + if [[ -f "$shell_file" ]]; then + if ! grep -q "source $autocomplete_script" "$shell_file"; then + echo "source $autocomplete_script" >>"$shell_file" + fi + installed_shells+=("$shell") + fi + done + + if [[ ${#installed_shells[@]} -gt 0 ]]; then + echo "Autocomplete script has been added for the following shells:" + for shell in "${installed_shells[@]}"; do + echo "- $shell" + done + else + echo "No supported shells found. Please add the following line to your shell's initialization file manually: 'source $autocomplete_script'" + fi } check_configured() {