Skip to content

Instantly share code, notes, and snippets.

@corporatepiyush
Last active August 28, 2024 06:49
Show Gist options
  • Save corporatepiyush/8f769e71c316731a6e2fd82e84537691 to your computer and use it in GitHub Desktop.
Save corporatepiyush/8f769e71c316731a6e2fd82e84537691 to your computer and use it in GitHub Desktop.
Rust based command line primitives
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
# Configuration
APT_PACKAGES="fd-find ripgrep bat exa procs zoxide git-delta hyperfine dust-zsh broot lsd bottom"
SHELL_CONFIG_FILES=("$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.zshrc")
# Logging functions
log_info() {
echo "[INFO] $1"
}
log_error() {
echo "[ERROR] $1" >&2
}
# Package installation
install_packages() {
log_info "Updating package lists..."
sudo apt-get update || { log_error "Failed to update package lists"; exit 1; }
log_info "Installing packages..."
sudo apt-get install -y $APT_PACKAGES || { log_error "Failed to install some packages"; exit 1; }
# Some packages might need manual installation or have different names
if ! command -v fd >/dev/null 2>&1; then
log_info "Installing fd-find..."
sudo apt-get install -y fd-find
sudo ln -s $(which fdfind) /usr/local/bin/fd
fi
if ! command -v bat >/dev/null 2>&1; then
log_info "Installing bat..."
sudo apt-get install -y bat
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat
fi
# Install zellij
if ! command -v zellij >/dev/null 2>&1; then
if command -v cargo >/dev/null 2>&1; then
log_info "Installing zellij..."
cargo install --locked zellij || log_error "Failed to install zellij."
else
log_error "Rust is not installed. Skipping zellij installation."
fi
fi
}
# Detect shell
detect_shell() {
if [ -n "$ZSH_VERSION" ]; then
echo "zsh"
elif [ -n "$BASH_VERSION" ]; then
echo "bash"
else
echo "unknown"
fi
}
# Shell configuration
generate_shell_config() {
local shell_type=$(detect_shell)
cat << EOF
alias find='fd'
alias grep='rg'
alias cat='bat'
alias ls='exa'
alias l='lsd -l'
alias ll='lsd -la'
alias ps='procs'
alias diff='delta'
alias du='dust'
alias tree='broot'
alias top='btm'
alias tmux='zellij'
# Zoxide initialization
eval "\$(zoxide init $shell_type)"
EOF
}
# Function to remove existing aliases
remove_existing_aliases() {
local config_file="$1"
local temp_file=$(mktemp)
# List of aliases to remove
local aliases_to_remove=("find" "grep" "cat" "ls" "l" "ll" "ps" "diff" "du" "tree" "top" "tmux")
# Remove existing aliases
while IFS= read -r line; do
local skip_line=false
for alias_name in "${aliases_to_remove[@]}"; do
if [[ "$line" =~ ^[[:space:]]*alias[[:space:]]+${alias_name}= ]]; then
skip_line=true
log_info "Removed existing alias: $alias_name from $config_file"
break
fi
done
if ! $skip_line; then
echo "$line" >> "$temp_file"
fi
done < "$config_file"
# Replace original file with cleaned version
mv "$temp_file" "$config_file"
}
update_shell_configs() {
local config_content=$(generate_shell_config)
for config_file in "${SHELL_CONFIG_FILES[@]}"; do
if [ ! -f "$config_file" ]; then
touch "$config_file"
fi
# Remove existing aliases
remove_existing_aliases "$config_file"
# Add new configuration
if ! grep -q "# CLI tools aliases and initialization" "$config_file"; then
echo -e "\n# CLI tools aliases and initialization" >> "$config_file"
echo "$config_content" >> "$config_file"
log_info "Updated $config_file with new aliases and zoxide initialization"
else
log_info "$config_file already contains CLI tools configuration. Updating..."
sed -i '/# CLI tools aliases and initialization/,/^$/d' "$config_file"
echo -e "\n# CLI tools aliases and initialization" >> "$config_file"
echo "$config_content" >> "$config_file"
log_info "Updated existing CLI tools configuration in $config_file"
fi
done
}
# Main execution
main() {
install_packages
update_shell_configs
log_info "Installation and configuration complete."
log_info "To apply the changes immediately, run:"
log_info "For Bash: source ~/.bashrc"
log_info "For Zsh: source ~/.zshrc"
log_info "Alternatively, restart your terminal."
log_info "After applying changes, you can use 'z' command for smart directory navigation."
}
main
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
# Configuration
BREW_PACKAGES="coreutils fd ripgrep bat eza procs zoxide git-delta hyperfine dust broot lsd zellij bottom"
SHELL_CONFIG_FILES=("$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.zshrc")
# Logging functions
log_info() {
echo "[INFO] $1"
}
log_error() {
echo "[ERROR] $1" >&2
}
# Homebrew installation
install_homebrew() {
if ! command -v brew >/dev/null 2>&1; then
log_info "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || { log_error "Failed to install Homebrew"; exit 1; }
else
log_info "Homebrew is already installed."
fi
}
# Package installation
install_packages() {
log_info "Installing packages via Homebrew..."
brew update
brew install $BREW_PACKAGES || { log_error "Failed to install some packages"; exit 1; }
}
# Detect shell
detect_shell() {
if [ -n "$ZSH_VERSION" ]; then
echo "zsh"
elif [ -n "$BASH_VERSION" ]; then
echo "bash"
else
echo "unknown"
fi
}
# Shell configuration
generate_shell_config() {
local shell_type=$(detect_shell)
cat << EOF
alias find='fd'
alias grep='rg'
alias cat='bat'
alias ls='eza'
alias l='lsd -l'
alias ll='lsd -la'
alias ps='procs'
alias diff='delta'
alias du='dust'
alias tree='broot'
alias tmux='zellij'
alias top='btm'
# Zoxide initialization
eval "\$(zoxide init $shell_type)"
EOF
}
# Function to remove existing aliases
remove_existing_aliases() {
local config_file="$1"
local temp_file=$(mktemp)
# List of aliases to remove
local aliases_to_remove=("find" "grep" "cat" "ls" "l" "ll" "ps" "diff" "du" "tree" "tmux" "top")
# Remove existing aliases
while IFS= read -r line; do
local skip_line=false
for alias_name in "${aliases_to_remove[@]}"; do
if [[ "$line" =~ ^[[:space:]]*alias[[:space:]]+${alias_name}= ]]; then
skip_line=true
log_info "Removed existing alias: $alias_name from $config_file"
break
fi
done
if ! $skip_line; then
echo "$line" >> "$temp_file"
fi
done < "$config_file"
# Replace original file with cleaned version
mv "$temp_file" "$config_file"
}
update_shell_configs() {
local config_content=$(generate_shell_config)
for config_file in "${SHELL_CONFIG_FILES[@]}"; do
if [ ! -f "$config_file" ]; then
touch "$config_file"
fi
# Remove existing aliases
remove_existing_aliases "$config_file"
# Add new configuration
if ! grep -q "# CLI tools aliases and initialization" "$config_file"; then
echo -e "\n# CLI tools aliases and initialization" >> "$config_file"
echo "$config_content" >> "$config_file"
log_info "Updated $config_file with new aliases and zoxide initialization"
else
log_info "$config_file already contains CLI tools configuration. Updating..."
sed -i.bak '/# CLI tools aliases and initialization/,/^$/d' "$config_file"
echo -e "\n# CLI tools aliases and initialization" >> "$config_file"
echo "$config_content" >> "$config_file"
log_info "Updated existing CLI tools configuration in $config_file"
fi
done
}
# Main execution
main() {
install_homebrew
install_packages
update_shell_configs
log_info "Installation and configuration complete."
log_info "To apply the changes immediately, run:"
log_info "For Bash: source ~/.bashrc"
log_info "For Zsh: source ~/.zshrc"
log_info "Alternatively, restart your terminal."
log_info "After applying changes, you can use 'z' command for smart directory navigation."
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment