]> git.alrj.org Git - zsh.d.git/commitdiff
Add battery level in right prompt.
authorAmand Tihon <amand.tihon@iba-group.com>
Fri, 19 Feb 2010 09:42:00 +0000 (10:42 +0100)
committerAmand Tihon <amand.tihon@iba-group.com>
Fri, 19 Feb 2010 09:42:00 +0000 (10:42 +0100)
Adding the battery level forced to
- move git-related psvar element.
- set colors earlier in the process

S10_zshopts
S60_git
S70_battery [new file with mode: 0644]
S90_prompt

index e382cfc65d7f24b7652c5f8763d095ac25bc4e76..c4c3b6c4281d5d570e2a6609b550b34953e8a7e7 100644 (file)
@@ -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 037e8b3aebb190ce5819caa6ba70b754f573e734..54e61d477ad110bc101814a157c7fdf88db7c995 100644 (file)
--- 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 (file)
index 0000000..6283574
--- /dev/null
@@ -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"
index 9ca2cd6222d79d8b8eb91b1e1c4ab75d92556ac5..e95bc9ee418ad62e7e7f0d219fe541d13d079b4d 100644 (file)
@@ -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