Initial commit of zrep

This commit is contained in:
Stig-Ørjan Smelror 2024-02-27 16:50:07 +01:00
parent 08e19f9abf
commit 41f0709722
3 changed files with 142 additions and 0 deletions

16
functions/zrep_fpath Normal file
View File

@ -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
}

43
functions/zrep_init Normal file
View File

@ -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."
}

83
zrep Executable file
View File

@ -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 <command> [arguments]"
echo "Available commands: init, install, update"
;;
esac
}
# Call main with all passed arguments
main "$@"
#echo "END: $fpath"