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:
Stig-Ørjan Smelror 2024-03-12 23:52:10 +01:00
parent 7ddcd309c5
commit de29949486
2 changed files with 119 additions and 52 deletions

10
themes/classic Normal file
View 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"
)

161
zrep
View File

@ -28,58 +28,92 @@ zrep_fpath ${HOME}/.zrep/functions
autoload -Uz zini autoload -Uz zini
# List of colors available # List of colors available
typeset -A colors typeset -A base_colors=(
colors=( [green]="\033[0;32m"
black "\033[0;30m" [yellow]="\033[1;33m"
red "\033[0;31m" [red]="\033[0;31m"
green "\033[0;32m" [bold_yellow]="\033[1;33m"
yellow "\033[0;33m" [magenta]="\033[0;35m"
blue "\033[0;34m" [white]="\033[1;37m"
magenta "\033[0;35m" [green_bg]="\033[42m"
cyan "\033[0;36m" [white_on_green]="\033[1;37m\033[42m" # Combined color
white "\033[0;37m" [end]="\033[0m"
bold_black "\033[1;30m" [black]="\033[0;30m"
bold_red "\033[1;31m" [blue]="\033[0;34m"
bold_green "\033[1;32m" [cyan]="\033[0;36m"
bold_yellow "\033[1;33m" [bold_black]="\033[1;30m"
bold_blue "\033[1;34m" [bold_red]="\033[1;31m"
bold_magenta "\033[1;35m" [bold_green]="\033[1;32m"
bold_cyan "\033[1;36m" [bold_blue]="\033[1;34m"
bold_white "\033[1;37m" [bold_magenta]="\033[1;35m"
black_bg "\033[40m" [bold_cyan]="\033[1;36m"
red_bg "\033[41m" [bold_white]="\033[1;37m"
green_bg "\033[42m" [black_bg]="\033[40m"
yellow_bg "\033[43m" [red_bg]="\033[41m"
blue_bg "\033[44m" [yellow_bg]="\033[43m"
magenta_bg "\033[45m" [blue_bg]="\033[44m"
cyan_bg "\033[46m" [magenta_bg]="\033[45m"
white_bg "\033[47m" [cyan_bg]="\033[46m"
end "\033[0m" [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() { 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() { function zrep_version() {
zrep_msg info "\nCreated by kekePower - 2024" zrep_msg info "\nCreated by kekePower - 2024"
zrep_msg info "License: MIT" zrep_msg info "License: MIT"
zrep_msg info "https://git.kekepower.com/kekePower/zrep/" 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 exit
} }
function zrep_msg() { function zrep_msg() {
local color=${colors[end]} # Default to no color if type is unrecognized local msg_type="$1"
case ${1} in local message="$2"
std) color=${colors[green]} ;; local color="${base_colors[end]}" # Default color
info) color=${colors[yellow]} ;;
debug) color=${colors[red]} ;; # Retrieve the color key from the current theme
other) color=${colors[bold_yellow]} ;; local theme_color_key="${current_theme[$msg_type]}"
sub) color=${colors[magenta]} ;;
main) color="${colors[white]} ${colors[green_bg]}" ;; # Check if a valid color was found based on the key
esac if [[ -n "${base_colors[$theme_color_key]}" ]]; then
printf "${color}%b${colors[end]}\n" "${2}" 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() { function zrep_init() {
@ -228,11 +262,11 @@ function zrep_list_installed_packages() {
# Iterate through each author and their 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 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 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) # Check if the package is active (only modify if active)
if grep -q "${package_name}" ${config[main_zrep_install_dir]}/.addons; then 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 fi
zrep_msg info " - ${package_info} ${is_active}" zrep_msg info " - ${package_info} ${is_active}"
@ -403,12 +437,12 @@ function zrep_update_package() {
if [[ -n "$version" ]]; then if [[ -n "$version" ]]; then
local author="${specificPackage%%/*}" local author="${specificPackage%%/*}"
local script="${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..." # echo "Updating $specificPackage to version $version..."
local install_pkg="${author}/${script}" local install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg} zrep_install_package u ${install_pkg}
else else
echo "No update available for ${specificPackage}." zrep_msg info "\nNo update available for ${specificPackage}."
fi fi
else else
if [[ ${updates} == "true" ]]; then if [[ ${updates} == "true" ]]; then
@ -429,6 +463,34 @@ function zrep_update_package() {
fi 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 to install a package by unzipping it to ${config[main_zrep_install_dir]}
function zrep_install_package() { function zrep_install_package() {
@ -456,13 +518,8 @@ function zrep_install_package() {
mkdir -p "${tmpDir}" mkdir -p "${tmpDir}"
local zipFile="${tmpDir}/${author}-${script_name}-${version}.zip" local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
curl -s -o "${zipFile}" "${dlurl}" zrep_download_package "${zipFile}" "${dlurl}"
if [[ $? -ne 0 ]]; then
echo "Error: Failed to download the package."
return 1
fi
unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}" unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}"
@ -630,9 +687,9 @@ function zrep_read_usage() {
function main() { function main() {
zrep_main_version_string zrep_main_version_string
zrep_load_config zrep_load_config
zrep_load_theme ${config[global_theme]}
# Check if the second argument is "help" and the first argument is not empty # Check if the second argument is "help" and the first argument is not empty
if [[ "${2}" == "help" && -n "${1}" ]]; then if [[ "${2}" == "help" && -n "${1}" ]]; then