Changeset - cf4ab3460dba
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 years ago 2022-05-31 21:15:41
branko@majic.rs
Noticket: [factorio_manager.sh] Add support for forcing colour output.
1 file changed with 86 insertions and 76 deletions:
0 comments (0 inline, 0 general) First comment
games/factorio_manager.sh
Show inline comments
 
@@ -212,6 +212,8 @@ versions
 

	
 
$program accepts the following options:
 

	
 
    -C
 
        Force colour output.
 
    -q
 
        Quiet mode. Output a message only if newer packages are available.
 
    -d
 
@@ -250,82 +252,85 @@ $program, version $version
 
EOF
 
}
 

	
 
# Set-up colours for message printing if we're not piping and terminal is
 
# capable of outputting the colors.
 
_color_terminal=$(tput colors 2>&1)
 
if [[ -t 1 ]] && (( _color_terminal > 0 )); then
 
    _text_black=$(tput setaf 0)
 
    _text_red=$(tput setaf 1)
 
    _text_green=$(tput setaf 2)
 
    _text_yellow=$(tput setaf 3)
 
    _text_blue=$(tput setaf 4)
 
    _text_purple=$(tput setaf 5)
 
    _text_cyan=$(tput setaf 6)
 
    _text_white=$(tput setaf 7)
 

	
 
    _text_bold=$(tput bold)
 
    _text_reset=$(tput sgr0)
 

	
 
    _bg_black=$(tput setab 0)
 
    _bg_red=$(tput setab 1)
 
    _bg_green=$(tput setab 2)
 
    _bg_yellow=$(tput setab 3)
 
    _bg_blue=$(tput setab 4)
 
    _bg_purple=$(tput setab 5)
 
    _bg_cyan=$(tput setab 6)
 
    _bg_white=$(tput setab 7)
 
else
 
    _text_black=""
 
    _text_red=""
 
    _text_green=""
 
    _text_yellow=""
 
    _text_blue=""
 
    _text_purple=""
 
    _text_cyan=""
 
    _text_white=""
 

	
 
    _text_bold=""
 
    _text_reset=""
 

	
 
    # Part of the standard Majic Bash script template.
 
    # shellcheck disable=SC2034
 
    _bg_black=""
 
    # shellcheck disable=SC2034
 
    _bg_red=""
 
    # shellcheck disable=SC2034
 
    _bg_green=""
 
    # shellcheck disable=SC2034
 
    _bg_yellow=""
 
    # shellcheck disable=SC2034
 
    _bg_blue=""
 
    # shellcheck disable=SC2034
 
    _bg_purple=""
 
    # shellcheck disable=SC2034
 
    _bg_cyan=""
 
    # shellcheck disable=SC2034
 
    _bg_white=""
 
fi
 
function setup_colours() {
 

	
 
    # Set-up colours for message printing if we're not piping and terminal is
 
    # capable of outputting the colors.
 
    _color_terminal=$(tput colors 2>&1)
 
    if [[ -t 1 ]] && (( _color_terminal > 0 )) || (( force_colours == 1 )); then
 
        _text_black=$(tput setaf 0)
 
        _text_red=$(tput setaf 1)
 
        _text_green=$(tput setaf 2)
 
        _text_yellow=$(tput setaf 3)
 
        _text_blue=$(tput setaf 4)
 
        _text_purple=$(tput setaf 5)
 
        _text_cyan=$(tput setaf 6)
 
        _text_white=$(tput setaf 7)
 

	
 
        _text_bold=$(tput bold)
 
        _text_reset=$(tput sgr0)
 

	
 
        _bg_black=$(tput setab 0)
 
        _bg_red=$(tput setab 1)
 
        _bg_green=$(tput setab 2)
 
        _bg_yellow=$(tput setab 3)
 
        _bg_blue=$(tput setab 4)
 
        _bg_purple=$(tput setab 5)
 
        _bg_cyan=$(tput setab 6)
 
        _bg_white=$(tput setab 7)
 
    else
 
        _text_black=""
 
        _text_red=""
 
        _text_green=""
 
        _text_yellow=""
 
        _text_blue=""
 
        _text_purple=""
 
        _text_cyan=""
 
        _text_white=""
 

	
 
        _text_bold=""
 
        _text_reset=""
 

	
 
        # Part of the standard Majic Bash script template.
 
        # shellcheck disable=SC2034
 
        _bg_black=""
 
        # shellcheck disable=SC2034
 
        _bg_red=""
 
        # shellcheck disable=SC2034
 
        _bg_green=""
 
        # shellcheck disable=SC2034
 
        _bg_yellow=""
 
        # shellcheck disable=SC2034
 
        _bg_blue=""
 
        # shellcheck disable=SC2034
 
        _bg_purple=""
 
        # shellcheck disable=SC2034
 
        _bg_cyan=""
 
        # shellcheck disable=SC2034
 
        _bg_white=""
 
    fi
 

	
 
# Make the colors available via an associative array as well.
 
declare -A _text_colors=()
 

	
 
_text_colors[black]="${_text_black}"
 
_text_colors[blue]="${_text_blue}"
 
_text_colors[cyan]="${_text_cyan}"
 
_text_colors[green]="${_text_green}"
 
_text_colors[purple]="${_text_purple}"
 
_text_colors[red]="${_text_red}"
 
_text_colors[white]="${_text_white}"
 
_text_colors[yellow]="${_text_yellow}"
 

	
 
_text_colors[boldblack]="${_text_bold}${_text_black}"
 
_text_colors[boldblue]="${_text_bold}${_text_blue}"
 
_text_colors[boldcyan]="${_text_bold}${_text_cyan}"
 
_text_colors[boldgreen]="${_text_bold}${_text_green}"
 
_text_colors[boldpurple]="${_text_bold}${_text_purple}"
 
_text_colors[boldred]="${_text_bold}${_text_red}"
 
_text_colors[boldwhite]="${_text_bold}${_text_white}"
 
_text_colors[boldyellow]="${_text_bold}${_text_yellow}"
 
    # Make the colors available via an associative array as well.
 
    declare -g -A _text_colors=()
 

	
 
    _text_colors[black]="${_text_black}"
 
    _text_colors[blue]="${_text_blue}"
 
    _text_colors[cyan]="${_text_cyan}"
 
    _text_colors[green]="${_text_green}"
 
    _text_colors[purple]="${_text_purple}"
 
    _text_colors[red]="${_text_red}"
 
    _text_colors[white]="${_text_white}"
 
    _text_colors[yellow]="${_text_yellow}"
 

	
 
    _text_colors[boldblack]="${_text_bold}${_text_black}"
 
    _text_colors[boldblue]="${_text_bold}${_text_blue}"
 
    _text_colors[boldcyan]="${_text_bold}${_text_cyan}"
 
    _text_colors[boldgreen]="${_text_bold}${_text_green}"
 
    _text_colors[boldpurple]="${_text_bold}${_text_purple}"
 
    _text_colors[boldred]="${_text_bold}${_text_red}"
 
    _text_colors[boldwhite]="${_text_bold}${_text_white}"
 
    _text_colors[boldyellow]="${_text_bold}${_text_yellow}"
 
}
 

	
 
# Set-up functions for printing coloured messages.
 
function debug() {
 
@@ -1220,6 +1225,7 @@ ERROR_GENERAL=3
 
# Disable debug and quiet modes by default.
 
debug=0
 
quiet=0
 
force_colours=0
 

	
 
# Set-up some default paths.
 
manager_directory="$HOME/.factorio"
 
@@ -1227,13 +1233,15 @@ game_installations_directory="$manager_directory/.game_installations"
 

	
 
# If no arguments were given, just show usage help.
 
if [[ -z ${1-} ]]; then
 
    setup_colours
 
    short_usage
 
    exit "$SUCCESS"
 
fi
 

	
 
# Parse the arguments
 
while getopts "qdvh" opt; do
 
while getopts "Cqdvh" opt; do
 
    case "$opt" in
 
        C) force_colours=1;;
 
	q) # shellcheck disable=SC2034 # part of standard Bash script template.
 
           quiet=1;;
 
	d) debug=1;;
 
@@ -1248,6 +1256,8 @@ done
 
i=$OPTIND
 
shift $((i-1))
 

	
 
setup_colours
 

	
 
# Make sure the manager home directory exists.
 
if [[ ! -e $manager_directory ]]; then
 
    info "Creating Factorio Manager home directory under: $manager_directory"
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now