# -*- mode: sh -*-

# only use aliases that is cross platform between linux and BSD.

# Emacs
export EDITOR="emacs -nw"
alias e='emacs -nw'

# ssh with color
alias ssh='TERM=xterm-256color ssh'

alias ll='ls -lah'
# git shortcuts
alias gst='git status'
alias gco='git checkout'
alias glg='git lg'
alias gci='git commit'
alias gad='git add'
alias gbr='git branch'
alias gps='git push'
alias gpl='git pull'
alias gmg='git merge'
alias gdf='git diff'

alias blaze='bazel'

alias k='kubectl'

alias ta='tmux a'
alias tn='tmux new'

alias assh='autossh -M -0'
# AutoSSH + tmux
function smux {
  autossh -M -0 $@ -t -- "tmux attach || tmux new"
}

function hide_desktop {
    defaults write com.apple.finder CreateDesktop false && Killall Finder
}

function show_desktop {
    defaults write com.apple.finder CreateDesktop true && Killall Finder
}

function activate_venv {
  # find .venv in the current directory and parent directories
  local dir="$PWD"
  while [[ "$dir" != "/" ]]; do
    if [[ -d "$dir/.venv" ]]; then
      # Use printf and read for better zsh compatibility
      printf "Found venv in %s. Activate it? [y/N] " "$dir"
      read response
      if [[ "$response" =~ ^[Yy]$ ]]; then
        source "$dir/.venv/bin/activate"
        echo "Activated venv in $dir"
        return 0
      else
        echo "Skipped activating venv"
        # Move to parent directory and continue searching
      fi
    fi
    dir="$(dirname "$dir")"
  done
  echo "No .venv directory found in current or parent directories"
  return 1
}

keep_running () {
  until "$@"; do
    printf 'keep_running: "%s" failed (exit %d) – retrying in 2s...\n' "$*" "$?"
    sleep 2
  done
}
