Skip to content

Instantly share code, notes, and snippets.

@iain
Last active December 12, 2022 00:56
Show Gist options
  • Save iain/88fa1befc0973f4acf4baf1bc9b511a0 to your computer and use it in GitHub Desktop.
Save iain/88fa1befc0973f4acf4baf1bc9b511a0 to your computer and use it in GitHub Desktop.
Minimaliain ZSH theme

Minimaliain

This is my ZSH theme. I've had it for years, this adoption is for use as an antigen theme.

The git prompt looks like this:

  • Current git project name (blue: last exit status was ok, red: last exit status failed)
  • Time since last commit
  • Branch name or commit ref (green: working tree is clean, red: dirty working tree)
  • Merge status (↑ is you can push, ↓ is you can pull, ↕ means you have diverged)
  • Amount of stashes (signalled by the letter Σ).
  • Warning if you are inside a rebase or merge commit.
  • Subdirectory inside the git project.
#!/bin/zsh
exit_status=$1
reset=$'%{\e[0;00m%}'
gray=$'%{\e[0;90m%}'
red=$'%{\e[0;31m%}'
green=$'%{\e[0;32m%}'
yellow=$'%{\e[0;33m%}'
blue=$'%{\e[0;34m%}'
magenta=$'%{\e[0;35m%}'
cyan=$'%{\e[0;36m%}'
white=$'%{\e[0;37m%}'
function finish {
exit_code_color
prompt_type
space
append $reset
}
function prompt_type {
git_status=$(git status 2> /dev/null)
if [[ $? == 0 ]]; then
git_prompt
else
normal_prompt
fi
}
function append {
echo -n $1
}
function space {
echo -n " "
}
function exit_code_color {
if [[ $exit_status == 0 ]]; then
append $blue
else
append $red
fi
}
function minutes_since_last_commit {
now=`date +%s`
last_commit=`git log --pretty=format:'%at' -1 2>/dev/null`
if $lastcommit ; then
seconds_since_last_commit=$((now-last_commit))
minutes_since_last_commit=$((seconds_since_last_commit/60))
echo $minutes_since_last_commit
else
echo "-1"
fi
}
function time_since_last_commit {
minutes=$(minutes_since_last_commit)
if [ "$minutes" -ne -1 ]; then
space
append $gray
if [ "$minutes" -ge 518400 ]; then
append $(($minutes/518400))
append y
elif [ "$minutes" -ge 43200 ]; then
append $(($minutes/43200))
append mo
elif [ "$minutes" -ge 10080 ]; then
append $(($minutes/10080))
append w
elif [ "$minutes" -ge 1440 ]; then
append $(($minutes/1440))
append d
elif [ "$minutes" -ge 60 ]; then
append $(($minutes/60))
append h
else
append $minutes
append m
fi
fi
}
function branch_or_commit {
branch=$(git branch --no-color | grep --color=never '^\*' | sed 's/^\*\ //')
if [[ $branch == "(no"* ]]; then
# when not on any branch, always color red
echo "$red$(git rev-list -n 1 --abbrev-commit HEAD)"
elif [[ "$branch" == "" ]]; then
# you are not on any branch or commit
echo "$red???"
else
echo $branch
fi
}
function git_prompt {
branch=$(branch_or_commit)
subdir=$(git rev-parse --show-prefix)
basedir=$(git rev-parse --show-cdup)
basedir
time_since_last_commit
branch
in_rebase
merge
stashes
subdir
}
function in_rebase {
if [ -n "$basedir" ]; then
dir=$basedir
else
dir='.'
fi
if [[ -d $dir/.git/rebase-apply ]]; then
space
append $red
append "[REBASE!]"
fi
}
function branch {
space
if clean; then
append $green
else
append $yellow
fi
append $branch
}
function normal_prompt {
append "${PWD/$HOME/~}"
}
function clean {
echo $git_status | grep "working tree clean" > /dev/null
}
function ahead {
echo $git_status | grep 'Your branch is ahead' > /dev/null
}
function diverged {
echo $git_status | grep 'have diverged' > /dev/null
}
function behind {
echo $git_status | grep 'Your branch is behind' > /dev/null
}
function basedir {
if [ -n "$basedir" ]; then
dir=$(cd $basedir; pwd)
else
dir=$PWD
fi
append $(basename $dir)
}
function merge {
append $magenta
if ahead; then
space
append "↑"
elif behind; then
space
append "↓"
elif diverged; then
space
append "↕"
fi
}
function stashes {
number_of_stashes=$(git stash list | wc -l | tr -d '[[:space:]]')
append $gray
if [ $number_of_stashes -eq "0" ]; then
# noop
elif [ $number_of_stashes -eq "1" ]; then
space
append "Σ"
elif [ $number_of_stashes -eq "2" ]; then
space
append "ΣΣ"
else
space
append "Σ${number_of_stashes}"
fi
}
function subdir {
space
append $cyan
append "/${subdir%/}"
}
finish
setopt prompt_subst
export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43'
export LSCOLORS='exfxcxdxbxegedabagacad'
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=00;31'
zstyle -e ':completion:*:approximate:*' max-errors 'reply=( $(( ($#PREFIX+$#SUFFIX)/2 )) numeric )'
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
__theme_base_dir=${0:a:h}
PROMPT='$($__theme_base_dir/minimaliain.zsh $?)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment