|
#!/bin/bash |
|
|
|
# mkcd installer - adds mkcd function to your shell |
|
|
|
set -e |
|
|
|
# Colors for output |
|
RED='\033[0;31m' |
|
GREEN='\033[0;32m' |
|
YELLOW='\033[1;33m' |
|
NC='\033[0m' # No Color |
|
|
|
# Detect shell |
|
detect_shell() { |
|
if [ -n "$ZSH_VERSION" ]; then |
|
echo "zsh" |
|
elif [ -n "$BASH_VERSION" ]; then |
|
echo "bash" |
|
elif [ -n "$FISH_VERSION" ]; then |
|
echo "fish" |
|
else |
|
# Fallback to checking $SHELL |
|
case "$SHELL" in |
|
*/zsh) echo "zsh" ;; |
|
*/bash) echo "bash" ;; |
|
*/fish) echo "fish" ;; |
|
*) echo "unknown" ;; |
|
esac |
|
fi |
|
} |
|
|
|
# Get config file for shell |
|
get_config_file() { |
|
local shell_type="$1" |
|
case "$shell_type" in |
|
bash) |
|
if [ -f "$HOME/.bashrc" ]; then |
|
echo "$HOME/.bashrc" |
|
else |
|
echo "$HOME/.bash_profile" |
|
fi |
|
;; |
|
zsh) |
|
echo "$HOME/.zshrc" |
|
;; |
|
fish) |
|
echo "$HOME/.config/fish/config.fish" |
|
;; |
|
*) |
|
echo "" |
|
;; |
|
esac |
|
} |
|
|
|
# Generate function for shell |
|
generate_function() { |
|
local shell_type="$1" |
|
case "$shell_type" in |
|
bash|zsh) |
|
cat << 'EOF' |
|
|
|
# mkcd - create directory and cd into it |
|
mkcd() { |
|
if [ $# -eq 0 ]; then |
|
echo "Usage: mkcd <directory>" |
|
return 1 |
|
fi |
|
mkdir -p "$1" && cd "$1" |
|
} |
|
EOF |
|
;; |
|
fish) |
|
cat << 'EOF' |
|
|
|
# mkcd - create directory and cd into it |
|
function mkcd |
|
if test (count $argv) -eq 0 |
|
echo "Usage: mkcd <directory>" |
|
return 1 |
|
end |
|
mkdir -p $argv[1] && cd $argv[1] |
|
end |
|
EOF |
|
;; |
|
esac |
|
} |
|
|
|
main() { |
|
echo -e "${GREEN}Installing mkcd function...${NC}" |
|
|
|
local shell_type=$(detect_shell) |
|
echo "Detected shell: $shell_type" |
|
|
|
if [ "$shell_type" = "unknown" ]; then |
|
echo -e "${RED}Error: Unsupported shell${NC}" |
|
echo "This installer supports bash, zsh, and fish" |
|
exit 1 |
|
fi |
|
|
|
local config_file=$(get_config_file "$shell_type") |
|
|
|
if [ -z "$config_file" ]; then |
|
echo -e "${RED}Error: Could not determine config file${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Create config directory if it doesn't exist (for fish) |
|
if [ "$shell_type" = "fish" ]; then |
|
mkdir -p "$(dirname "$config_file")" |
|
fi |
|
|
|
# Check if mkcd already exists |
|
if grep -q "mkcd()" "$config_file" 2>/dev/null || grep -q "function mkcd" "$config_file" 2>/dev/null; then |
|
echo -e "${YELLOW}mkcd function already exists in $config_file${NC}" |
|
echo "Remove it manually if you want to reinstall" |
|
exit 0 |
|
fi |
|
|
|
# Add function to config file |
|
echo "Adding mkcd function to $config_file" |
|
generate_function "$shell_type" >> "$config_file" |
|
|
|
echo -e "${GREEN}✓ mkcd function installed successfully!${NC}" |
|
echo |
|
echo "To use it immediately, run:" |
|
echo " source $config_file" |
|
echo |
|
echo "Or restart your terminal." |
|
echo |
|
echo "Usage: mkcd <directory>" |
|
} |
|
|
|
main "$@" |