Skip to content

Instantly share code, notes, and snippets.

@sneils
Last active November 25, 2021 15:50
Show Gist options
  • Save sneils/1685eacb56cc851944707bea2cb51e19 to your computer and use it in GitHub Desktop.
Save sneils/1685eacb56cc851944707bea2cb51e19 to your computer and use it in GitHub Desktop.
zsh
#!/usr/bin/env zsh
alias lx='ls -lhAF --group-directories-first --color'
alias dm="docker-machine"
alias dc="docker-compose"
alias gb="git --no-pager branch"
alias go='docker run --rm -it -w /go/src -v "${PWD}":/go/src -v /tmp/gopkg:/go/pkg golang:1.13 go'
alias cargo='docker run --rm -it -w /app -v "${PWD}":/app -e USER=root rust:1.40 cargo'
#!/usr/bin/env zsh
delete_local_only_branches() {
git branch | grep -v "^[*]" | grep -v -E "ma(ster|in)$" | xargs -n1 git branch -D
}
clone() {
git clone "[email protected]:${1:?}" "${2:-"$(basename "$1")"}"
}
makepr() {
local repo="$(git rev-parse --show-toplevel | xargs basename)"
local origin="$(git config --get remote.origin.url | awk -F'github.com' '{print $2}')"
local upstream="$(git config --get remote.upstream.url | awk -F'github.com' '{print $2}')"
origin="$(cut -d/ -f1 <<<"${origin:1}")"
upstream="$(cut -d/ -f1 <<<"${upstream:1}")"
# try the upstream branch if possible otherwise origin will do
upstream="${upstream:-"${origin}"}"
local to_branch="${1:-"master"}"
local from_branch="$(git rev-parse --abbrev-ref HEAD)"
local pr_url="https://github.com/${upstream}/${repo}/pull/new/${upstream}:${to_branch}...${origin}:${from_branch}"
open "${pr_url}"
}
#!/usr/bin/env zsh
# Partly stolen.... errr inspired by sunrise.zsh-theme and robbyrussell.zsh-theme
# http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
# %F, %f .. start, stop using foreground color
# %K, %k .. background color
# %B, %b .. bold
# %(if.then.else)
function sneils_git_prompt_status() {
local STATUS=""
# need to set IFS= or all leading/trailing whitespace will be stripped
while IFS= read -r line; do
# empty line? ignore
if [ "${line}" = "" ]; then
continue
fi
# TODO? implement other statuses from git_prompt_status()?
local symbol=''
# this should be much faster than the 100 invocations of echo|grep in the original function
# only match first 3 chars
case "${line:0:3}" in
# Not-Staged
("?? ") symbol="${ZSH_THEME_GIT_PROMPT_UNTRACKED}" ;;
("UU ") symbol="${ZSH_THEME_GIT_PROMPT_UNMERGED}" ;;
(" D ") symbol="${ZSH_THEME_GIT_PROMPT_DELETED}" ;;
("AM ") symbol="${ZSH_THEME_GIT_PROMPT_MODIFIED}" ;;
(" M ") symbol="${ZSH_THEME_GIT_PROMPT_MODIFIED}" ;;
(" T ") symbol="${ZSH_THEME_GIT_PROMPT_MODIFIED}" ;;
# Staged
("D ") symbol="${ZSH_THEME_GIT_PROMPT_STAGED_DELETED}" ;;
("R ") symbol="${ZSH_THEME_GIT_PROMPT_STAGED_RENAMED}" ;;
("M ") symbol="${ZSH_THEME_GIT_PROMPT_STAGED_MODIFIED}" ;;
("A ") symbol="${ZSH_THEME_GIT_PROMPT_STAGED_ADDED}" ;;
# Unknown
(*) symbol="${ZSH_THEME_GIT_PROMPT_WTF}" ;;
esac
# ignore duplicates
if ! [[ "${STATUS}" = *"${symbol}"* ]]; then
STATUS+="${symbol}"
fi
done <<< "$(git status --porcelain 2>/dev/null)"
if [ "${STATUS}" ]; then
echo -n "${ZSH_THEME_GIT_PROMPT_SEPERATOR}"
fi
echo "${STATUS}"
}
function sneils_git_prompt() {
local ref
ref="$(git_current_branch)"
if [ "${ref}" = "" ]; then
return 0
fi
local PROMPT=''
# dirty/clean flag
PROMPT+="$(parse_git_dirty)"
# prefix
PROMPT+="${ZSH_THEME_GIT_PROMPT_PREFIX}"
# branch name
PROMPT+="%F{yellow}${ref}%f"
# git status flags
# add an empty space between the dirty/clean/ahead/behind and the status flags
PROMPT+="$(sneils_git_prompt_status)"
# suffix
PROMPT+="${ZSH_THEME_GIT_PROMPT_SUFFIX}"
# exists remote?
PROMPT+="$(git_prompt_remote)"
# ahead flag
PROMPT+="$(git_commits_ahead)"
# behind flag
PROMPT+="$(git_commits_behind)"
# end with trailing whitespace
echo "${PROMPT} "
}
function sneils_prompt() {
local PROMPT=' '
# path %~ / %2~ = last 2 dirs
#PROMPT+='%B%2~%b'
# trim path to 30 chars max
PROMPT+='%B%30<...<%~%<<%b '
# git
# shellcheck disable=SC2016
PROMPT+='$(sneils_git_prompt)'
# add warning if root %! + caret
PROMPT+="%(!.${ZSH_THEME_PROMPT_PRIVILEGED}.${ZSH_THEME_PROMPT_CARET}) "
echo "${PROMPT}"
}
function sneils_rprompt() {
local PROMPT=' '
# jobs %j
PROMPT+="%(1j.%F{yellow}jobs%f${ZSH_THEME_GIT_PROMPT_PREFIX}%j${ZSH_THEME_GIT_PROMPT_SUFFIX}.) "
# return value %?
PROMPT+="%(?.. %F{red}%?%f${ZSH_THEME_PROMPT_RETURN_VALUE}) "
echo "${PROMPT}"
}
ZSH_THEME_PROMPT_RETURN_VALUE='%F{red}↩%f'
ZSH_THEME_PROMPT_PRIVILEGED="%B%F{red}‼»%f%b"
ZSH_THEME_PROMPT_CARET="%B%F{magenta}»%f%b"
ZSH_THEME_GIT_PROMPT_WTF="%B%F{blue}⁇%f%b"
ZSH_THEME_GIT_PROMPT_SEPERATOR="%F{yellow}|%f"
ZSH_THEME_GIT_PROMPT_PREFIX="%F{yellow}‹%f"
ZSH_THEME_GIT_PROMPT_SUFFIX="%F{yellow}›%f"
# used in parse_git_dirty()
ZSH_THEME_GIT_PROMPT_DIRTY="%F{red}✗%f"
ZSH_THEME_GIT_PROMPT_CLEAN="%F{green}✓%f"
# used in git_commits_ahead() / git_prompt_ahead()
ZSH_THEME_GIT_PROMPT_AHEAD="%B%F{blue}↑%f%b"
ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX="${ZSH_THEME_GIT_PROMPT_AHEAD}%B%F{blue}"
ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX="%f%b"
# used in git_commits_behind() / git_prompt_behind()
ZSH_THEME_GIT_PROMPT_BEHIND="%B%F{blue}↓%f%b"
ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX="${ZSH_THEME_GIT_PROMPT_BEHIND}%B%F{blue}"
ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX="%f%b"
# FIXME: implement remote existance tracking?
#ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS='%B%F{blue}✓%f%b'
ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS=''
ZSH_THEME_GIT_PROMPT_REMOTE_MISSING='%B%F{blue}✗%f%b'
# Staged
ZSH_THEME_GIT_PROMPT_STAGED_ADDED='%F{green}A%f'
ZSH_THEME_GIT_PROMPT_STAGED_MODIFIED='%F{green}M%f'
ZSH_THEME_GIT_PROMPT_STAGED_RENAMED='%F{green}R%f'
ZSH_THEME_GIT_PROMPT_STAGED_DELETED='%F{green}D%f'
# Not-staged
ZSH_THEME_GIT_PROMPT_UNTRACKED='%F{red}+%f'
ZSH_THEME_GIT_PROMPT_MODIFIED='%F{red}M%f'
ZSH_THEME_GIT_PROMPT_DELETED='%F{red}D%f'
ZSH_THEME_GIT_PROMPT_UNMERGED='%F{red}U%f'
PROMPT="$(sneils_prompt)"
RPROMPT="$(sneils_rprompt)"
#!/usr/bin/env zsh
# sadly the zstyle syntax with ssh-agent plugin doesn't work for me :(
#zstyle :omz:plugins:ssh-agent identities xxx
KEYS=(
somekey
someotherkey
)
keychain -q --nogui "${KEYS[@]}"
source "${HOME}/.keychain/${HOST}-sh"
#!/usr/bin/env zsh
alias tf="terraform"
alias tfa="tf apply"
alias tfaa="tfa -auto-approve"
alias tfano="tfa -refresh=false"
alias tfp="tf plan"
alias tfv="tf validate"
alias tfs="tf state"
alias tfss="tfs show"
alias tfsl="tfs list"
alias tfr="tf refresh"
alias tfpr="tf providers"
alias tfim="tf import"
alias tfun="tf force-unlock"
alias tfi="tf init"
alias tfu="tfi -upgrade"
alias tfur="tfu -reconfigure"
alias tfw="tf workspace"
alias tfwl="tfw list"
alias tfws="tfw show"
alias tfwu="tfw select"
alias tfwd="tfwu default"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment