From 41f0709722b62897071030c8cb16b8dea657a0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig-=C3=98rjan=20Smelror?= Date: Tue, 27 Feb 2024 16:50:07 +0100 Subject: [PATCH] Initial commit of zrep --- functions/zrep_fpath | 16 +++++++++ functions/zrep_init | 43 +++++++++++++++++++++++ zrep | 83 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 functions/zrep_fpath create mode 100644 functions/zrep_init create mode 100755 zrep diff --git a/functions/zrep_fpath b/functions/zrep_fpath new file mode 100644 index 0000000..54b2069 --- /dev/null +++ b/functions/zrep_fpath @@ -0,0 +1,16 @@ +function zrep_fpath() { + local base_dir="$1" + + # Check if the base directory exists + if [[ ! -d "$base_dir" ]]; then + echo "Error: Base directory '$base_dir' does not exist." + return 1 + fi + + # Add directories containing at least one file to fpath + for dir in $base_dir/**/*(/N); do + if [[ -n $(ls -A "$dir") ]]; then + fpath=($dir $fpath) + fi + done +} diff --git a/functions/zrep_init b/functions/zrep_init new file mode 100644 index 0000000..bd73f4b --- /dev/null +++ b/functions/zrep_init @@ -0,0 +1,43 @@ +autoload -Uz zini +echo "zrep_init: FPATH: $fpath" +# Initialize zrep +zrep_init() { + local config_file="$HOME/.zreprc" + local zshrc_file="$HOME/.zshrc" + local install_dir + + # Check if .zreprc exists + if [[ ! -f $config_file ]]; then + echo "$config_file not found. Creating it..." + # Prompt user for install directory + read "?Enter zrep installation directory [$HOME/.zrep]: " install_dir + install_dir=${install_dir:-$HOME/.zrep} + + # Write to .zreprc + echo "[main]" > $config_file + echo "zrep_install_dir = $install_dir" >> $config_file + else + echo "Running zini $config_file" + zini $config_file + echo "Setting install_dir" + install_dir=${config[zrep_install_dir]} + echo "install_dir=${install_dir}" + fi + + # Ensure zrep_install_dir exists + mkdir -p "$install_dir" + + # Update or add fpath in .zshrc, ensuring no duplicate or empty entries + if ! grep -q "fpath=(.*$install_dir)" "$zshrc_file"; then + echo "Adding zrep installation directory to fpath in .zshrc..." + echo "fpath=('$install_dir' \$fpath)" >> "$zshrc_file" + if ! grep -q "^export fpath" "$zshrc_file"; then + echo "export fpath" >> "$zshrc_file" + fi + else + echo "zrep installation directory ($install_dir) is already included in fpath." + fi + + source ${zshrc_file} + echo "zrep initialization complete." +} diff --git a/zrep b/zrep new file mode 100755 index 0000000..dada488 --- /dev/null +++ b/zrep @@ -0,0 +1,83 @@ +#!/usr/local/bin/zsh + +function zrep_fpath() { + local base_dir="$1" + + # Check if the base directory exists + if [[ ! -d "$base_dir" ]]; then + echo "Error: Base directory '$base_dir' does not exist." + return 1 + fi + + # Add directories containing at least one file to fpath + for dir in $base_dir/**/*(/N); do + if [[ -n $(ls -A "$dir") ]]; then + fpath=($dir $fpath) + fi + done +} + +#echo "BEGIN: $fpath" + +zrep_fpath ${HOME}/bin/include + +autoload -Uz zini + +# Define the path to .zreprc +ZREP_CONFIG="$HOME/.zreprc" + +# Function to load configuration +function load_config() { +autoload -Uz zrep_init + if [[ -f "$ZREP_CONFIG" ]]; then + # echo ".zreprc found, loading configuration..." + zini "$ZREP_CONFIG" + zrep_fpath ${config[main_zrep_install_dir]} + else + if [[ "$1" == "init" ]]; then + echo "$ZREP_CONFIG not found. Proceeding with 'zrep init'..." + zrep_init + else + echo "$ZREP_CONFIG not found." + # Ask the user if they want to run 'zrep init' + read "response?Would you like to run 'zrep init' to set up? (y/n): " + if [[ "$response" =~ ^[Yy]$ ]]; then + zrep_init + else + echo "Initialization canceled. Please run 'zrep init' manually to set up." + exit 1 + fi + fi + fi +} + +# Main script logic +main() { + # Load configuration + load_config + + # Example command handling structure + case "$1" in + init) + autoload -Uz zrep_init + zrep_init + zrep_fpath ${config[main_zrep_install_dir]} + exit + ;; + install) + echo "Install function here" + ;; + update) + echo "Update function here" + ;; + *) + echo "Usage: zrep [arguments]" + echo "Available commands: init, install, update" + ;; + esac +} + +# Call main with all passed arguments +main "$@" + +#echo "END: $fpath"