# ------------------------------------------------------------
# Mac terminal aliases, from tonyschaffert.com
# https://tonyschaffert.com/blogs/en/mac-terminals-alias/
# Paste into ~/.zshrc, then run: source ~/.zshrc
# ------------------------------------------------------------

# Get around
# Up one folder.
alias ..='cd ..'
# Up two folders.
alias ...='cd ../..'
# Up three folders.
alias ....='cd ../../..'
# Jump to your home folder.
alias home='cd ~'
# Jump to Downloads.
alias dl='cd ~/Downloads'
# Jump to the Desktop.
alias dt='cd ~/Desktop'
# Jump to Documents.
alias docs='cd ~/Documents'

# Files and folders
# List everything, with sizes you can read.
alias ll='ls -lahG'
# Short list that includes hidden files.
alias la='ls -AG'
# List everything, newest first.
alias lt='ls -lahGt'
# Copy, but ask before overwriting.
alias cp='cp -iv'
# Move, but ask before overwriting.
alias mv='mv -iv'
# Create nested folders in one go.
alias mkdir='mkdir -pv'
# Delete every .DS_Store file under the current folder.
alias dsclean="find . -type f -name '.DS_Store' -delete"

# Shell shortcuts
# Clear the screen.
alias c='clear'
# Show your command history.
alias h='history'
# Apply changes to your config without reopening the terminal.
alias reload='source ~/.zshrc'
# Open your config in TextEdit.
alias zshrc='open -e ~/.zshrc'
# Print your PATH, one entry per line.
alias path='echo $PATH | tr ":" "\n"'

# Only on a Mac
# Open the current folder in Finder.
alias o='open .'
# Copy the current folder path to the clipboard.
alias cpwd='pwd | tr -d "\n" | pbcopy'
# Show hidden files in Finder.
alias showfiles='defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder'
# Hide them again.
alias hidefiles='defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder'
# Put the display to sleep right now.
alias afk='pmset displaysleepnow'
# Keep the Mac awake until you press Control C.
alias awake='caffeinate -d'
# Battery level and time remaining.
alias battery='pmset -g batt'

# Network
# Your public IP address.
alias myip='curl -s https://api.ipify.org && echo'
# Your IP on the local network (Wi-Fi on most Macs).
alias localip='ipconfig getifaddr en0'
# See which apps are listening on which ports.
alias ports='lsof -iTCP -sTCP:LISTEN -n -P'
# Clear the DNS cache. Asks for your password.
alias flushdns='sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder'
# Internet speed test built into macOS 12 and later.
alias speedtest='networkQuality'

# Git
# Short status with the branch name.
alias gs='git status -sb'
# Stage a file.
alias ga='git add'
# Stage everything.
alias gaa='git add -A'
# Commit with a message: gc "fix typo".
alias gc='git commit -m'
# Push.
alias gp='git push'
# Pull.
alias gl='git pull'
# Switch branch.
alias gco='git checkout'
# List branches.
alias gb='git branch'
# See what changed.
alias gd='git diff'
# The last 15 commits as a compact graph.
alias glog='git log --oneline --graph --decorate -15'
# Undo the last commit and keep your changes.
alias undo='git reset --soft HEAD~1'

# Power tools
# These need Homebrew. The second one also needs fzf: brew install fzf.
# Update Homebrew and everything installed with it.
alias brewup='brew update && brew upgrade && brew cleanup'
# Pick a recent branch from a searchable list and switch to it.
alias cbr='git branch --sort=-committerdate --format="%(refname:short)" | fzf --header "Checkout recent branch" --preview "git diff {1}" | xargs git checkout'
