You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.8 KiB

# Custom dir command
prompt_dir(){
homeIcon=" "
wpPluginsIcon=".p."
wpThemesIcon=".t."
7 years ago
siteIcon=" "
wpSiteIcon=" "
gitReposIcon=""
7 years ago
magentoSiteIcon=" "
7 years ago
dropboxIcon=""
dotfilesIcon=""
7 years ago
seperator=" "
seperatorDual=" "
root=" "
# Gets the path.
local current_path="$(print -P "%~")"
# Replace either ~ or ~/ with
current_path=$(echo $current_path | sed -r -e "s/\~/$homeIcon/")
8 years ago
# Replace wp-content/themes with theme icon if in theme
# current_path=$(echo $current_path | sed -r -e "s/wp\-content\/themes/$wpThemesIcon/")
8 years ago
# Replace wp-content/plugins with plugin icon if in plugin
# current_path=$(echo $current_path | sed -r -e "s/wp\-content\/plugins/$wpPluginsIcon/")
8 years ago
current_project_full="$(type -p project > /dev/null && project current --path)"
5 years ago
if [ -n "$current_project_full" ]; then
5 years ago
if echo "$PWD" | grep -q "$current_project_full"; then
current_path=$(echo $PWD | sed -r -e "s#$current_project_full##" | sed -r -e 's/^\///')
current_path="$current_path"
fi
fi
#
8 years ago
#This is used for checking if wp or magento
local ph=${PWD%/public_html*}/public_html
# If in a site folder and a wp site, replace home/Sites/<site-name>/public_html with siteIcon <site-url>
8 years ago
if [[ -d $ph ]]; then
local icon=$siteIcon
if [[ -e "$ph/wp-config.php" ]]; then #If WordPress
icon=$wpSiteIcon
# Change wp-content in sub folders
current_path=$(echo $current_path | sed -r -e "s/wp\-content\//wpc\//")
if [[ $(tput cols) -lt 100 ]]; then
current_path=$(echo $current_path | sed -r -e "s/wpc\/themes\//\//")
7 years ago
current_path=$(echo $current_path | sed -r -e "s/wpc\/plugins\//\//")
fi
8 years ago
7 years ago
elif [[ -e "$ph/bin/magento" ]]; then #If magento
8 years ago
icon=$magentoSiteIcon
fi
current_path=$(echo $current_path | sed -r -e "s/$homeIcon\/Sites\/([a-z_\-]*)\/public_html/$icon\1/")
8 years ago
fi
8 years ago
# Replace Dropbox with icon
8 years ago
current_path=$(echo $current_path | sed -r -e "s/$homeIcon\/Dropbox/$dropboxIcon/")
# Replace GitRepos with icon
current_path=$(echo $current_path | sed -r -e "s/$homeIcon\/GitRepos/$gitReposIcon/")
# Replace GitRepos with icon
current_path=$(echo $current_path | sed -r -e "s/$homeIcon\/.dotfiles/$dotfilesIcon/")
8 years ago
# Set the root
current_path=$(echo $current_path | sed -r -e "s/^\//$root\//g")
if [[ $(tput cols) -lt 100 ]]; then
current_path=$(echo $current_path | sed -r -e "s/\/.*\//\/\//g")
fi
8 years ago
# Set the dual seperator
current_path=$(echo $current_path | sed -r -e "s/\/\//$seperatorDual/g")
# Set the seperator
current_path=$(echo $current_path | sed -r -e "s/\//$seperator/g")
8 years ago
7 years ago
# If seperator is at the end, remove it (this should only be the case if in root directory)
current_path=$(echo $current_path | sed -r -e "s/$seperator\$//g")
8 years ago
echo $current_path
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
echo "green"
else
echo "blue"
fi
echo "black"
}
5 years ago
function prompt_project() {
type -p project > /dev/null || return
# Prints the current project and a recording symbol if appropriate
5 years ago
local parent_process="$(ps -ocommand -p $PPID | grep -v 'COMMAND' | cut -d' ' -f1)"
5 years ago
local current_project_full="$(project current --path)"
local current_project_name="$(project current)"
local background=""
if [[ "$parent_process" == "/usr/bin/script" ]]; then
segment_content="辶"
fi
5 years ago
# If there is a current project
if [ -n "$current_project_name" ]; then
segment_content="${segment_content}$current_project_name"
5 years ago
# If in the current project
if echo "$PWD" | grep -q "$current_project_full"; then
background="green"
5 years ago
elif echo "$PWD" | grep -q "$HOME/Projects/"; then
background="red"
5 years ago
else
background="yellow"
5 years ago
fi
fi
echo "$state$segment_content"
echo "$background"
echo "black"
}
prompt_git(){
local repoTopLevel="$(command git rev-parse --show-toplevel 2> /dev/null)"
if [ -n "$repoTopLevel" ]; then
local branch="$(git branch --show-current 2> /dev/null || git rev-parse --abbrev-ref HEAD)"
local tag="$(git describe --tags --exact-match HEAD 2> /dev/null)"
local color="green"
local ret=""
[ -n "$branch" ] && ret=" $branch "
[ -n "$tag" ] && ret+=" $tag "
[ -n "$ret" ] || ret="$(git rev-parse --short HEAD 2> /dev/null)"
local untrackedFiles=$(command git ls-files --others --exclude-standard "${repoTopLevel}" 2> /dev/null)
local modified=$(command git diff --name-only 2> /dev/null)
local staged=$(command git diff --staged --name-only 2> /dev/null)
local conflicts=$(command git status --porcelain | grep -E '^(.?U|DD|AA)')
local tokens
if [ -n "$untrackedFiles" ]; then
tokens+=" "
color="orange1"
fi
if [ -n "$modified" ]; then
tokens+=" "
color="orange1"
fi
if [ -n "$staged" ]; then
tokens+=" "
color="orange1"
fi
if [ -n "$conflicts" ]; then
tokens=" "
color="red1"
fi
ret+="$tokens"
5 years ago
fi
ret="$(echo "$ret" | sed -e 's/ *$//')"
echo "$ret"
echo "$color"
5 years ago
}
prompt_last_exit_code() {
local LAST_EXIT_CODE="$1"
if [[ $LAST_EXIT_CODE -ne 0 ]]; then
local EXIT_CODE_PROMPT=' '
EXIT_CODE_PROMPT+="%{$fg[red]%}-%{$reset_color%}"
EXIT_CODE_PROMPT+="%{$fg_bold[red]%}$LAST_EXIT_CODE%{$reset_color%}"
EXIT_CODE_PROMPT+="%{$fg[red]%}-%{$reset_color%}"
echo "$EXIT_CODE_PROMPT"
fi
}
5 years ago
prompt_email(){
type -p notmuch > /dev/null || return 0
[ -n "$(project current)" ] && return 0
unread="$(notmuch count "tag:unread")"
if [ "$unread" -gt 0 ]; then
echo "🖂 $unread"
echo "yellow3"
fi
}
# Draws a seperator
# Takes 2 arguments, from color then to color
# If only 1 given, assumes it is the last
seperator(){
local from="$1"
local to="$2"
local sep=""
if [ -z "$to" ]; then
resetColor
else
focusBackgroundColor "$to"
fi
focusForegroundColor "$from"
echo -n "$sep"
resetColor
}
5 years ago
draw_segment(){
# Echoes a segment
# $1 is the command to be run to print the segment
# $2 is the color of the last segment (if empty, no seperator is added)
# Returns segment on line 1
# returns background color on line 2
local output="$($1)"
local contents="$(echo "$output" | sed -n '1p')"
local previousBackground="$2"
local background=""
local foreground=""
local ret=""
if [ -n "$contents" ]; then
if [ -n "$NO_COLOR" ]; then
ret=" $(seperator white black) $contents"
else
background="$(echo "$output" | sed -n '2p')"
foreground="$(echo "$output" | sed -n '3p')"
if [ -n "$previousBackground" ]; then
ret=" $(seperator $previousBackground $background)"
fi
ret="$ret$(focusBackgroundColor $background) $(focusForegroundColor $foreground)$contents"
fi
echo "$ret"
echo "$background"
fi
}
set_prompts(){
# Get the return status of the previous command
local RETVAL=$?
#Set background to nothing at the start of the prompt
local background=""
#Set the prompt to an empty string
PROMPT=""
###### Each segment printed here
# Project
segment="$(draw_segment "prompt_project" "$background")"
if [ -n "$(echo "$segment" | sed -n '1p')" ];then
PROMPT="$PROMPT$(echo "$segment" | sed -n '1p')"
background="$(echo "$segment" | sed -n '2p')"
fi
# Directory
segment="$(draw_segment "prompt_dir" "$background")"
if [ -n "$(echo "$segment" | sed -n '1p')" ];then
PROMPT="$PROMPT$(echo "$segment" | sed -n '1p')"
background="$(echo "$segment" | sed -n '2p')"
fi
# Emails
segment="$(draw_segment "prompt_email" "$background")"
if [ -n "$(echo "$segment" | sed -n '1p')" ];then
PROMPT="$PROMPT$(echo "$segment" | sed -n '1p')"
background="$(echo "$segment" | sed -n '2p')"
fi
# Git
segment="$(draw_segment "prompt_git" "$background")"
if [ -n "$(echo "$segment" | sed -n '1p')" ];then
PROMPT="$PROMPT$(echo "$segment" | sed -n '1p')"
background="$(echo "$segment" | sed -n '2p')"
fi
PROMPT="$PROMPT $(seperator "$background")$(resetColor)"
#$(resetColor)
RPROMPT="$(resetColor)$(prompt_last_exit_code "$RETVAL")$(resetColor)"
}
zle -N set_prompts
autoload -U add-zsh-hook
add-zsh-hook precmd set_prompts