From 40adc05b9b5d7bf22df954050f676538967f915d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig-=C3=98rjan=20Smelror?= Date: Wed, 13 Mar 2024 22:52:47 +0100 Subject: [PATCH] Update zrep script: - Changed comment to define default path for .zreprc - Updated comment to define list of colors available for themes - Removed clearing of previous theme settings - Added function to check if a string exists in ~/.zshrc - Added function to check for required dependencies - Updated zrep_init function to include checking for dependencies - Updated zrep_init function to create default file and directory structure - Updated zrep_init function to check for existing zini path in .zshrc - Updated zrep_init function to check for existing source command for .zrep_addons in .zshrc - Updated zrep_init function to create or update .zrep_addons file - Updated zrep_check_for_updates function to display message if no updates found - Removed redundant message in zrep_update_package function - Updated zrep_download_package function to display download attempt message - Updated zrep_download_package function to display download successful message - Updated zrep_install_package function to include fetching package information - Removed commented out line in zrep_install_package function - Removed commented out line in zrep_parse_package_name function --- zrep | 87 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 29 deletions(-) diff --git a/zrep b/zrep index 70984c2..53e4aac 100755 --- a/zrep +++ b/zrep @@ -4,7 +4,7 @@ setopt extendedglob VERSION="0.0.2" # Sat-2024-03-13 ZREP="Zsh Repository Tool" -# Define the path to .zreprc +# Define the default path to .zreprc ZREP_CONFIG="${HOME}/.zreprc" function zrep_fpath() { @@ -27,7 +27,7 @@ function zrep_fpath() { zrep_fpath ${HOME}/.zrep/functions autoload -Uz zini -# List of colors available +# Define a list of colors available to use in themes typeset -A base_colors=( [green]="\033[0;32m" [yellow]="\033[1;33m" @@ -71,10 +71,6 @@ zrep_load_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" @@ -82,7 +78,6 @@ zrep_load_theme() { for key value in ${(kv)theme_colors}; do current_theme[$key]="$value" done - # print -l ${(kv)current_theme} } function zrep_main_version_string() { @@ -97,10 +92,12 @@ function zrep_version() { exit } +# This function is used to display messages and use colors +# from the loaded theme. function zrep_msg() { local msg_type="$1" local message="$2" - local color="${base_colors[end]}" # Default color + local color="${base_colors[end]}" # Default color is NONE # Retrieve the color key from the current theme local theme_color_key="${current_theme[$msg_type]}" @@ -116,11 +113,44 @@ function zrep_msg() { printf "%b\n" "${color}${message}${base_colors[end]}" } +# Function to check if a given string exists in ~/.zshrc +function zrep_find_string() { + local searchString="$1" + local found=0 + + while IFS= read -r line || [[ -n $line ]]; do + if [[ "$line" == *"$searchString"* ]]; then + found=1 + break + fi + done < "${HOME}/.zshrc" + + echo $found +} + +function zrep_check_for_deps() { + # Array of required external programs + local required_programs=('jq') + + # Iterate over the array + for program in "${required_programs[@]}"; do + # Check if the program is available in the system's PATH + if ! command -v "$program" &> /dev/null; then + # Program not found, display a message + echo "Required program not found: $program" + fi + done +} + +# The first step after downloading zrep. +# Here we create the default file and directory structure function zrep_init() { local zshrc_file="${HOME}/.zshrc" - local addons_file="${HOME}/.zrep_addons" + local zrep_addons="${HOME}/.zrep_addons" local install_dir + zrep_check_for_deps + # Check if .zreprc exists if [[ ! -f ${ZREP_CONFIG} ]]; then echo "${ZREP_CONFIG} not found. Creating it..." @@ -130,11 +160,12 @@ function zrep_init() { # Write to .zreprc cat > "${ZREP_CONFIG}" <> ${zshrc_file} fi # Check if .zshrc already sources .zrep_addons, if not, add it - if ! grep -q "source ${addons_file}" "${zshrc_file}"; then + if [[ $(zrep_find_string "source ${zrep_addons}") -eq 0 ]]; then echo "Adding source command for .zrep_addons to .zshrc..." - echo "source ${addons_file}" >> "${zshrc_file}" + echo "source ${zrep_addons}" >> "${zshrc_file}" fi # Create or update the .zrep_addons file - if [[ ! -f ${addons_file} ]]; then - echo "Creating file ${addons_file}..." - cat > "${addons_file}" < "${zrep_addons}" <