Skip to content

Instantly share code, notes, and snippets.

@jyc
Created March 30, 2020 22:09
Show Gist options
  • Save jyc/ec785f2653a20ef76ca328ef44dbd0a7 to your computer and use it in GitHub Desktop.
Save jyc/ec785f2653a20ef76ca328ef44dbd0a7 to your computer and use it in GitHub Desktop.
#!/bin/zsh
cd "$2"
ellipsize() {
# Shouldn't add ellipsis if we didn't actually need to ellipsize.
ellipsized="$(echo "$1" | cut -f 1-8 -d' ' | cut -c 1-32)"
if [[ "$ellipsized" != "$1" ]]; then
echo "$ellipsized…"
else
echo "$1"
fi
}
local EXIT_CODE=$1
local EXIT_CODE_TEXT=
# Make it automatically add %F{244}.
# Handle spaces.
if [[ $EXIT_CODE -eq 0 ]]; then
PROMPT_ARROW_COLOR=245
else
PROMPT_ARROW_COLOR=001
# EXIT_CODE_TEXT="%f,%B%F{001} exit=$EXIT_CODE%F{244}%b"
fi
PROMPT_EXTRA=
[ -d .git ] || git rev-parse --git-dir >/dev/null 2>&1
if [ $? -eq 0 ]; then
# Git prompt.
local ONELINE="$(git log -n 1 --format=oneline 2>/dev/null)"
local REV
local TITLE
local BRANCH
if [ $? -eq 0 ]; then
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
case "$BRANCH" in
"") BRANCH="" ;;
"HEAD") BRANCH="(detached)" ;;
esac
if [ ! -z "$BRANCH" ]; then
BRANCH=" %F{040}$BRANCH%f"
fi
if [ ! -z "$ONELINE" ]; then
REV="$(echo "$ONELINE" | cut -f 1 -d' ')"
if [[ "$REV" == "$(git rev-parse HEAD 2>/dev/null)" ]]; then
HEAD="/%B%F{044}HEAD%f%b"
fi
TITLE="$(echo "$ONELINE" | cut -f 2- -d' ')"
TITLE=" %b%F{244}$(ellipsize "$TITLE")%f"
fi
else
HEAD=
TITLE=" no commits"
fi
local GIT_DIR="$(git rev-parse --git-dir)"
local REBASING
if [ -d "$GIT_DIR/rebase-merge" ] || [ -d ".git/rebase-apply" ]; then
#REBASING=" %B%F{001}⟲ %f%b"
REBASING=" 🏗 "
fi
if [ -e "$GIT_DIR/CHERRY_PICK_HEAD" ]; then
REBASING=" 🍒"
fi
# Slow.
#MODIFIED=
#git ls-files -m
#git status --porcelain | rg '^\s[MADRCU]' >/dev/null
#if [[ $? -eq 0 ]]; then
# MODIFIED=" %B%F{196}M%f%b"
#fi
PROMPT_EXTRA="%B%F{202}git%b%f${SPACE}${REBASING}${BRANCH}${HEAD}${TITLE}"
fi
if [ -d ".hg" ]; then
# Mercurial prompt.
# Slow.
local data="$(hg log -r tip -T '{node|short}:{tags}:{desc}' | head -n1)"
local node="$(echo "$data" | cut -d: -f1)"
local tag="$(echo "$data" | cut -d: -f2 | cut -d' ' -f1)"
local desc="$(ellipsize "$(echo "$data" | cut -d: -f3-)")"
local node_or_tag="$tag"
if [ -z "$tag" ]; then
node_or_tag="$node"
fi
local bookmark
bookmark="default"
if [ -e .hg/bookmarks.current ]; then
bookmark="$(cat .hg/bookmarks.current)"
fi
PROMPT_EXTRA="%B%F{245}hg%b%f%F{244} %F{040}$bookmark%f/%B%F{044}$node_or_tag%f%b %F{244}$desc%f"
fi
if [ ! -z "$PROMPT_EXTRA" ]; then
PROMPT_EXTRA="($PROMPT_EXTRA%f%b)"
fi
#if [ "$EXIT_CODE" != 0 ]; then
# PROMPT_EXTRA="$PROMPT_EXTRA !%F{001}$EXIT_CODE%f!"
#fi
echo "$PROMPT_ARROW_COLOR:$PROMPT_EXTRA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment