Skip to content

Instantly share code, notes, and snippets.

@EJIqpEP
Created January 6, 2021 08:47
Show Gist options
  • Select an option

  • Save EJIqpEP/62b05bc612de4ab88e294f8f67af894a to your computer and use it in GitHub Desktop.

Select an option

Save EJIqpEP/62b05bc612de4ab88e294f8f67af894a to your computer and use it in GitHub Desktop.
# http://zsh.sourceforge.net/Doc/Release/User-Contributions.html
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git hg
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' stagedstr "%F{green}●%f" # default 'S'
zstyle ':vcs_info:*' unstagedstr "%F{red}●%f" # default 'U'
zstyle ':vcs_info:*' use-simple true
zstyle ':vcs_info:git+set-message:*' hooks git-untracked
zstyle ':vcs_info:git*:*' formats '[%b%m%c%u] ' # default ' (%s)-[%b]%c%u-'
zstyle ':vcs_info:git*:*' actionformats '[%b|%a%m%c%u] ' # default ' (%s)-[%b|%a]%c%u-'
# for prompt
add-zsh-hook precmd vcs_info
function () {
if [[ -n "$TMUX" ]]; then
local LVL=$(($SHLVL - 1))
else
local LVL=$SHLVL
fi
if [[ $EUID -eq 0 ]]; then
local SUFFIX=$(printf '#%.0s' {1..$LVL})
else
local SUFFIX=$(printf '\$%.0s' {1..$LVL})
fi
if [[ -n "$TMUX" ]]; then
# Note use a non-breaking space at the end of the prompt because we can use it as
# a find pattern to jump back in tmux.
local NBSP=' '
export PS1="%F{green}${SSH_TTY:+%n@%m}%f%B${SSH_TTY:+:}%b%F{blue}%1~%F{yellow}%B%(1j.*.)%(?..!)%b%f%F{red}%B${SUFFIX}%b%f${NBSP}"
export ZLE_RPROMPT_INDENT=0
else
# Don't bother with ZLE_RPROMPT_INDENT here, because it ends up eating the
# space after PS1.
export PS1="%F{green}${SSH_TTY:+%n@%m}%f%B${SSH_TTY:+:}%b%F{blue}%1~%F{yellow}%B%(1j.*.)%(?..!)%b%f%F{red}%B${SUFFIX}%b%f "
fi
}
RPROMPT_BASE="\${vcs_info_msg_0_}%F{blue}%~%f"
setopt PROMPT_SUBST
export RPROMPT=$RPROMPT_BASE
typeset -F SECONDS
function record-start-time() {
emulate -L zsh
ZSH_START_TIME=${ZSH_START_TIME:-$SECONDS}
}
add-zsh-hook preexec record-start-time
function report-start-time() {
emulate -L zsh
if [ $ZSH_START_TIME ]; then
local DELTA=$(($SECONDS - $ZSH_START_TIME))
local DAYS=$((~~($DELTA / 86400)))
local HOURS=$((~~(($DELTA - $DAYS * 86400) / 3600)))
local MINUTES=$((~~(($DELTA - $DAYS * 86400 - $HOURS * 3600) / 60)))
local SECS=$(($DELTA - $DAYS * 86400 - $HOURS * 3600 - $MINUTES * 60))
local ELAPSED=''
test "$DAYS" != '0' && ELAPSED="${DAYS}d"
test "$HOURS" != '0' && ELAPSED="${ELAPSED}${HOURS}h"
test "$MINUTES" != '0' && ELAPSED="${ELAPSED}${MINUTES}m"
if [ "$ELAPSED" = '' ]; then
SECS="$(print -f "%.2f" $SECS)s"
elif [ "$DAYS" != '0' ]; then
SECS=''
else
SECS="$((~~$SECS))s"
fi
ELAPSED="${ELAPSED}${SECS}"
local ITALIC_ON=$'\e[3m'
local ITALIC_OFF=$'\e[23m'
export RPROMPT="%F{cyan}%{$ITALIC_ON%}${ELAPSED}%{$ITALIC_OFF%}%f $RPROMPT_BASE"
unset ZSH_START_TIME
else
export RPROMPT="$RPROMPT_BASE"
fi
}
add-zsh-hook precmd report-start-time
function +vi-git-untracked() {
emulate -L zsh
if [[ -n $(git ls-files --exclude-standard --others 2> /dev/null) ]]; then
hook_com[unstaged]+="%F{blue}●%f"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment