Update zrep script with color definitions using associative array and refactor color usage in functions. Add functionality to display package-specific help from USAGE file. - Wed, 06 Mar 2024 18:33:14 +0100

This commit is contained in:
Stig-Ørjan Smelror 2024-03-06 18:33:14 +01:00
parent e3ee9e49a9
commit 5f7c437701

121
zrep
View File

@ -30,59 +30,58 @@ zrep_fpath ${HOME}/.zrep/functions
autoload -Uz zini
# List of colors available
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 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"
)
function zrep_main_version_string() {
echo "${magenta}${blue_bg} ${ZREP} ${end}${bold_white}${blue_bg}${VERSION} ${end}"
echo "${colors[bold_black]}${colors[white_bg]} ${ZREP} ${colors[end]}${colors[white]}${colors[black_bg]} ${VERSION} ${colors[end]}"
}
function zrep_version() {
zrep_msg info "Created by kekePower - 2024"
zrep_msg info "- https://git.kekepower.com/kekePower/zrep/${end}"
zrep_msg info "Please see '${ZSH_SCRIPT:t} help' for more info"
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[green]}${ZSH_SCRIPT:t} help${colors[end]}'${colors[yellow]} for more info"
exit
}
function zrep_msg() {
# Determine the color based on the type
# local color="${end}" # Default to no color if type is unrecognized
local color=${colors[end]} # Default to no color if type is unrecognized
case ${1} in
std) color="${green}" ;;
info) color="${yellow}" ;;
debug) color="${red}" ;;
other) color="${bold_yellow}" ;;
sub) color="${magenta}" ;;
main) color="${white} ${green_bg}" ;;
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
# echo "1=$1\n2=$2\ncolor=$color"
printf "${color}%b${end}\n" "${2}"
printf "${color}%b${colors[end]}\n" "${2}"
}
function zrep_init() {
@ -172,7 +171,7 @@ function zrep_update_installed_json() {
.[$author] = [{"script": $script, "version": $version}]
end' "$json_file" > "$json_file.tmp" && mv "$json_file.tmp" "$json_file"
echo "Package '$script' by '$author' version $version installed successfully."
zrep_msg info "Package '$script' by '$author' version $version installed successfully."
}
# Function to list installed packages from installed.json
@ -181,7 +180,7 @@ function zrep_list_installed_packages() {
# Check if installed.json exists
if [[ ! -f "$installed_json" ]]; then
echo "No installed packages found."
zrep_msg debug "No installed packages found."
return 0
fi
@ -191,11 +190,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="${white}(${end}${red}Inactive${end}${white})${end}" # Set default to Inactive
local is_active="${colors[white]}(${colors[end]}${colors[red]}Inactive${colors[end]}${colors[white]})${colors[end]}" # Set default to Inactive
# Check if the package is active (only modify if active)
if grep -q "$package_name" ~/.zrep_addons; then
is_active="${white}(${end}${bold_green}Active${end}${white})${end}"
is_active="${colors[white]}(${colors[end]}${colors[bold_green]}Active${colors[end]}${colors[white]})${colors[end]}"
fi
zrep_msg other " - $package_info $is_active"
@ -506,7 +505,7 @@ function zrep_disable() {
# Help function to display available options
function zrep_help() {
zrep_msg sub "Usage: zrep <command> [arguments]"
zrep_msg sub "\nUsage: zrep <command> [arguments]"
zrep_msg other "Available commands:"
zrep_msg other " init: Initialize zrep"
zrep_msg other " install (i) <author/package>: Install a package"
@ -516,15 +515,43 @@ function zrep_help() {
zrep_msg other " disable <author/package>: Disable zrep package"
zrep_msg other " version: Display zrep version"
zrep_msg other " list: List installed packages"
zrep_msg other " <author/package> help: Display help for pacakage"
}
function zrep_read_usage() {
local package_name="$1"
# Parse the package name to extract author and script
local author="${package_name%/*}"
local script="${package_name#*/}"
local first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]')
# Construct the path to the USAGE file
local usage_file="${config[main_zrep_install_dir]}/$first_letter/$author/$script/USAGE"
# Check if the USAGE file exists
if [[ -f "$usage_file" ]]; then
# Display the content of the USAGE file
zrep_msg sub "\n$package_name:"
local usage_buffer=$(<${usage_file})
zrep_msg other "$usage_buffer"
else
zrep_msg debug "No USAGE file found for package '$package_name'."
fi
}
function main() {
zrep_main_version_string
load_config
# Check if the second argument is "help" and the first argument is not empty
if [[ "${2}" == "help" && -n "${1}" ]]; then
zrep_read_usage "${1}"
exit
fi
# Example command handling structure
case "${1}" in
init)