Skip to content

Instantly share code, notes, and snippets.

@chentex
Last active September 18, 2020 17:38
Show Gist options
  • Save chentex/6843db6ffe305d83d378255c3205fa21 to your computer and use it in GitHub Desktop.
Save chentex/6843db6ffe305d83d378255c3205fa21 to your computer and use it in GitHub Desktop.
My config
__GIT_PROMPT_DIR="${0:A:h}"
## Hook function definitions
function chpwd_update_git_vars() {
update_current_git_vars
}
function preexec_update_git_vars() {
case "$2" in
git*|hub*|gh*|stg*)
__EXECUTED_GIT_COMMAND=1
;;
esac
}
function precmd_update_git_vars() {
if [ -n "$__EXECUTED_GIT_COMMAND" ] || [ ! -n "$ZSH_THEME_GIT_PROMPT_CACHE" ]; then
update_current_git_vars
unset __EXECUTED_GIT_COMMAND
fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_update_git_vars
add-zsh-hook precmd precmd_update_git_vars
add-zsh-hook preexec preexec_update_git_vars
## Function definitions
function update_current_git_vars() {
unset __CURRENT_GIT_STATUS
local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
_GIT_STATUS=$(python ${gitstatus} 2>/dev/null)
__CURRENT_GIT_STATUS=("${(@s: :)_GIT_STATUS}")
GIT_BRANCH=$__CURRENT_GIT_STATUS[1]
GIT_AHEAD=$__CURRENT_GIT_STATUS[2]
GIT_BEHIND=$__CURRENT_GIT_STATUS[3]
GIT_STAGED=$__CURRENT_GIT_STATUS[4]
GIT_CONFLICTS=$__CURRENT_GIT_STATUS[5]
GIT_CHANGED=$__CURRENT_GIT_STATUS[6]
GIT_UNTRACKED=$__CURRENT_GIT_STATUS[7]
}
git_super_status() {
precmd_update_git_vars
if [ -n "$__CURRENT_GIT_STATUS" ]; then
STATUS="$ZSH_THEME_GIT_PROMPT_PREFIX$ZSH_THEME_GIT_PROMPT_BRANCH$GIT_BRANCH%{${reset_color}%}"
if [ "$GIT_BEHIND" -ne "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
fi
if [ "$GIT_AHEAD" -ne "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
fi
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_SEPARATOR"
if [ "$GIT_STAGED" -ne "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
fi
if [ "$GIT_CONFLICTS" -ne "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CONFLICTS$GIT_CONFLICTS%{${reset_color}%}"
fi
if [ "$GIT_CHANGED" -ne "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
fi
if [ "$GIT_UNTRACKED" -ne "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED%{${reset_color}%}"
fi
if [ "$GIT_CHANGED" -eq "0" ] && [ "$GIT_CONFLICTS" -eq "0" ] && [ "$GIT_STAGED" -eq "0" ] && [ "$GIT_UNTRACKED" -eq "0" ]; then
STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
fi
STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
echo "$STATUS"
fi
}
# Default values for the appearance of the prompt.
ZSH_THEME_GIT_PROMPT_PREFIX="("
ZSH_THEME_GIT_PROMPT_SUFFIX=")"
ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[239]%}"
ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[red]%}%{● %G%}"
ZSH_THEME_GIT_PROMPT_CONFLICTS="%{$fg[red]%}%{✖ %G%}"
ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚ %G%}"
ZSH_THEME_GIT_PROMPT_BEHIND="%{↓ %G%}"
ZSH_THEME_GIT_PROMPT_AHEAD="%{↑ %G%}"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{… %G%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔ %G%}"
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import re
from subprocess import Popen, PIPE, check_output
def get_tagname_or_hash():
"""return tagname if exists else hash"""
# get hash
hash_cmd = ['git', 'rev-parse', '--short', 'HEAD']
hash_ = check_output(hash_cmd).strip()
# get tagname
tags_cmd = ['git', 'for-each-ref', '--points-at=HEAD', '--count=2', '--sort=-version:refname', '--format=%(refname:short)', 'refs/tags']
tags = check_output(tags_cmd).split()
if tags:
return tags[0] + ('+' if len(tags) > 1 else '')
elif hash_:
return hash_
return None
# `git status --porcelain --branch` can collect all information
# branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind
po = Popen(['git', 'status', '--porcelain', '--branch'], env=dict(os.environ, LANG="C"), stdout=PIPE, stderr=PIPE)
stdout, sterr = po.communicate()
if po.returncode != 0:
sys.exit(0) # Not a git repository
# collect git status information
untracked, staged, changed, conflicts = [], [], [], []
ahead, behind = 0, 0
status = [(line[0], line[1], line[2:]) for line in stdout.decode('utf-8').splitlines()]
for st in status:
if st[0] == '#' and st[1] == '#':
if re.search('Initial commit on', st[2]) or re.search('No commits yet on', st[2]):
branch = st[2].split(' ')[-1]
elif re.search('no branch', st[2]): # detached status
branch = get_tagname_or_hash()
elif len(st[2].strip().split('...')) == 1:
branch = st[2].strip()
else:
# current and remote branch info
branch, rest = st[2].strip().split('...')
if len(rest.split(' ')) == 1:
# remote_branch = rest.split(' ')[0]
pass
else:
# ahead or behind
divergence = ' '.join(rest.split(' ')[1:])
divergence = divergence.lstrip('[').rstrip(']')
for div in divergence.split(', '):
if 'ahead' in div:
ahead = int(div[len('ahead '):].strip())
elif 'behind' in div:
behind = int(div[len('behind '):].strip())
elif st[0] == '?' and st[1] == '?':
untracked.append(st)
else:
if st[1] == 'M':
changed.append(st)
if st[0] == 'U':
conflicts.append(st)
elif st[0] != ' ':
staged.append(st)
out = ' '.join([
branch,
str(ahead),
str(behind),
str(len(staged)),
str(len(conflicts)),
str(len(changed)),
str(len(untracked)),
])
print(out, end='')
# If you come from bash you might have to change your $PATH. [0/0]
export PATH=$HOME/bin:/usr/local/bin:$HOME/go/bin:/var/data/python/bin:/home/vzepedamas/.local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/home/vzepedamas/.oh-my-zsh"
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="fino-ctx"
plugins=(
asdf git docker docker-compose git-flow golang helm kubectl minikube python suse systemd terraform tmux vagrant vscode
)
source $ZSH/oh-my-zsh.sh
source $ZSH/plugins/kind/kind.plugin.zsh
alias vi vim
alias iosc="osc -A https://api.suse.de"
if [ "$TMUX" = "" ]; then tmux; fi
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
# GO
export GOENV_ROOT="$HOME/.goenv"
export GOENV_DISABLE_GOPATH="1"
export PATH="$GOENV_ROOT/bin:$PATH"
export GO111MODULE="on"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="/home/vzepedamas/.sdkman"
[[ -s "/home/vzepedamas/.sdkman/bin/sdkman-init.sh" ]] && source "/home/vzepedamas/.sdkman/bin/sdkman-init.sh"
# fino.zsh-theme
# Use with a dark background and 256-color terminal!
# Meant for people with rbenv and git. Tested only on OS X 10.7.
# You can set your computer name in the ~/.box-name file if you want.
# Borrowing shamelessly from these oh-my-zsh themes:
# bira
# robbyrussell
#
# Also borrowing from http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
source $HOME/.custom/git-prompt.plugin.zsh
function prompt_char {
git branch >/dev/null 2>/dev/null && echo "±" && return
echo '○'
}
function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST}
}
local ruby_env='using%{$FG[243]%} $(ruby_prompt_info)'
local git_info='$(git_super_status)'
local prompt_char='$(prompt_char)'
PROMPT="╭─%{$FG[040]%}%n%{$reset_color%} %{$FG[239]%}at%{$reset_color%} %{$FG[033]%}$(box_name)%{$reset_color%} %{$FG[239]%}in%{$reset_color%} %{$terminfo[bold]$FG[226]%}%~%{$reset_color%}${git_info}
╰─${prompt_char}%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_PREFIX=" %{$FG[239]%}on%{$reset_color%} %{$fg[255]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$FG[202]%}✘✘✘"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$FG[040]%}✔"
ZSH_THEME_RUBY_PROMPT_PREFIX="‹"
ZSH_THEME_RUBY_PROMPT_SUFFIX="›%{$reset_color%}"
# VCS
YS_VCS_PROMPT_PREFIX1=" %{$fg[white]%}on%{$reset_color%} "
YS_VCS_PROMPT_PREFIX2=":%{$fg[cyan]%}"
YS_VCS_PROMPT_SUFFIX="]%{$reset_color%}"
YS_VCS_PROMPT_DIRTY=" %{$fg[red]%}x"
YS_VCS_PROMPT_CLEAN=" %{$fg[green]%}o"
# Git info
# local git_info='$(git_prompt_info)'
local git_info='$(git_super_status)'
ZSH_THEME_GIT_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}git${YS_VCS_PROMPT_PREFIX2}["
ZSH_THEME_GIT_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX"
ZSH_THEME_GIT_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY"
ZSH_THEME_GIT_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN"
# HG info
local hg_info='$(ys_hg_prompt_info)'
ys_hg_prompt_info() {
# make sure this is a hg dir
if [ -d '.hg' ]; then
echo -n "${YS_VCS_PROMPT_PREFIX1}hg${YS_VCS_PROMPT_PREFIX2}"
echo -n $(hg branch 2>/dev/null)
if [ -n "$(hg status 2>/dev/null)" ]; then
echo -n "$YS_VCS_PROMPT_DIRTY"
else
echo -n "$YS_VCS_PROMPT_CLEAN"
fi
echo -n "$YS_VCS_PROMPT_SUFFIX"
fi
}
local exit_code="%(?,,C:%{$fg[red]%}%?%{$reset_color%})"
# Prompt format:
#
# PRIVILEGES USER @ MACHINE in DIRECTORY on git:BRANCH STATE [TIME] C:LAST_EXIT_CODE
# $ COMMAND
#
# For example:
#
# % ys @ ys-mbp in ~/.oh-my-zsh on git:master x [21:47:42] C:0
# $
PROMPT="
%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n) \
%{$fg[white]%}@ \
%{$fg[green]%}%m \
%{$fg[white]%}in \
%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
${hg_info}\
${git_info}\
\
%{$fg[white]%} $exit_code
%{$terminfo[bold]$fg[red]%}$ %{$reset_color%}"
RPROMPT=%{$terminfo[bold]$fg[red]%}"[%D{%y/%m/%f}|%*]"%{$reset_color%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment