Last active
January 17, 2022 10:45
-
-
Save akora/a578ded2a31c24758acb41233c8d1255 to your computer and use it in GitHub Desktop.
zsh-prompt-macos-catalina
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# compaudit | xargs chmod g-w | |
autoload -Uz compinit && compinit | |
autoload -U promptinit && promptinit | |
autoload -U colors && colors # Enable colors in prompt | |
autoload -Uz vcs_info | |
precmd() { vcs_info } | |
setopt prompt_subst | |
# The below is based on the fantastic work of Josh Dick from | |
# https://joshdick.net/2017/06/08/my_git_prompt_for_zsh_revisited.html | |
# Echoes information about Git repository status when inside a Git repository | |
git_info() { | |
# Exit if not inside a Git repository | |
! git rev-parse --is-inside-work-tree > /dev/null 2>&1 && return | |
# Git branch/tag, or name-rev if on detached head | |
local GIT_LOCATION=${$(git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD)#(refs/heads/|tags/)} | |
local AHEAD="%{$fg[red]%}⇡NUM%{$reset_color%}" | |
local BEHIND="%{$fg[cyan]%}⇣NUM%{$reset_color%}" | |
local MERGING="%{$fg[magenta]%}⚡︎%{$reset_color%}" | |
local UNTRACKED="%{$fg[red]%}●%{$reset_color%}" | |
local MODIFIED="%{$fg[yellow]%}●%{$reset_color%}" | |
local STAGED="%{$fg[green]%}●%{$reset_color%}" | |
local -a DIVERGENCES | |
local -a FLAGS | |
local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')" | |
if [ "$NUM_AHEAD" -gt 0 ]; then | |
DIVERGENCES+=( "${AHEAD//NUM/$NUM_AHEAD}" ) | |
fi | |
local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')" | |
if [ "$NUM_BEHIND" -gt 0 ]; then | |
DIVERGENCES+=( "${BEHIND//NUM/$NUM_BEHIND}" ) | |
fi | |
local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)" | |
if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then | |
FLAGS+=( "$MERGING" ) | |
fi | |
if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then | |
FLAGS+=( "$UNTRACKED" ) | |
fi | |
if ! git diff --quiet 2> /dev/null; then | |
FLAGS+=( "$MODIFIED" ) | |
fi | |
if ! git diff --cached --quiet 2> /dev/null; then | |
FLAGS+=( "$STAGED" ) | |
fi | |
local -a GIT_INFO | |
GIT_INFO+=( "\033[38;5;15m±" ) | |
[ -n "$GIT_STATUS" ] && GIT_INFO+=( "$GIT_STATUS" ) | |
[[ ${#DIVERGENCES[@]} -ne 0 ]] && GIT_INFO+=( "${(j::)DIVERGENCES}" ) | |
[[ ${#FLAGS[@]} -ne 0 ]] && GIT_INFO+=( "${(j::)FLAGS}" ) | |
GIT_INFO+=( "\033[38;5;15m$GIT_LOCATION%{$reset_color%}" ) | |
echo "${(j: :)GIT_INFO}" | |
} | |
# Use ❯ as the non-root prompt character; # for root | |
# Change the prompt character color if the last command had a nonzero exit code | |
# Derived from: | |
# Allow for colors | |
# autoload -U promptinit | |
# promptinit | |
# PROMPT=" | |
# (%D{%a %f %b %T}) <%?> [%~] | |
# %n at %m.%l ($SHLVL-%!-z)%# " | |
# +--------------------------- current time | |
# | +------- return code from last command | |
# | | +-- current working directory | |
# | | | | |
# vvv v vvv | |
# (Fri 3 May 21:23:55) <1> [~/notebook/2019/0503] | |
# vogelke at myhost.pts/0 (6-11316-z)% | |
# ^^^ ^^^ ^^^ ^ ^^^ ^ ^ | |
# | | | | | | | | |
# | | | | | | +-- non-root shell | |
# | | | | | +---- ZSH (tcsh/bash prompts are similar) | |
# | | | | +--------- number of commands typed | |
# | | | +------------ number of shells started | |
# | | +------------------ TTY I'm using | |
# | +------------------------- my hostname | |
# +------------------------------------ my username (or ROOT) | |
# Full, original | |
# PROMPT=' | |
# $fg[magenta]%}(%D{%a %f %b %T}) $fg[green]%}<%?> $fg[red]%}[%~] $(git_info) | |
# $fg[yellow]%}%n@%m.%l $fg[cyan]%}($SHLVL-%!-z) %(?.%{$fg[blue]%}.%{$fg[red]%})%(!.#.❯)%{$reset_color%} ' | |
local PATH_COLOR="032" | |
local USER_HOST_COLOR="036" | |
local TTY_COLOR="245" | |
local PROMPT_CHAR_NON_ROOT_COLOR="075" | |
local PROMPT_CHAR_ROOT_COLOR="124" | |
PS1=' | |
%F{$PATH_COLOR}[%~] $(git_info) | |
%F{$USER_HOST_COLOR}%n@%m %F{$TTY_COLOR}%l %(?.%{%F{$PROMPT_CHAR_NON_ROOT_COLOR}.%{%F{$PROMPT_CHAR_ROOT_COLOR})%(!.#.❯)%{$reset_color%} ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment