From 24e74f7cd9e63583edcc3facf253bf439202319f Mon Sep 17 00:00:00 2001 From: Amand Tihon Date: Wed, 4 May 2011 11:26:51 +0200 Subject: [PATCH] Rework the prompt config. * 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. --- S10_zshopts | 7 ----- S11_colors | 19 +++++++++++++ S22_exitcode | 11 +++++--- S60_git | 42 ++++------------------------- S61_konsoletabs | 2 +- S70_battery | 31 ++++++++------------- S90_prompt | 72 +++++++++++++++++++++++++++++-------------------- 7 files changed, 86 insertions(+), 98 deletions(-) create mode 100644 S11_colors diff --git a/S10_zshopts b/S10_zshopts index 2697a16..36840e2 100644 --- a/S10_zshopts +++ b/S10_zshopts @@ -20,13 +20,6 @@ typeset -ga chpwd_functions # 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 diff --git a/S11_colors b/S11_colors new file mode 100644 index 0000000..6fb4572 --- /dev/null +++ b/S11_colors @@ -0,0 +1,19 @@ +#! /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]}%}" diff --git a/S22_exitcode b/S22_exitcode index 230294d..003c9f0 100644 --- a/S22_exitcode +++ b/S22_exitcode @@ -3,12 +3,15 @@ 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' +} diff --git a/S60_git b/S60_git index 2972c39..256afc8 100644 --- a/S60_git +++ b/S60_git @@ -1,10 +1,12 @@ #! /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 @@ -59,40 +61,16 @@ git_get_status() { 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 @@ -134,28 +112,18 @@ git_parse() { 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 diff --git a/S61_konsoletabs b/S61_konsoletabs index 58dbb9f..340eb16 100644 --- a/S61_konsoletabs +++ b/S61_konsoletabs @@ -1,4 +1,4 @@ -#! /usr/binn/zsh +#! /usr/bin/zsh # Change konsole tab to current path konsole-rename-path () { diff --git a/S70_battery b/S70_battery index 023f004..bdc1750 100644 --- a/S70_battery +++ b/S70_battery @@ -1,51 +1,42 @@ #! /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" diff --git a/S90_prompt b/S90_prompt index 10832a2..b9a7d4e 100644 --- a/S90_prompt +++ b/S90_prompt @@ -1,39 +1,53 @@ #! /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)' -- 2.39.2