* Move colors definitions in its own file.
* Use prompt_subst for the path part, including git status when applicable.
* Rework the battery status part of the prompt.
* Rework the exit status.
* Show the exit status only once, clear on empty line.
* Rework git status, now clean of psvar
* Now free of psvar.
# use emacs bindings
bindkey -e
-# This must be done very early.
-clear_psvar() {
- psvar=()
-}
-precmd_functions+='clear_psvar'
-
-
[ -w $zsh_cache ] && HISTFILE=$zsh_cache/history
HISTSIZE=10000 # size of history
--- /dev/null
+#! /usr/bin/zsh
+
+#
+# Prepare the colors for the prompt
+#
+C_NO="%{$reset_color%}"
+
+# normal
+C_CYAN="%{${fg[cyan]}%}"
+C_WHITE="%{${fg[white]}%}"
+C_MAGENTA="%{${fg[magenta]}%}"
+C_GREEN="%{${fg[green]}%}"
+
+# bright/bold
+C_BCYAN="%{${fg_bold[cyan]}%}"
+C_BYELLOW="%{${fg_bold[yellow]}%}"
+C_BRED="%{${fg_bold[red]}%}"
+C_MAGENTA="%{${fg_bold[magenta]}%}"
+C_BGREEN="%{${fg_bold[green]}%}"
parse_exitcode() {
EX=`print -P %?`
- psvar[1]=$EX
+
+ [[ $EX -eq 0 ]] && return
if [[ $EX -ge 128 && $EX -le (127+${#signals}) ]]
then
- psvar[1]="${signals[${EX}-127]}"
+ EX=${signals[${EX}-127]}
fi
-}
+
+ echo "${C_BRED}[ $EX ]
+${C_NO}"
-precmd_functions+='parse_exitcode'
+}
#! /usr/bin/zsh
+# Those can be used, for instance to construct the prompt:
__ZSH_GIT_BASEDIR=""
__ZSH_GIT_SUBDIR=""
__ZSH_GIT_BRANCH=""
__ZSH_GIT_ACTION=""
__ZSH_GIT_STATUS=""
+
__ZSH_GIT_VARS_INVALID=1
__ZSH_GIT_STATUS_INVALID=1
git_parse() {
- # psvar[5] == current action (merge, rebase, ...)
- # psvar[6] == current branch
- # psvar[7] == repository base directory
- # psvar[8] == current subdir into repository
- # psvar[9] == status (untracked, unstaged, staged)
-
local git_dir ref base_dir sub_dir action branch gitstat gitstatus
-
# If nothing has been invalidated
if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then
-
- # reuse previous values
- psvar[5]=${__ZSH_GIT_ACTION}
- psvar[6]=${__ZSH_GIT_BRANCH}
- psvar[7]=${__ZSH_GIT_BASEDIR}
- psvar[8]=${__ZSH_GIT_SUBDIR}
- psvar[9]=${__ZSH_GIT_STATUS}
-
return
fi
# If only status has been invalidated
if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then
-
- # reuse previous values
- psvar[5]=${__ZSH_GIT_ACTION}
- psvar[6]=${__ZSH_GIT_BRANCH}
- psvar[7]=${__ZSH_GIT_BASEDIR}
- psvar[8]=${__ZSH_GIT_SUBDIR}
-
__ZSH_GIT_STATUS=$(git_get_status)
- psvar[9]=${__ZSH_GIT_STATUS}
-
__ZSH_GIT_STATUS_INVALID=0
return
fi
action="-merge"
branch="$ref"
else
- test -f "$git_dir/BISECT_LOG" && psvar[3]="bisect"
+ test -f "$git_dir/BISECT_LOG" && action="-bisect"
branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
branch="$(cut -c1-7 "$git_dir/HEAD")..."
fi
- # Status
- gitstatus=`git_get_status`
-
- # Got here, we're in git
- psvar[5]=${action}
- psvar[6]=${branch#refs/heads/}
- psvar[7]=${base_dir}
- psvar[8]=${sub_dir}
- psvar[9]=${gitstatus}
-
- # Save for next time
+
__ZSH_GIT_BASEDIR="${base_dir}"
__ZSH_GIT_SUBDIR="${sub_dir}"
__ZSH_GIT_BRANCH="${branch#refs/heads/}"
__ZSH_GIT_ACTION="${action}"
- __ZSH_GIT_STATUS="${gitstatus}"
+ __ZSH_GIT_STATUS=`git_get_status`
__ZSH_GIT_VARS_INVALID=0
__ZSH_GIT_STATUS_INVALID=0
-#! /usr/binn/zsh
+#! /usr/bin/zsh
# Change konsole tab to current path
konsole-rename-path () {
#! /usr/bin/zsh
-#
-# Prepare a "battery level" gauge ready to be used in prompt.
-#
-
battery_level()
{
+ # Return a "battery level" gauge ready to be used in prompt.
+
local online remaining bstatus gauge gcolor
online=0
acpi -a 2> /dev/null | grep -q "on-line" && online=1
-
bstatus="$(acpi -b 2> /dev/null)"
remaining="$(echo ${bstatus[(w)4]} | sed -r 's/(^[0-9]+)%.*/\1/')"
if [[ -z "$remaining" ]]; then
- psvar[2]=""
return
elif [[ "$remaining" -eq "100" && "$online" -eq "1" ]]; then
- psvar[2]=""
return
- #elif [[ "$remaining" -gt "95" ]]; then
- # export BATT_GAUGE=""
- # return
elif [[ "$remaining" -lt "10" ]]; then
- psvar[2]="►"
- psvar[3]="red"
+ echo "${C_RED}►${C_NO}"
return
fi
gauge=""
- gcolor="green"
+ rgauge=""
+ gcolor=${C_BGREEN}
for i in $(seq ${remaining[1]}); do
gauge="${gauge}►"
done
+ for i in $(seq $(( 10 - ${remaining[1]})) ); do
+ rgauge="${rgauge}·"
+ done
if [[ "${#gauge}" -lt "3" ]]; then
- gcolor="red"
+ gcolor=${C_BRED}
elif [[ "${#gauge}" -lt "6" ]]; then
- gcolor="yellow"
+ gcolor=${C_BYELLOW}
fi
- psvar[2]="${gauge}"
- psvar[3]="${gcolor}"
-
+ echo "${gcolor}${gauge}${C_NO}${C_WHITE}${rgauge}${C_NO}"
}
-
-precmd_functions+="battery_level"
#! /usr/bin/zsh
-#
-# Prepare colors, ignoring length
-#
-C_NO="%{$reset_color%}"
-C_BYELLOW="%{${fg_bold[yellow]}%}"
-C_BRED="%{${fg_bold[red]}%}"
-C_BCYAN="%{${fg_bold[cyan]}%}"
-C_GREEN="%{${fg[green]}%}"
-C_BGREEN="%{${fg_bold[green]}%}"
-C_MAGENTA="%{${fg_bold[magenta]}%}"
-
-
#
-# Prompt parts
+# Exit code.
#
-_username="${C_BYELLOW}%n${C_NO}"
-_machine="${C_BRED}%m${C_NO}"
-_hour="${C_BCYAN}[%D{%H:%M}]${C_NO}"
-
-_exitcode="%(?::${C_BRED}[ %1v ]${C_NO}
-)$(true)"
+# Hide exit code after a blank line.
+# Inspired from https://github.com/robbyrussell/oh-my-zsh/blob/66b7fe1b27637feba61a4b47e113b18b69432bf2/themes/dieter.zsh-theme
+
+
+get_exitcode=true
+
+function accept-line-or-clear-warning () {
+ if [[ -z $BUFFER ]]; then
+ get_exitcode=true
+ else
+ get_exitcode=parse_exitcode
+ fi
+ zle accept-line
+}
+zle -N accept-line-or-clear-warning
+bindkey '^M' accept-line-or-clear-warning
+
+
+setprompt()
+{
+ # This function sets the entire prompt.
+ # It requires that prompt_subst is set.
+
+ _username="${C_BYELLOW}%n${C_NO}"
+ _machine="${C_BRED}%m${C_NO}"
+
+ # Git status in the pwd, if applicable
+ if [[ -n $__ZSH_GIT_BASEDIR ]]; then
+ _basedir="${C_GREEN}%20<..<${__ZSH_GIT_BASEDIR}%<<${C_NO}"
+ _branch="${C_MAGENTA}@${__ZSH_GIT_BRANCH}${__ZSH_GIT_ACTION}${C_NO}"
+ _status="${__ZSH_GIT_STATUS}"
+ _subdir="${C_GREEN}/%15<..<${__ZSH_GIT_SUBDIR}%<<${C_NO}"
+ _path="${_basedir}${_branch}${_status}${_subdir}"
+ else
+ _path="${C_GREEN}%25<..<%~%<<${C_NO}"
+ fi
+
+
+ echo ${_username}@${_machine}:${_path}
+}
-_action="%(5v,%5v%,)"
-_path='${C_GREEN}%(7v,%20<..<%7v%<<${C_NO}${C_MAGENTA}@%6v%5v${__ZSH_GIT_STATUS}${C_GREEN}/%15<..<%8v%<<,%25<..<%~%<<)${C_NO}'
-
-
-_batt='%{${fg_bold[$psvar[3]]}%}%2v%{$reset_color%} '
-
#
# Now, the prompt itself
#
-#PROMPT="${_exitcode}${_hour} ${_username}@${_machine}:${_vcs}${_path}$ "
-PROMPT="${_exitcode}${_username}@${_machine}:${_vcs}${_path}$ "
-
-RPROMPT="${_batt}"
\ No newline at end of file
+PROMPT='$($get_exitcode)$(setprompt)$ '
+RPROMPT='$(battery_level)'