major refactor

This commit is contained in:
2026-06-19 23:42:18 +05:30
parent a786af12c9
commit e9fbd08423
12 changed files with 621 additions and 356 deletions

View File

@@ -2,16 +2,8 @@
#
# Zoxide Installer Script
#
# What this script does:
# 1. Detects the Linux distribution.
# 2. Checks whether zoxide is already installed.
# 3. Prompts before installing or upgrading zoxide.
# 4. Downloads and runs the official zoxide installer script.
# 5. Configures shell configuration files (~/.bashrc / ~/.zshrc) to initialize zoxide.
# 6. Checks if fzf is installed; if not, installs it using the system package manager.
#
# Run metascript to check if the shell is bash
# Run metascript to check if the shell is bash and load libraries
PARENT_DIR="$(dirname "$0")/.."
METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh"
METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh"
@@ -31,94 +23,61 @@ fi
set -euo pipefail
confirm() {
local prompt="$1"
local response
read -r -p "$prompt [y/N]: " response </dev/tty || true
[[ "$response" =~ ^[Yy]$ ]]
}
install_curl() {
if ! command -v curl >/dev/null 2>&1; then
echo "curl not found. Installing curl..."
if command -v pacman >/dev/null 2>&1; then
sudo pacman -Sy --needed curl
elif command -v apt >/dev/null 2>&1; then
sudo apt update
sudo apt install -y curl
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y curl
fi
if ! has_command curl; then
log_info "curl not found. Installing curl..."
pkg_install curl
fi
}
install_fzf() {
if command -v fzf >/dev/null 2>&1; then
echo "fzf is already installed."
if has_command fzf; then
log_info "fzf is already installed."
return
fi
echo "fzf not found. Installing fzf..."
if command -v pacman >/dev/null 2>&1; then
sudo pacman -Sy --needed fzf
elif command -v apt >/dev/null 2>&1; then
sudo apt update
sudo apt install -y fzf
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y fzf
else
echo "Warning: Unsupported distribution. Please install fzf manually." >&2
fi
log_info "fzf not found. Installing fzf..."
pkg_install fzf
}
install_zoxide() {
if command -v zoxide >/dev/null 2>&1 || [ -f "$HOME/.local/bin/zoxide" ]; then
if has_command zoxide || [ -f "$HOME/.local/bin/zoxide" ]; then
if ! confirm "Zoxide is already installed. Reinstall/Upgrade?"; then
echo "Skipping Zoxide installation."
log_info "Skipping Zoxide installation."
return
fi
else
if ! confirm "Install Zoxide?"; then
echo "Skipping Zoxide installation."
log_info "Skipping Zoxide installation."
return
fi
fi
install_curl
echo "Downloading and running the official zoxide installer..."
log_info "Downloading and running the official zoxide installer..."
curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh
}
configure_shell() {
local target_files=()
[ -f "$HOME/.bashrc" ] && target_files+=("$HOME/.bashrc")
[ -f "$HOME/.zshrc" ] && target_files+=("$HOME/.zshrc")
# Add ~/.local/bin to PATH for the current process
export PATH="$HOME/.local/bin:$PATH"
for config_file in "${target_files[@]}"; do
# Clean up old block if it exists
if grep -q "# >>> zoxide init >>>" "$config_file" 2>/dev/null; then
sed -i '/# >>> zoxide init >>>/,/# <<< zoxide init <<</d' "$config_file"
fi
IFS=' ' read -ra target_files <<< "$(get_shell_configs)"
for config_file in "${target_files[@]}"; do
local shell_name="bash"
if [[ "$config_file" == *".zshrc" ]]; then
shell_name="zsh"
fi
echo "Adding zoxide initialization to $config_file..."
cat << EOF >> "$config_file"
log_info "Adding zoxide initialization to $config_file..."
local content
content="eval \"\$(zoxide init --cmd cd $shell_name)\""
inject_block "$config_file" "zoxide init" "$content"
# >>> zoxide init >>>
eval "\$(zoxide init --cmd cd $shell_name)"
# <<< zoxide init <<<
EOF
# Source if modified (only for bashrc currently)
# Source if modified (only for bashrc)
if [ "$config_file" = "$HOME/.bashrc" ]; then
. "$config_file" 2>/dev/null || true
fi
@@ -131,7 +90,7 @@ main() {
install_fzf
echo
echo "Zoxide installation and configuration complete."
log_success "Zoxide installation and configuration complete."
}
main "$@"