From 4e8ef2688401ea492adc5ded48d85f447e9100ac Mon Sep 17 00:00:00 2001 From: Amand Tihon Date: Fri, 19 Feb 2010 10:42:00 +0100 Subject: [PATCH] Add battery level in right prompt. Adding the battery level forced to - move git-related psvar element. - set colors earlier in the process --- S10_zshopts | 4 +++ S60_git | 80 ++++++++++++++++++++++++++--------------------------- S70_battery | 51 ++++++++++++++++++++++++++++++++++ S90_prompt | 11 ++++---- 4 files changed, 100 insertions(+), 46 deletions(-) create mode 100644 S70_battery diff --git a/S10_zshopts b/S10_zshopts index e382cfc..c4c3b6c 100644 --- a/S10_zshopts +++ b/S10_zshopts @@ -8,6 +8,10 @@ fi setopt auto_cd setopt nobeep +autoload -U colors +colors +setopt prompt_subst + typeset -ga preexec_functions typeset -ga precmd_functions typeset -ga chpwd_functions diff --git a/S60_git b/S60_git index 037e8b3..54e61d4 100644 --- a/S60_git +++ b/S60_git @@ -1,17 +1,17 @@ #! /usr/bin/zsh -export __ZSH_GIT_BASEDIR="" -export __ZSH_GIT_SUBDIR="" -export __ZSH_GIT_BRANCH="" -export __ZSH_GIT_ACTION="" -export __ZSH_GIT_STATUS="" -export __ZSH_GIT_VARS_INVALID=1 -export __ZSH_GIT_STATUS_INVALID=1 +__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_chpwd() { # On cd, invalidate git status in prompt - export __ZSH_GIT_VARS_INVALID=1 + __ZSH_GIT_VARS_INVALID=1 } @@ -19,12 +19,12 @@ git_preexec() { # On git command, invalidate git status in prompt case "$1" in git*) - export __ZSH_GIT_VARS_INVALID=1 + __ZSH_GIT_VARS_INVALID=1 ;; esac # *any* command could invalidate the repository status (new file, ...) - export __ZSH_GIT_STATUS_INVALID=1 + __ZSH_GIT_STATUS_INVALID=1 } @@ -52,11 +52,11 @@ git_get_status() { git_parse() { - # psvar[3] == current action (merge, rebase, ...) - # psvar[4] == current branch - # psvar[5] == repository base directory - # psvar[6] == current subdir into repository - # psvar[7] == status (untracked, unstaged, staged) + # 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 @@ -65,11 +65,11 @@ git_parse() { if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then # reuse previous values - psvar[3]=${__ZSH_GIT_ACTION} - psvar[4]=${__ZSH_GIT_BRANCH} - psvar[5]=${__ZSH_GIT_BASEDIR} - psvar[6]=${__ZSH_GIT_SUBDIR} - psvar[7]=${__ZSH_GIT_STATUS} + 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 @@ -78,15 +78,15 @@ git_parse() { if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then # reuse previous values - psvar[3]=${__ZSH_GIT_ACTION} - psvar[4]=${__ZSH_GIT_BRANCH} - psvar[5]=${__ZSH_GIT_BASEDIR} - psvar[6]=${__ZSH_GIT_SUBDIR} + psvar[5]=${__ZSH_GIT_ACTION} + psvar[6]=${__ZSH_GIT_BRANCH} + psvar[7]=${__ZSH_GIT_BASEDIR} + psvar[8]=${__ZSH_GIT_SUBDIR} - export __ZSH_GIT_STATUS=$(git_get_status) - psvar[7]=${__ZSH_GIT_STATUS} + __ZSH_GIT_STATUS=$(git_get_status) + psvar[9]=${__ZSH_GIT_STATUS} - export __ZSH_GIT_STATUS_INVALID=0 + __ZSH_GIT_STATUS_INVALID=0 return fi @@ -132,21 +132,21 @@ git_parse() { gitstatus=`git_get_status` # Got here, we're in git - psvar[3]=${action} - psvar[4]=${branch#refs/heads/} - psvar[5]=${base_dir} - psvar[6]=${sub_dir} - psvar[7]=${gitstatus} + psvar[5]=${action} + psvar[6]=${branch#refs/heads/} + psvar[7]=${base_dir} + psvar[8]=${sub_dir} + psvar[9]=${gitstatus} # Save for next time - export __ZSH_GIT_BASEDIR="${base_dir}" - export __ZSH_GIT_SUBDIR="${sub_dir}" - export __ZSH_GIT_BRANCH="${branch#refs/heads/}" - export __ZSH_GIT_ACTION="${action}" - export __ZSH_GIT_STATUS="${gitstatus}" - - export __ZSH_GIT_VARS_INVALID=0 - export __ZSH_GIT_STATUS_INVALID=0 + __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_VARS_INVALID=0 + __ZSH_GIT_STATUS_INVALID=0 } diff --git a/S70_battery b/S70_battery new file mode 100644 index 0000000..6283574 --- /dev/null +++ b/S70_battery @@ -0,0 +1,51 @@ +#! /usr/bin/zsh + +# +# Prepare a "battery level" gauge ready to be used in prompt. +# + +battery_level() +{ + local online remaining bstatus gauge gcolor + + online=0 + acpi -B -a | grep -q "on-line" && online=1 + + + bstatus="$(acpi -b)" + 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" + return + fi + + gauge="" + gcolor="green" + + for i in $(seq ${remaining[1]}); do + gauge="${gauge}►" + done + + if [[ "${#gauge}" -lt "3" ]]; then + gcolor="red" + elif [[ "${#gauge}" -lt "6" ]]; then + gcolor="yellow" + fi + + psvar[2]="${gauge}" + psvar[3]="${gcolor}" + +} + +precmd_functions+="battery_level" diff --git a/S90_prompt b/S90_prompt index 9ca2cd6..e95bc9e 100644 --- a/S90_prompt +++ b/S90_prompt @@ -1,9 +1,5 @@ #! /usr/bin/zsh -autoload -U colors -colors -setopt prompt_subst - # # Prepare colors, ignoring length # @@ -26,9 +22,11 @@ _hour="${C_BCYAN}[%D{%H:%M}]${C_NO}" _exitcode="%(?::${C_BRED}[ %1v ]${C_NO} )$(true)" -_action="%(3v,%3v%,)" -_path='${C_GREEN}%(5v,%20<..<%5v%<<${C_NO}${C_MAGENTA}@%4v%3v${C_BYELLOW}%7v${C_NO}${C_GREEN}/%15<..<%6v%<<,%25<..<%~%<<)${C_NO}' +_action="%(5v,%5v%,)" +_path='${C_GREEN}%(7v,%20<..<%7v%<<${C_NO}${C_MAGENTA}@%6v%5v${C_BYELLOW}%9v${C_NO}${C_GREEN}/%15<..<%8v%<<,%25<..<%~%<<)${C_NO}' + +_batt='%{${fg_bold[$psvar[3]]}%}%2v%{$reset_color%} ' # # Now, the prompt itself @@ -36,3 +34,4 @@ _path='${C_GREEN}%(5v,%20<..<%5v%<<${C_NO}${C_MAGENTA}@%4v%3v${C_BYELLOW}%7v${C_ #PROMPT="${_exitcode}${_hour} ${_username}@${_machine}:${_vcs}${_path}$ " PROMPT="${_exitcode}${_username}@${_machine}:${_vcs}${_path}$ " +RPROMPT="${_batt}" \ No newline at end of file -- 2.39.5