Changeset - 38335e9c4c93
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 years ago 2022-07-15 16:04:59
branko@majic.rs
[factorio_manager.sh] Added Bash completion for the script:

- Completion code is kept inline within the script itself, making it
easy to automatically generate it and include in Bash startup files.
1 file changed with 158 insertions and 0 deletions:
0 comments (0 inline, 0 general) First comment
games/factorio_manager.sh
Show inline comments
 
@@ -52,6 +52,8 @@ Usage:
 
  $program [OPTIONS] remove-backup INSTANCE BACKUP_NAME
 

	
 
  $program [OPTIONS] set-game-dir GAME_INSTALLATIONS_DIRECTORY
 

	
 
  $program [OPTIONS] bash-complete
 
EOF
 
}
 

	
 
@@ -99,6 +101,12 @@ backup INSTANCE [DESCRIPTION]
 
    easier to distinguish between different backups. Hidden files
 
    (names starting with '.') will be omitted from the backup.
 

	
 
bash-complete
 

	
 
    Generates code for Bash completion. Include generated code in your
 
    Bash start-up files to use it. Requires standard Bash completion
 
    libraries to be available on the system.
 

	
 
copy SOURCE_INSTANCE DESTINATION_INSTANCE
 

	
 
    Creates a copy of an existing instance. Requires name of an
 
@@ -2776,6 +2784,156 @@ elif [[ $command == rename ]]; then
 
        success "Renamed instance $(colorecho -n green "$current_name") to $(colorecho -n green "$new_name")."
 

	
 
    ) 200>"$current_instance_lock_file"
 

	
 

	
 
#===============#
 
# bash-complete #
 
#===============#
 
elif [[ $command == bash-complete ]]; then
 
    bash_completion='#!/bin/bash
 

	
 
#
 
# Bash completion for factorio_manager.sh
 
#
 
function _factorio_manager() {
 
    local cur word command command_index candidate instance
 
    declare -a commands options candidates
 

	
 
    # Store currently provided word.
 
    cur="${COMP_WORDS[COMP_CWORD]}"
 

	
 
    # Commands that can be run, -v and -h are included as well since
 
    # they effectively behave as commands.
 
    commands=(
 
        launch launch-loop info list
 
        create create-server rename copy remove import set-version reset-server-map
 
        versions install
 
        list-backups backup restore remove-backup
 
        set-game-dir bash-complete
 
        -v -h
 
    )
 

	
 
    # Optional arguments.
 
    options=(
 
        -C
 
        -V
 
        -q
 
        -d
 
    )
 

	
 
    # Options are allowed only at beginning and when no command has
 
    # already been specified. Clear list of options to denote that
 
    # they can no longer be set.
 
    for word in "${COMP_WORDS[@]:1}"; do
 
        [[ -n $word && $word != -* || $word == -v || $word == -h ]] && options=()
 
    done
 

	
 
    # If options are still allowed, command has not been specified
 
    # yet, so we can show both options and commands.
 
    if [[ ${#options[@]} != 0 ]]; then
 
        COMPREPLY=()
 
        candidates=("${commands[@]}" "${options[@]}")
 

	
 
        for candidate in "${candidates[@]}"; do
 
            if [[ $candidate == $cur* ]]; then
 
                COMPREPLY+=("$candidate")
 
            fi
 
        done
 

	
 
        return
 
    fi
 

	
 
    # Locate where the last option ends in the array. The next element should be the command.
 
    for (( i=0; i < ${#COMP_WORDS[@]}; ++i )); do
 
        [[ ${COMP_WORDS[i]} == -* && ${COMP_WORDS[i]} != -h && ${COMP_WORDS[i]} != -v ]] && (( command_index = i + 1 ))
 
    done
 

	
 
    # No options were present, command is at index 1 (index 0 is the name of shell script itself).
 
    [[ -z $command_index ]] && command_index=1
 

	
 
    # Command name has not been fully completed, we are still working on word at its expected index.
 
    if (( command_index == COMP_CWORD )); then
 
        COMPREPLY=()
 

	
 
        candidates=("${commands[@]}")
 

	
 
        for candidate in "${candidates[@]}"; do
 
            if [[ $candidate == $cur* ]]; then
 
                COMPREPLY+=("$candidate")
 
            fi
 
        done
 

	
 
        return
 
    fi
 

	
 
    # Command is now fully completed, and we can now start processing
 
    # completion based on invoked command and its expected arguments.
 
    command="${COMP_WORDS[command_index]}"
 

	
 
    # Command that expect instance name as first argument.
 
    if [[ $command =~ (launch|launch-loop|info|rename|copy|remove|set-version|reset-server-map|list-backups|backup|restore|remove-backup) ]] &&
 
           (( COMP_CWORD == command_index + 1 )); then
 
        readarray -t candidates < <(factorio_manager.sh list | grep "^  -" | sed -e "s#^  - ##;s# ([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+)##")
 

	
 
        for candidate in "${candidates[@]}"; do
 
            if [[ $candidate == $cur* ]]; then
 
                COMPREPLY+=("$candidate")
 
            fi
 
        done
 

	
 
        return
 
    fi
 

	
 
    # "import" command expects directory as second argument. Reuse standard bash completion function.
 
    if [[ $command == import ]] && (( COMP_CWORD == command_index + 2 )); then
 
        if [[ $(type -t _filedir) == "function" ]]; then
 
            _filedir -d
 
        fi
 

	
 
        return
 
    fi
 

	
 
    # "set-game-dir" command expects directory as first argument. Reuse standard bash completion function.
 
    if [[ $command == set-game-dir ]] && (( COMP_CWORD == command_index + 1 )); then
 
        if [[ $(type -t _filedir) == "function" ]]; then
 
            _filedir -d
 
        fi
 

	
 
        return
 
    fi
 

	
 
    # "install" command expects path to tar.xz file as first argument. Reuse standard bash completion function.
 
    if [[ $command == install ]] && (( COMP_CWORD == command_index + 1 )); then
 
        if [[ $(type -t _filedir_xspec) == "function" ]]; then
 
            _filedir_xspec "unxz"
 
        fi
 

	
 
        return
 
    fi
 

	
 
    # "restore" and "remove-backup" commands expect backup name as second argument.
 
    if [[ $command =~ (restore|remove-backup) ]] && (( COMP_CWORD == command_index + 2 )); then
 
        instance="${COMP_WORDS[command_index+1]}"
 
        readarray -t candidates < <(factorio_manager.sh list-backups "$instance"  | grep "  -" | sed -e "s#^  - ##;s# - .*##")
 

	
 
        COMPREPLY=()
 
        for candidate in "${candidates[@]}"; do
 
            if [[ $candidate == $cur* ]]; then
 
                COMPREPLY+=("$candidate")
 
            fi
 
        done
 

	
 
        return
 
    fi
 
}
 

	
 
# Verify availability of standard bash completion functions.
 
if [[ $(type -t _filedir) == "function" && $(type -t _filedir_xspec == "function") && -n ${_xspecs[unxz]} ]]; then
 
    complete -F _factorio_manager factorio_manager.sh
 
else
 
    echo "[ERROR] Unable to initialise bash completion for factorio_manager.sh due to missing/incomplete standard bash completion functions." >&2
 
fi'
 
    echo "$bash_completion"
 
else
 
    error "Invalid command: $command"
 

	
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now