Skip to content

Instantly share code, notes, and snippets.

@dongfg
Last active June 16, 2021 11:01
Show Gist options
  • Save dongfg/7283347430da1caa3b9e59a0862f7b78 to your computer and use it in GitHub Desktop.
Save dongfg/7283347430da1caa3b9e59a0862f7b78 to your computer and use it in GitHub Desktop.
zsh xxf custom theme
# Copy and self modified from xxf
# Machine name.
function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || echo $HOST
}
function exists {
command -v $1 >/dev/null 2>&1
}
# Directory info.
local current_dir='${PWD/#$HOME/~}'
# VCS
VCS_PROMPT_PREFIX1="%{$fg[white]%}on%{$reset_color%} "
VCS_PROMPT_PREFIX2=":%{$fg[cyan]%}"
VCS_PROMPT_SUFFIX="%{$reset_color%} "
VCS_PROMPT_DIRTY=" %{$fg[red]%}✗"
VCS_PROMPT_CLEAN=" %{$fg[green]%}✔︎"
##### Git info #####
local git_info='$(git_prompt_info)'
local git_last_commit_string='$(f_git_last_commit)'
ZSH_THEME_GIT_PROMPT_PREFIX="${VCS_PROMPT_PREFIX1}git${VCS_PROMPT_PREFIX2}"
ZSH_THEME_GIT_PROMPT_SUFFIX="$VCS_PROMPT_SUFFIX"
ZSH_THEME_GIT_PROMPT_DIRTY="$VCS_PROMPT_DIRTY"
ZSH_THEME_GIT_PROMPT_CLEAN="$VCS_PROMPT_CLEAN"
function f_git_last_commit() {
local git_last_commit="$(git log --pretty=format:"%h \"%s\"" -1 2>/dev/null)"
if [ ${#git_last_commit} -gt 29 ]; then
echo ${git_last_commit:9:21}"\n"
else
echo ${git_last_commit}
fi
}
##### Package version #####
local package_version_string='$(f_package_version)'
function f_package_version() {
local 'package_version'
if [[ -f package.json ]]; then
if exists jq; then
package_version=$(jq -r '.version' package.json 2>/dev/null)
elif exists python; then
package_version=$(python -c "import json; print(json.load(open('package.json'))['version'])" 2>/dev/null)
elif exists node; then
package_version=$(node -p "require('./package.json').version" 2>/dev/null)
fi
fi
if [[ -f pom.xml ]]; then
if exists xmllint; then
package_version=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml 2>/dev/null)
elif exists python; then
package_version=$(python -c "import xml.etree.ElementTree as ET; r=ET.parse('pom.xml').getroot();ns=r.tag.replace('project','');print(r.find(ns+'version').text)" 2>/dev/null)
fi
fi
[[ -z $package_version || "$package_version" == "null" || "$package_version" == "undefined" ]] && return
echo "%{$fg[white]%}ver %{$fg[magenta]%}${package_version} "
}
# Prompt format: USER at MACHINE in [DIRECTORY] on git:BRANCH STATE \n TIME $
PROMPT="
%{$fg[cyan]%}%n \
%{$fg[white]%}at \
%{$fg[green]%}$(box_name) \
%{$fg[white]%}in \
%{$terminfo[bold]$fg[yellow]%}[${current_dir}]%{$reset_color%} \
${package_version_string}\
${git_info}${git_last_commit_string}
%{$fg[red]%}%* \
%{$terminfo[bold]$fg[white]%}$ %{$reset_color%}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment