diff --git a/bash/bashrc b/bash/bashrc index 1f6ef57e..1abd3f05 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -3,6 +3,9 @@ ## errol@sancaktar.net ## +# If not running interactively, don't do anything and return early +[[ $- == *i* ]] || return + # make less more friendly for non-text input files, see lesspipe(1) [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" @@ -168,3 +171,6 @@ for bcfile in ~/.bash_completion.d/*; do fi done + +# Set the Hi status to be displayed as part of the prompt. #!>>HI< /dev/null && __hi_prompt_large)${PS1}" #!>>HI</dev/null; then - brew install --cask --force $package - else - brew install --force $package - fi - fi - done -} - -brew_packages=( - "opentofu" - "docker" - "docker-completion" - "helm" - "ipython" - "jq" - "k9s" - "kubernetes-cli" - "neovim" - "nmap" - "stow" - "terragrunt" - "tmux" - "font-hack-nerd-font" - "alacritty" - "spotify" - "tailscale" - "visual-studio-code" - "fzf" - "ripgrep" - "fd" - "spotify_player" -) - - - -function alacritty_config_setup { - if [[ -f $HOME/.config/alacritty/alacritty-base.toml ]] - then - # echo $(pwd) - toml_files=("$HOME/.config/alacritty/alacritty-base.toml" "$HOME/.config/alacritty/alacritty-$1.toml") - combined_file="$HOME/.config/alacritty/alacritty.toml" - echo -n > "$combined_file" - for file in "${toml_files[@]}"; do - # Check if the file exists - if [ -f "$file" ]; then - # Append content of the current TOML file to the combined file - cat "$file" >> "$combined_file" - # Add a newline to separate TOML sections (optional) - echo >> "$combined_file" - else - echo "File '$file' not found. Skipping." - fi - done - fi -} -function shell_start_disp { - echo -n "$(hostname) - " - if [[ -f /sbin/ifconfig ]] - then - ifconfig | awk '/inet / && !/inet6/ && !/127.0.0.1/ {ips = ips " " $2} END {print ips}' - fi -} - - -## Check for HomeBrew -if [[ -f "/opt/homebrew/bin/brew" ]] then - eval "$(/opt/homebrew/bin/brew shellenv)" - export HOMEBREW_NO_ENV_HINTS=1 - ensure_brew_packages $brew_packages -fi - -# IF Linux -if [[ `uname` == "Linux" ]] -then - [[ ":$PATH:" != *":/home/errol/Applications:"* ]] && PATH="/home/errol/Applications:${PATH}" - # The next line updates PATH for the Google Cloud SDK. - if [ -f '/home/errol/google-cloud-sdk/path.zsh.inc' ]; then . '/home/errol/google-cloud-sdk/path.zsh.inc'; fi - - # The next line enables shell command completion for gcloud. - if [ -f '/home/errol/google-cloud-sdk/completion.zsh.inc' ]; then . '/home/errol/google-cloud-sdk/completion.zsh.inc'; fi - export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" - - # Alacritty Crap - alacritty_config_setup linux -fi - -## IF MacOS -if [[ $(uname) == "Darwin" ]] -then - - # Alacritty Crap - alacritty_config_setup osx -fi - -shell_start_disp +setopt EXTENDED_HISTORY APPEND_HISTORY SHARE_HISTORY HIST_IGNORE_SPACE +setopt HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS HIST_IGNORE_DUPS HIST_FIND_NO_DUPS +setopt AUTOCD # Keybindings bindkey -e @@ -122,77 +13,145 @@ bindkey '^p' history-search-backward bindkey '^n' history-search-forward bindkey '^[w' kill-region -# Alias -if [[ -r ~/.aliasrc ]]; then - . ~/.aliasrc -fi - -# History -HISTSIZE=5000000 -HISTFILE=~/.zsh_history -SAVEHIST=$HISTSIZE - -setopt EXTENDED_HISTORY -setopt APPEND_HISTORY -setopt SHARE_HISTORY -setopt HIST_IGNORE_SPACE -setopt HIST_IGNORE_ALL_DUPS -setopt HIST_SAVE_NO_DUPS -setopt HIST_IGNORE_DUPS -setopt HIST_FIND_NO_DUPS - -# Fuzzy search -function fh() { - eval $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//') -} - -# history with search -function h() { - # check if we passed any parameters - if [ -z "$*" ]; then - # if no parameters were passed print entire history - history 1 - else - # if words were passed use it as a search - history 1 | egrep --color=auto "$@" - fi -} -# Auto CD -setopt AUTOCD - # Completion styling zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}' zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" zstyle ':completion:*' menu no zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath' zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'ls --color $realpath' -zstyle ':completion:*' range 1000:100 # Try 100 history words at a time; max 1000 words. +zstyle ':completion:*' range 1000:100 +# Path Management (Linux specific from your original file) +# This assumes your main environment is Linux given the path elements. +# If this is a dual-boot or you frequently switch OS, +# you might need to adjust the PATH logic further. +if [[ $(uname) == "Linux" ]]; then + # Ensure /usr/local/bin is at the front + PATH="/usr/local/bin:$PATH" -# Check Starship is installed -if which starship &> /dev/null -then + # Google Cloud SDK PATH and completion + if [ -f '/home/errol/google-cloud-sdk/path.zsh.inc' ]; then + . '/home/errol/google-cloud-sdk/path.zsh.inc' + fi + if [ -f '/home/errol/google-cloud-sdk/completion.zsh.inc' ]; then + . '/home/errol/google-cloud-sdk/completion.zsh.inc' + fi + + # Krew (Kubernetes plugin manager) + export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" + + # Google Application Credentials + export GOOGLE_APPLICATION_CREDENTIALS=/Users/errol/.config/gcloud/application_default_credentials.json + + # Alacritty config setup + alacritty_config_setup linux +fi + +# Homebrew setup (for macOS and Linuxbrew) +if [[ -f "/opt/homebrew/bin/brew" ]]; then + eval "$(/opt/homebrew/bin/brew shellenv)" + export HOMEBREW_NO_ENV_HINTS=1 + # Function to ensure Homebrew packages are installed + function ensure_brew_packages { + local packages=("$@") + local cellar_dir="/opt/homebrew/Cellar" + local caskroom_dir="/opt/homebrew/Caskroom" + for package in "${packages[@]}"; do + if ! [[ -d "$cellar_dir/$package" || -d "$caskroom_dir/$package" ]]; then + echo "Installing $package..." + if brew info --cask "$package" &>/dev/null; then + brew install --cask --force "$package" + else + brew install --force "$package" + fi + fi + done + } + # List of Homebrew packages to ensure are installed + brew_packages=( + "opentofu" "docker" "docker-completion" "helm" "ipython" "jq" "k9s" + "kubernetes-cli" "neovim" "nmap" "stow" "terragrunt" "tmux" "font-hack-nerd-font" + "alacritty" "spotify" "tailscale" "visual-studio-code" "fzf" "ripgrep" + "fd" "spotify_player" + ) + ensure_brew_packages "${brew_packages[@]}" +fi + +# Alacritty config setup (for macOS) +if [[ $(uname) == "Darwin" ]]; then + alacritty_config_setup osx +fi + +# Shell start display +function shell_start_disp { + echo -n "$(hostname) - " + if command -v ifconfig &>/dev/null; then # Use command -v for portability + ifconfig | awk '/inet / && !/inet6/ && !/127.0.0.1/ {ips = ips " " $2} END {print ips}' + elif command -v ip &>/dev/null; then # Fallback for systems without ifconfig + ip -4 addr show | awk '/inet / && !/127.0.0.1/ {print $2}' | cut -d/ -f1 | tr '\n' ' ' + fi + echo # Add a newline to separate prompt from output +} +shell_start_disp + +# Load aliases and Google specific configurations +if [[ -r ~/.aliasrc ]]; then + . ~/.aliasrc +fi +if [[ -r ~/.google_rc ]]; then + . ~/.google_rc +fi + +# Fuzzy search history +function fh() { + eval $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//') +} + +# History search function (fixes the grep issue by using \grep) +function h() { + if [ -z "$*" ]; then + history 1 + else + history 1 | \grep --color=auto "$@" # Use \grep to avoid alias recursion + fi +} + +# Starship prompt +if command -v starship &> /dev/null; then eval "$(starship init zsh)" else + # Simplified installation, assumes curl is available and interactive script is okay + # You might want to make this non-interactive or pre-download in a production setup curl -sS https://starship.rs/install.sh | sh fi -# Created by `pipx` on 2024-07-16 03:28:59 -export PATH="$PATH:/Users/errol/.local/bin" -if [[ ! -d ~/.tmux/plugins/tpm ]] -then +# tmux plugin manager +if [[ ! -d ~/.tmux/plugins/tpm ]]; then git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm fi -# The next line updates PATH for the Google Cloud SDK. -if [ -f '/Users/errol/Downloads/google-cloud-sdk/path.zsh.inc' ]; then . '/Users/errol/Downloads/google-cloud-sdk/path.zsh.inc'; fi - -# The next line enables shell command completion for gcloud. -if [ -f '/Users/errol/Downloads/google-cloud-sdk/completion.zsh.inc' ]; then . '/Users/errol/Downloads/google-cloud-sdk/completion.zsh.inc'; fi - -export GOOGLE_APPLICATION_CREDENTIALS=/Users/errol/.config/gcloud/application_default_credentials.json +# Gam alias alias gam="/home/errol/bin/gamadv-xtd3/gam" -if which kubectl &> /dev/null -then + +# Kubectl completion +if command -v kubectl &> /dev/null; then source <(kubectl completion zsh) fi + +# --- Helper Functions (defined at the end for clarity) --- +# Function to setup Alacritty configuration +function alacritty_config_setup { + if [[ -f "$HOME/.config/alacritty/alacritty-base.toml" ]]; then + local toml_files=("$HOME/.config/alacritty/alacritty-base.toml" "$HOME/.config/alacritty/alacritty-$1.toml") + local combined_file="$HOME/.config/alacritty/alacritty.toml" + echo -n > "$combined_file" + for file in "${toml_files[@]}"; do + if [ -f "$file" ]; then + cat "$file" >> "$combined_file" + echo >> "$combined_file" # Add newline + else + echo "File '$file' not found. Skipping." + fi + done + fi +}