#!/bin/sh

## COLOR DEFINITIONS
# Define colors for usage in prompts. Note that these are escaped for usage
# in the prompt and shouldn't be used elsewhere. Read the bash man page to
# see why this is necessary
export eBLACK='\[\e[0;30m\]'
export eBLUE='\[\e[0;34m\]'
export eGREEN='\[\e[0;32m\]'
export eCYAN='\[\e[0;36m\]'
export eRED='\[\e[0;31m\]'
export ePURPLE='\[\e[0;35m\]'
export eBROWN='\[\e[0;33m\]'
export eLIGHTGRAY='\[\e[0;37m\]'
export eDARKGRAY='\[\e[1;30m\]'
export eLIGHTBLUE='\[\e[1;34m\]'
export eLIGHTGREEN='\[\e[1;32m\]'
export eLIGHTCYAN='\[\e[1;36m\]'
export eLIGHTRED='\[\e[1;31m\]'
export eIGHTPURPLE='\[\e[1;35m\]'
export eYELLOW='\[\e[1;33m\]'
export eWHITE='\[\e[1;37m\]'
export eNC='\[\e[0m\]'

# Define colors for generic usage
export BLACK='\e[0;30m'
export BLUE='\e[0;34m'
export GREEN='\e[0;32m'
export CYAN='\e[0;36m'
export RED='\e[0;31m'
export PURPLE='\e[0;35m'
export BROWN='\e[0;33m'
export LIGHTGRAY='\e[0;37m'
export DARKGRAY='\e[1;30m'
export LIGHTBLUE='\e[1;34m'
export LIGHTGREEN='\e[1;32m'
export LIGHTCYAN='\e[1;36m'
export LIGHTRED='\e[1;31m'
export LIGHTPURPLE='\e[1;35m'
export YELLOW='\e[1;33m'
export WHITE='\e[1;37m'
export NC='\e[0m'
## END COLOR DEFINITIONS

function ErrorMsg() {
	echo "$@" 1>&2
}

# Helper function to give informative messages to the user
# These are more-or-less based on the similar functions used in Gentoo's
# init scripts and portage.

# Print an informative message to the user on STDERR
function emsg() {
	#echo -e " ${LIGHTGREEN}*$NC $@" 1>&2
	printf "%b %s\n" " ${LIGHTGREEN}*$NC" "$@" 1>&2
}

# Print a warning message to the user on STDERR
function ewarn() {
	printf "${YELLOW}*$NC %s\n" "${@}" 1>&2
}

# Print an error message to the user on STDERR
function eerror() {
#	echo -e " ${LIGHTRED}*$NC $@" 1>&2
	printf "${LIGHTRED}*$NC %s\n" "${@}" 1>&2
}

kspinner() {
    local pid=$1
    local delay=0.1
    # Define frames as an array of colored characters
    local spinstr=( $'\e[31m|\e[0m' $'\e[32m/\e[0m' $'\e[33m-\e[0m' $'\e[34m\\\e[0m' )
    local i=0

    while kill -0 "$pid" 2>/dev/null; do
        # Access the array element at index i
        # Use %s because the element contains multiple characters (escape codes)
        printf "\r [%s] " "${spinstr[i]}"
        
        # Increment index and wrap around using modulo
        i=$(( (i + 1) % ${#spinstr[@]} ))
        
        sleep "$delay"
    done
    # Clean up the line when done
    printf "\r        \r"
}

ispinner() {
    # local pid=$1
    local status="$1"
    local delay=0.1
    # Define frames as an array of colored characters
    local spinstr=( $'\e[31m|\e[0m' $'\e[32m/\e[0m' $'\e[33m-\e[0m' $'\e[34m\\\e[0m' )
    local i=0

    while [ 1 = 1 ]; do
        # Access the array element at index i
        # Use %s because the element contains multiple characters (escape codes)
        printf "\r%-60s [%s] " "${status}" "${spinstr[i]}"
        
        # Increment index and wrap around using modulo
        i=$(( (i + 1) % ${#spinstr[@]} ))
        
        sleep "$delay"
    done
    # Clean up the line when done
    printf "\r        \r"
}


## END OUTPUT FUNCTIONS