Update zrep script with new color scheme and theme loading functionality.
- Added new base_colors associative array for color definitions. - Implemented zrep_load_theme function to load themes. - Modified zrep_msg function to use colors from the current theme. - Updated zrep_version function to use theme colors for help and info. - Adjusted zrep_list_installed_packages to use base_colors for color definitions. - Enhanced zrep_update_package with informative messages. - Added zrep_download_package function for downloading packages. - Improved zrep_install_package to use zrep_download_package for downloads. - Updated main function to load the global theme.
This commit is contained in:
		
							
								
								
									
										10
									
								
								themes/classic
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								themes/classic
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
# Classic zrep theme
 | 
			
		||||
declare -A theme_colors=(
 | 
			
		||||
    [std]="green"
 | 
			
		||||
    [info]="yellow"
 | 
			
		||||
    [debug]="red"
 | 
			
		||||
    [other]="bold_yellow"
 | 
			
		||||
    [sub]="magenta"
 | 
			
		||||
    [main]="white green_bg" # Combining colors for 'main'
 | 
			
		||||
    [help]="bold_green"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										159
									
								
								zrep
									
									
									
									
									
								
							
							
						
						
									
										159
									
								
								zrep
									
									
									
									
									
								
							@@ -28,58 +28,92 @@ zrep_fpath ${HOME}/.zrep/functions
 | 
			
		||||
autoload -Uz zini
 | 
			
		||||
 | 
			
		||||
# List of colors available
 | 
			
		||||
typeset -A colors
 | 
			
		||||
colors=(
 | 
			
		||||
  black "\033[0;30m"
 | 
			
		||||
  red "\033[0;31m"
 | 
			
		||||
  green "\033[0;32m"
 | 
			
		||||
  yellow "\033[0;33m"
 | 
			
		||||
  blue "\033[0;34m"
 | 
			
		||||
  magenta "\033[0;35m"
 | 
			
		||||
  cyan "\033[0;36m"
 | 
			
		||||
  white "\033[0;37m"
 | 
			
		||||
  bold_black "\033[1;30m"
 | 
			
		||||
  bold_red "\033[1;31m"
 | 
			
		||||
  bold_green "\033[1;32m"
 | 
			
		||||
  bold_yellow "\033[1;33m"
 | 
			
		||||
  bold_blue "\033[1;34m"
 | 
			
		||||
  bold_magenta "\033[1;35m"
 | 
			
		||||
  bold_cyan "\033[1;36m"
 | 
			
		||||
  bold_white "\033[1;37m"
 | 
			
		||||
  black_bg "\033[40m"
 | 
			
		||||
  red_bg "\033[41m"
 | 
			
		||||
  green_bg "\033[42m"
 | 
			
		||||
  yellow_bg "\033[43m"
 | 
			
		||||
  blue_bg "\033[44m"
 | 
			
		||||
  magenta_bg "\033[45m"
 | 
			
		||||
  cyan_bg "\033[46m"
 | 
			
		||||
  white_bg "\033[47m"
 | 
			
		||||
  end "\033[0m"
 | 
			
		||||
typeset -A base_colors=(
 | 
			
		||||
    [green]="\033[0;32m"
 | 
			
		||||
    [yellow]="\033[1;33m"
 | 
			
		||||
    [red]="\033[0;31m"
 | 
			
		||||
    [bold_yellow]="\033[1;33m"
 | 
			
		||||
    [magenta]="\033[0;35m"
 | 
			
		||||
    [white]="\033[1;37m"
 | 
			
		||||
    [green_bg]="\033[42m"
 | 
			
		||||
    [white_on_green]="\033[1;37m\033[42m" # Combined color
 | 
			
		||||
    [end]="\033[0m"
 | 
			
		||||
    [black]="\033[0;30m"
 | 
			
		||||
    [blue]="\033[0;34m"
 | 
			
		||||
    [cyan]="\033[0;36m"
 | 
			
		||||
    [bold_black]="\033[1;30m"
 | 
			
		||||
    [bold_red]="\033[1;31m"
 | 
			
		||||
    [bold_green]="\033[1;32m"
 | 
			
		||||
    [bold_blue]="\033[1;34m"
 | 
			
		||||
    [bold_magenta]="\033[1;35m"
 | 
			
		||||
    [bold_cyan]="\033[1;36m"
 | 
			
		||||
    [bold_white]="\033[1;37m"
 | 
			
		||||
    [black_bg]="\033[40m"
 | 
			
		||||
    [red_bg]="\033[41m"
 | 
			
		||||
    [yellow_bg]="\033[43m"
 | 
			
		||||
    [blue_bg]="\033[44m"
 | 
			
		||||
    [magenta_bg]="\033[45m"
 | 
			
		||||
    [cyan_bg]="\033[46m"
 | 
			
		||||
    [white_bg]="\033[47m"
 | 
			
		||||
    [underline]="\033[4m"
 | 
			
		||||
    [italic]="\033[3m"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Define the global associative array to hold the current theme
 | 
			
		||||
declare -A current_theme
 | 
			
		||||
 | 
			
		||||
zrep_load_theme() {
 | 
			
		||||
  local theme_name="$1"
 | 
			
		||||
  local theme_file="${config[main_zrep_install_dir]}/themes/${theme_name}"
 | 
			
		||||
 | 
			
		||||
  if [[ ! -f "$theme_file" ]]; then
 | 
			
		||||
    echo "Error: Theme file for '${theme_name}' not found. Falling back to the 'classic' theme."
 | 
			
		||||
    theme_file="${config[main_zrep_install_dir]}/themes/classic"
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  # Clear previous theme settings
 | 
			
		||||
  #unset current_theme
 | 
			
		||||
  #declare -gA current_theme
 | 
			
		||||
 | 
			
		||||
  # Source the theme file, which should define 'theme_colors'
 | 
			
		||||
  source "$theme_file"
 | 
			
		||||
 | 
			
		||||
  # Copy 'theme_colors' to 'current_theme'
 | 
			
		||||
  for key value in ${(kv)theme_colors}; do
 | 
			
		||||
    current_theme[$key]="$value"
 | 
			
		||||
  done
 | 
			
		||||
  # print -l ${(kv)current_theme}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function zrep_main_version_string() {
 | 
			
		||||
  echo "${colors[bold_black]}${colors[white_bg]} ${ZREP} ${colors[end]}${colors[bold_white]}${colors[black_bg]} ${VERSION} ${colors[end]}"
 | 
			
		||||
  echo "${base_colors[bold_black]}${base_colors[white_bg]} ${ZREP} ${base_colors[end]}${base_colors[bold_white]}${base_colors[black_bg]} ${VERSION} ${base_colors[end]}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function zrep_version() {
 | 
			
		||||
  zrep_msg info "\nCreated by kekePower - 2024"
 | 
			
		||||
  zrep_msg info "License: MIT"
 | 
			
		||||
  zrep_msg info "https://git.kekepower.com/kekePower/zrep/"
 | 
			
		||||
  zrep_msg info "Please see '${colors[bold_green]}${ZSH_SCRIPT:t} help${colors[end]}'${colors[yellow]} for more info"
 | 
			
		||||
  zrep_msg info "Please see '${base_colors[${current_theme[help]}]}${ZSH_SCRIPT:t} help${base_colors[end]}${base_colors[${current_theme[info]}]}' for more info${base_colors[end]}"
 | 
			
		||||
  exit
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function zrep_msg() {
 | 
			
		||||
    local color=${colors[end]} # Default to no color if type is unrecognized
 | 
			
		||||
    case ${1} in
 | 
			
		||||
        std) color=${colors[green]} ;;
 | 
			
		||||
        info) color=${colors[yellow]} ;;
 | 
			
		||||
        debug) color=${colors[red]} ;;
 | 
			
		||||
        other) color=${colors[bold_yellow]} ;;
 | 
			
		||||
        sub) color=${colors[magenta]} ;;
 | 
			
		||||
        main) color="${colors[white]} ${colors[green_bg]}" ;;
 | 
			
		||||
    esac
 | 
			
		||||
    printf "${color}%b${colors[end]}\n" "${2}"
 | 
			
		||||
    local msg_type="$1"
 | 
			
		||||
    local message="$2"
 | 
			
		||||
    local color="${base_colors[end]}" # Default color
 | 
			
		||||
 | 
			
		||||
    # Retrieve the color key from the current theme
 | 
			
		||||
    local theme_color_key="${current_theme[$msg_type]}"
 | 
			
		||||
 | 
			
		||||
    # Check if a valid color was found based on the key
 | 
			
		||||
    if [[ -n "${base_colors[$theme_color_key]}" ]]; then
 | 
			
		||||
        color="${base_colors[$theme_color_key]}"
 | 
			
		||||
    else
 | 
			
		||||
        # Handle invalid theme color key if needed
 | 
			
		||||
        echo "Warning: Theme color key '$theme_color_key' not found. Using default." >&2
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    printf "%b\n" "${color}${message}${base_colors[end]}"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function zrep_init() {
 | 
			
		||||
@@ -228,11 +262,11 @@ function zrep_list_installed_packages() {
 | 
			
		||||
    # Iterate through each author and their packages
 | 
			
		||||
    jq -r 'to_entries | .[] | .key as $author | .value[] | "\($author)/\(.script) (\(.version))"' "${installed_json}" | while IFS= read -r package_info; do
 | 
			
		||||
        local package_name=$(echo "${package_info}" | cut -d ' ' -f1)  # Extract package name before the version
 | 
			
		||||
        local is_active="${colors[white]}(${colors[end]}${colors[bold_red]}Inactive${colors[end]}${colors[white]})${colors[end]}"  # Set default to Inactive
 | 
			
		||||
        local is_active="${base_colors[white]}(${base_colors[end]}${base_colors[bold_red]}Inactive${base_colors[end]}${base_colors[white]})${base_colors[end]}"  # Set default to Inactive
 | 
			
		||||
 | 
			
		||||
        # Check if the package is active (only modify if active)
 | 
			
		||||
        if grep -q "${package_name}" ${config[main_zrep_install_dir]}/.addons; then
 | 
			
		||||
            is_active="${colors[white]}(${colors[end]}${colors[bold_green]}Active${colors[end]}${colors[white]})${colors[end]}"
 | 
			
		||||
            is_active="${base_colors[white]}(${base_colors[end]}${base_colors[bold_green]}Active${base_colors[end]}${base_colors[white]})${base_colors[end]}"
 | 
			
		||||
        fi
 | 
			
		||||
 | 
			
		||||
        zrep_msg info " - ${package_info} ${is_active}"
 | 
			
		||||
@@ -403,12 +437,12 @@ function zrep_update_package() {
 | 
			
		||||
        if [[ -n "$version" ]]; then
 | 
			
		||||
            local author="${specificPackage%%/*}"
 | 
			
		||||
            local script="${specificPackage#*/}"
 | 
			
		||||
            local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
 | 
			
		||||
            # local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
 | 
			
		||||
            # echo "Updating $specificPackage to version $version..."
 | 
			
		||||
            local install_pkg="${author}/${script}"
 | 
			
		||||
            zrep_install_package u ${install_pkg}
 | 
			
		||||
        else
 | 
			
		||||
            echo "No update available for ${specificPackage}."
 | 
			
		||||
            zrep_msg info "\nNo update available for ${specificPackage}."
 | 
			
		||||
        fi
 | 
			
		||||
    else
 | 
			
		||||
        if [[ ${updates} == "true" ]]; then
 | 
			
		||||
@@ -429,6 +463,34 @@ function zrep_update_package() {
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function zrep_download_package() {
 | 
			
		||||
    local zipFile="${1}"
 | 
			
		||||
    local dlurl="${2}"
 | 
			
		||||
    local retries=5
 | 
			
		||||
    local delay=5
 | 
			
		||||
    local attempt=1
 | 
			
		||||
 | 
			
		||||
    while (( attempt <= retries )); do
 | 
			
		||||
        zrep_msg info "Attempt $attempt of $retries: Downloading..."
 | 
			
		||||
        # Use curl to download the file, capturing HTTP status code
 | 
			
		||||
        http_status=$(curl -s -w "%{http_code}" -o "${zipFile}" "${dlurl}" -o /dev/null)
 | 
			
		||||
 | 
			
		||||
        # Check if curl command was successful and HTTP status is 200 (OK)
 | 
			
		||||
        if [[ $? -eq 0 && ${http_status} -eq 200 ]]; then
 | 
			
		||||
            zrep_msg info "Download successful."
 | 
			
		||||
            return 0
 | 
			
		||||
        else
 | 
			
		||||
            zrep_msg debug "Error: Failed to download the package. HTTP status: ${http_status}."
 | 
			
		||||
            ((attempt++))
 | 
			
		||||
            zrep_msg info "Waiting ${delay} seconds before retrying..."
 | 
			
		||||
            sleep ${delay}
 | 
			
		||||
        fi
 | 
			
		||||
    done
 | 
			
		||||
 | 
			
		||||
    zrep_msg debug "Error: The download failed after ${retries} attempts."
 | 
			
		||||
    return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Function to install a package by unzipping it to ${config[main_zrep_install_dir]}
 | 
			
		||||
function zrep_install_package() {
 | 
			
		||||
 | 
			
		||||
@@ -456,13 +518,8 @@ function zrep_install_package() {
 | 
			
		||||
 | 
			
		||||
    mkdir -p "${tmpDir}"
 | 
			
		||||
 | 
			
		||||
    local zipFile="${tmpDir}/${author}-${script_name}-${version}.zip"
 | 
			
		||||
    curl -s -o "${zipFile}" "${dlurl}"
 | 
			
		||||
 | 
			
		||||
    if [[ $? -ne 0 ]]; then
 | 
			
		||||
        echo "Error: Failed to download the package."
 | 
			
		||||
        return 1
 | 
			
		||||
    fi
 | 
			
		||||
    local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
 | 
			
		||||
    zrep_download_package "${zipFile}" "${dlurl}"
 | 
			
		||||
 | 
			
		||||
    unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}"
 | 
			
		||||
 | 
			
		||||
@@ -631,8 +688,8 @@ function zrep_read_usage() {
 | 
			
		||||
function main() {
 | 
			
		||||
 | 
			
		||||
    zrep_main_version_string
 | 
			
		||||
 | 
			
		||||
    zrep_load_config
 | 
			
		||||
    zrep_load_theme ${config[global_theme]}
 | 
			
		||||
 | 
			
		||||
    # Check if the second argument is "help" and the first argument is not empty
 | 
			
		||||
    if [[ "${2}" == "help" && -n "${1}" ]]; then
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user