Skip to content

Instantly share code, notes, and snippets.

@penglei
Last active June 27, 2022 07:24
Show Gist options
  • Save penglei/bd8bd0ce35a49338e1e23cc378ae90a2 to your computer and use it in GitHub Desktop.
Save penglei/bd8bd0ce35a49338e1e23cc378ae90a2 to your computer and use it in GitHub Desktop.
my oh-my-zsh theme
#based on steeef theme
#run bellow command in terminal to show color list
#: spectrum_ls
function _penglei_theme_info_sep {
echo "%{$FG[245]%}|%{$reset_color%}"
}
# Determine our shell
if [ "${ZSH_VERSION-}" ]; then
KUBE_PS1_SHELL="zsh"
elif [ "${BASH_VERSION-}" ]; then
KUBE_PS1_SHELL="bash"
fi
KUBE_PS1_SYMBOL_DEFAULT=${KUBE_PS1_SYMBOL_DEFAULT:-$'\u2388'}
KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
_kube_ps1_symbol() {
case "${KUBE_PS1_SHELL}" in
bash)
if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u2388' != "\\u2388" ]]; then
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
KUBE_PS1_SYMBOL_IMG=$'\u2638\ufe0f'
else
KUBE_PS1_SYMBOL=$'\xE2\x8E\x88'
KUBE_PS1_SYMBOL_IMG=$'\xE2\x98\xB8'
fi
;;
zsh)
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
KUBE_PS1_SYMBOL_IMG="\u2638";;
*)
KUBE_PS1_SYMBOL="k8s"
esac
if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
fi
echo "%{$FG[033]%}${KUBE_PS1_SYMBOL}%{$reset_color%}"
}
export VIRTUAL_ENV_DISABLE_PROMPT=1
function _penglei_theme_virtualenv_info {
[ $VIRTUAL_ENV ] && echo $(_penglei_theme_info_sep)%F{136}`basename $(dirname $VIRTUAL_ENV)`%f
}
function _penglei_theme_kube_info {
local info=$(kubectl config view --minify --output 'jsonpath={@.current-context}:{@.contexts[0].context.namespace}')
local kube_sep="%{$FG[024]%}:%{$reset_color%}"
local context=$(echo -n ${info} | awk -F ':' '{print $1}')
local namespace=$(echo -n ${info} | awk -F ':' '{print $2}')
if [[ -z $namespace ]]; then
kube_sep=""
fi
echo "$(_kube_ps1_symbol) %{$FG[033]%}${context}%{$reset_color%}${kube_sep}%{$fg[cyan]%}${namespace}%{$reset_color%}"
}
setopt prompt_subst
autoload -U add-zsh-hook
autoload -Uz vcs_info
#use extended color palette if available
if [[ $terminfo[colors] -ge 256 ]]; then
turquoise="%F{81}"
orange="%F{166}"
purple="%F{135}"
hotpink="%F{161}"
limegreen="%F{118}"
else
turquoise="%F{cyan}"
orange="%F{yellow}"
purple="%F{magenta}"
hotpink="%F{red}"
limegreen="%F{green}"
fi
# enable VCS systems you use
zstyle ':vcs_info:*' enable git
# check-for-changes can be really slow.
# you should disable it, if you work with large repositories
zstyle ':vcs_info:*:prompt:*' check-for-changes true
# set formats
# %b - branchname
# %u - unstagedstr (see below)
# %c - stagedstr (see below)
# %a - action (e.g. rebase-i)
# %R - repository path
# %S - path in the repository
PR_RST="%f"
FMT_BRANCH="(%{$turquoise%}%b%u%c${PR_RST})"
FMT_ACTION="(%{$limegreen%}%a${PR_RST})"
FMT_UNSTAGED="%{$orange%}●"
FMT_STAGED="%{$limegreen%}●"
zstyle ':vcs_info:*:prompt:*' unstagedstr "${FMT_UNSTAGED}"
zstyle ':vcs_info:*:prompt:*' stagedstr "${FMT_STAGED}"
zstyle ':vcs_info:*:prompt:*' actionformats "${FMT_BRANCH}${FMT_ACTION}"
zstyle ':vcs_info:*:prompt:*' formats "${FMT_BRANCH}"
zstyle ':vcs_info:*:prompt:*' nvcsformats ""
PR_GIT_UPDATE=1
function _penglei_preexec {
case "$2" in
*git*)
PR_GIT_UPDATE=1
;;
*hub*)
PR_GIT_UPDATE=1
;;
esac
}
add-zsh-hook preexec _penglei_preexec
function _penglei_chpwd {
PR_GIT_UPDATE=1
}
add-zsh-hook chpwd _penglei_chpwd
function _penglei_precmd {
if [[ -n "$PR_GIT_UPDATE" ]] ; then
# check for untracked files or updated submodules, since vcs_info doesn't
if git ls-files --other --exclude-standard 2> /dev/null | grep -q "."; then
PR_GIT_UPDATE=1
FMT_BRANCH="(%{$turquoise%}%b%u%c%{$hotpink%}●${PR_RST})"
else
FMT_BRANCH="(%{$turquoise%}%b%u%c${PR_RST})"
fi
zstyle ':vcs_info:*:prompt:*' formats "${FMT_BRANCH}"
vcs_info 'prompt'
PR_GIT_UPDATE=
fi
}
add-zsh-hook precmd _penglei_precmd
local exit_code="%(?,,C:%{$fg[red]%}%?%{$reset_color%})"
local date_time="[%{$FG[154]%}%D %*%{$reset_color%}]"
function _penglei_theme_user() {
echo -n "%{$purple%}%n${PR_RST}@"
}
function _penglei_theme_host() {
echo -n "%{$orange%}%m${PR_RST}"
}
function _penglei_theme_pre() {
if [[ ${VENDOR} != "apple" ]];then
_penglei_theme_user;_penglei_theme_host;echo -n ":"
fi
}
PROMPT='$(_penglei_theme_pre)%{$limegreen%}%~${PR_RST} $vcs_info_msg_0_($(_penglei_theme_kube_info)$(_penglei_theme_virtualenv_info)) $date_time $exit_code
▶ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment