simplified prompt creation

This commit is contained in:
Paweł Płazieński 2015-12-22 13:39:31 +01:00
parent f1e18348d4
commit 4d7e8eae57

77
.zshrc
View File

@ -106,31 +106,28 @@ export SAVEHIST=1000000
# Based on agnoster's Theme - https://gist.github.com/3712874 # Based on agnoster's Theme - https://gist.github.com/3712874
CURRENT_BG='NONE' CURRENT_BG='NONE'
PRIMARY_FG=black
# Characters # Characters
SEGMENT_SEPARATOR="\ue0b0" SEGMENT_SEPARATOR_FORWARD="\ue0b0"
# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() { prompt_segment() {
local bg fg if [[ -z $3 ]]; then return; fi
[[ -n $1 ]] && bg="%K{$1}" || bg="%k" local newbg newfg
[[ -n $2 ]] && fg="%F{$2}" || fg="%f" [[ -n $1 ]] && newbg="%K{$1}" || newbg="%k"
[[ -n $2 ]] && newfg="%F{$2}" || newfg="%f"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
print -n "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}" print -n "%{$newbg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR_FORWARD%{$newfg%}"
else else
print -n "%{$bg%}%{$fg%}" print -n "%{$newbg%}%{$newfg%}"
fi fi
print -n " $3 "
CURRENT_BG=$1 CURRENT_BG=$1
[[ -n $3 ]] && print -n $3
} }
# End the prompt, closing any open segments # End the prompt, closing any open segments
prompt_end() { prompt_end() {
if [[ -n $CURRENT_BG ]]; then if [[ -n $CURRENT_BG ]]; then
print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR_FORWARD"
else else
print -n "%{%k%}" print -n "%{%k%}"
fi fi
@ -144,54 +141,25 @@ prompt_end() {
# Root privileges # Root privileges
prompt_root() { prompt_root() {
if [[ $UID -eq 0 ]]; then if [[ $UID -eq 0 ]]; then
prompt_segment red yellow " %(!.%{%F{yellow}%}.)$LIGHTNING " print -n $LIGHTNING
fi fi
} }
# Different username # Different username
prompt_user() { prompt_user() {
local user=`whoami` local user=$(whoami)
if [[ "$user" != "$DEFAULT_USER" && $UID -ne 0 ]]; then if [[ "$user" != "$DEFAULT_USER" && $UID -ne 0 ]]; then
prompt_segment magenta $PRIMARY_FG " %(!.%{%F{yellow}%}.)$user " print -n $user
fi fi
} }
# Different host # Different host
prompt_host() { prompt_host() {
if [[ -n "$SSH_CONNECTION" ]]; then if [[ -n "$SSH_CONNECTION" ]]; then
prompt_segment cyan $PRIMARY_FG " %m " print -n "%m"
fi fi
} }
# VCS status
prompt_vcs() {
local ref
ref="$vcs_info_msg_0_"
if [[ -n "$ref" ]]; then
prompt_segment yellow $PRIMARY_FG " $ref "
fi
}
# Dir: current working directory
prompt_mvn() {
if [[ -n "$MAVEN_PROJECT" ]]; then
prompt_segment magenta $PRIMARY_FG " $MAVEN_PROJECT "
fi
}
# show current time
prompt_time() {
local current_date
current_date=`date +%R`
prompt_segment green $PRIMARY_FG " ${current_date} "
}
# Dir: current working directory
prompt_dir() {
prompt_segment blue $PRIMARY_FG ' %~ '
}
# Status: # Status:
# - was there an error # - was there an error
# - are there background jobs? # - are there background jobs?
@ -208,21 +176,21 @@ prompt_status() {
symbols+="%{%F{cyan}%}$GEAR" symbols+="%{%F{cyan}%}$GEAR"
fi fi
if [[ -n "$symbols" ]]; then if [[ -n "$symbols" ]]; then
prompt_segment $PRIMARY_FG default " $symbols " echo "$symbols"
fi fi
} }
## Main prompt ## Main prompt
prompt_main() { prompt_main() {
CURRENT_BG='NONE' CURRENT_BG='NONE'
prompt_time prompt_segment green black "$(date +%R)" # prompt time
prompt_status prompt_segment black default "$(prompt_status)"
prompt_root prompt_segment red yellow "$(prompt_root)"
prompt_user prompt_segment magenta black "$(prompt_user)"
prompt_host prompt_segment cyan black "$(prompt_host)"
prompt_dir prompt_segment blue black '%~' # prompt directory
prompt_mvn prompt_segment magenta black "$MAVEN_PROJECT" # prompt maven project
prompt_vcs prompt_segment yellow black "$vcs_info_msg_0_" # prompt vcs
prompt_end prompt_end
} }
@ -231,7 +199,6 @@ prompt_precmd() {
PROMPT="%{%f%b%k%}$(prompt_main) " PROMPT="%{%f%b%k%}$(prompt_main) "
} }
prompt_opts=(cr subst percent) prompt_opts=(cr subst percent)
add-zsh-hook precmd prompt_precmd add-zsh-hook precmd prompt_precmd