Skip to content

Instantly share code, notes, and snippets.

@fabidick22
Last active February 26, 2024 04:03
Show Gist options
  • Save fabidick22/c3443f9290ce809c9c1ad0fce235ceac to your computer and use it in GitHub Desktop.
Save fabidick22/c3443f9290ce809c9c1ad0fce235ceac to your computer and use it in GitHub Desktop.
# More details: https://github.com/maverick9000/zsh2000
CURRENT_BG='NONE'
SEGMENT_SEPARATOR_RIGHT='\ue0b2'
SEGMENT_SEPARATOR_LEFT='\ue0b0'
ZSH_THEME_GIT_PROMPT_UNTRACKED=" ✭"
ZSH_THEME_GIT_PROMPT_DIRTY=''
ZSH_THEME_GIT_PROMPT_STASHED=' ⚑'
ZSH_THEME_GIT_PROMPT_DIVERGED=' ⚡'
ZSH_THEME_GIT_PROMPT_ADDED=" ✚"
ZSH_THEME_GIT_PROMPT_MODIFIED=" ✹"
ZSH_THEME_GIT_PROMPT_DELETED=" ✖"
ZSH_THEME_GIT_PROMPT_RENAMED=" ➜"
ZSH_THEME_GIT_PROMPT_UNMERGED=" ═"
ZSH_THEME_GIT_PROMPT_AHEAD=' ⬆'
ZSH_THEME_GIT_PROMPT_BEHIND=' ⬇'
ZSH_THEME_GIT_PROMPT_DIRTY=' ±'
prompt_segment() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR_LEFT%{$fg%} "
else
echo -n "%{$bg%}%{$fg%} "
fi
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
prompt_segment_new_line() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
echo -n "%{%k%F{$CURRENT_BG}%}"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
echo -n "$SEGMENT_SEPARATOR_LEFT%{$bg%}%{$fg%}"
else
echo -n "%{$bg%}%{$fg%} "
fi
CURRENT_BG=$1
[[ -n $3 ]] && echo -n "\n $3"
}
prompt_segment_right() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
echo -n "%K{$CURRENT_BG}%F{$1}$SEGMENT_SEPARATOR_RIGHT%{$bg%}%{$fg%} "
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR_LEFT"
else
echo -n "%{%k%}"
fi
echo -n "%{%f%}"
CURRENT_BG=''
}
prompt_user_hostname() {
local user=`whoami`
if [ -n "$SSH_CLIENT" ]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$user@%m"
fi
}
# Context: user@hostname (who am I and where am I)
prompt_context() {
if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)%n@%m"
fi
}
# Put cursor on a new line
prompt_cursor(){
prompt_segment_new_line red default ">_$"
}
prompt_git() {
local ref dirty
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
dirty=$(parse_git_dirty)
ref=$(git symbolic-ref HEAD 2> /dev/null)
if [[ -n $dirty ]]; then
prompt_segment magenta black
else
prompt_segment green black
fi
if [ "$ZSH_2000_DISABLE_GIT_STATUS" != "true" ];then
echo -n "\ue0a0 ${ref/refs\/heads\//}$dirty"$(git_prompt_status)
else
echo -n "\ue0a0 ${ref/refs\/heads\//}$dirty"
fi
fi
}
prompt_dir() {
prompt_segment blue white '%~'
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{yellow}%}✖"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
[[ -n "$symbols" ]] && prompt_segment black default "$symbols"
}
prompt_time() {
prompt_segment_right white black '%D{%H:%M:%S} '
}
prompt_k8s() {
# You need add https://github.com/superbrothers/zsh-kubectl-prompt
local color=cyan
if [[ "$ZSH_KUBECTL_USER" =~ "prod" || "$ZSH_KUBECTL_USER" =~ "PROD" ]]; then
color=red
fi
str=$ZSH_KUBECTL_PROMPT
i=$((${#str}-40))
prompt_segment_right $color black "(...${str:$i:40})"
}
prompt_rvm() {
local rvm_prompt
rvm_prompt=`rvm-prompt`
if [ "$rvm_prompt" != "" ]; then
prompt_segment_right "240" white "$rvm_prompt "
fi
}
build_prompt() {
if [ "$ZSH_2000_DISABLE_STATUS" != 'true' ];then
RETVAL=$?
prompt_status
fi
prompt_user_hostname
prompt_context
prompt_dir
prompt_git
prompt_cursor
prompt_end
}
ZSH_THEME_GIT_TIME_SINCE_COMMIT_SHORT="%{$fg[green]%}"
ZSH_THEME_GIT_TIME_SHORT_COMMIT_MEDIUM="%{$fg[yellow]%}"
ZSH_THEME_GIT_TIME_SINCE_COMMIT_LONG="%{$fg[red]%}"
ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL="%{$fg[cyan]%}"
#Customized git status, oh-my-zsh currently does not allow render dirty status before branch
git_custom_status() {
local cb=$(current_branch)
if [ -n "$cb" ]; then
echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
fi
}
# Determine the time since last commit. If branch is clean,
# use a neutral color, otherwise colors will vary according to time.
function git_time_since_commit() {
if git rev-parse --git-dir > /dev/null 2>&1; then
# Only proceed if there is actually a commit.
if [[ $(git log 2>&1 > /dev/null | grep -c "^fatal: bad default revision") == 0 ]]; then
# Get the last commit.
last_commit=`git log --pretty=format:'%at' -1 2> /dev/null`
now=`date +%s`
seconds_since_last_commit=$((now-last_commit))
# Totals
MINUTES=$((seconds_since_last_commit / 60))
HOURS=$((seconds_since_last_commit/3600))
# Sub-hours and sub-minutes
DAYS=$((seconds_since_last_commit / 86400))
SUB_HOURS=$((HOURS % 24))
SUB_MINUTES=$((MINUTES % 60))
if [[ -n $(git status -s 2> /dev/null) ]]; then
if [ "$MINUTES" -gt 30 ]; then
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_LONG"
elif [ "$MINUTES" -gt 10 ]; then
COLOR="$ZSH_THEME_GIT_TIME_SHORT_COMMIT_MEDIUM"
else
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_SHORT"
fi
else
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL"
fi
if [ "$HOURS" -gt 24 ]; then
echo "($COLOR${DAYS}d${SUB_HOURS}h${SUB_MINUTES}m%{$reset_color%})"
elif [ "$MINUTES" -gt 60 ]; then
echo "($COLOR${HOURS}h${SUB_MINUTES}m%{$reset_color%})"
else
echo "($COLOR${MINUTES}m%{$reset_color%})"
fi
fi
fi
}
build_rprompt() {
prompt_time
prompt_k8s
}
PROMPT='%{%f%b%k%}$(build_prompt) '
if [ "$ZSH_2000_DISABLE_RIGHT_PROMPT" != 'true' ];then
RPROMPT='%{%f%b%k%}$(git_time_since_commit)$(build_rprompt)'
fi
@fabidick22
Copy link
Author

DeepinScreenshot_gjs_20210217141835

@fabidick22
Copy link
Author

vim zsh2001.zsh-theme
pasted code
vim .zshrc
ZSH_THEME="zsh2001"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment