# Helper functions
show_spinner() {
    local pid=$1
    local delay=0.1
    local spinstr='|/-\'
    local i=0

    while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
        # Get the current character for the spinner
        printf "\b%c" "${spinstr:i++%${#spinstr}:1}"
        sleep $delay
    done

    # Clear the spinner once the process is complete
    printf "\b \b \n"
}

is_installed() {
    local package="$1"
    if command -v "$package" >/dev/null 2>&1; then
        return 0 # success
    else
        return 1 # failure
    fi
}

install() {
    for package in $1; do
        # Check if app is installed
        if command -v $package >/dev/null 2>&1; then
            info "$package is already installed"
            continue
        fi

        info "Installing $package"
        # Start apt in the background, suppressing all output and errors
        sudo apt install -y $package >/dev/null 2>&1 &

        # Capture the PID of curl
        local aptPid=$!

        # Show the spinner while apt is running
        show_spinner $aptPid

        # Wait for curl to complete
        wait $aptPid
    done
    # Check if the download was successful
    success "Done"
}
