├── .gitignore ├── LICENSE.txt ├── README.md └── battery /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Nicolas Goles Domic 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Battery 2 | 3 | ![image](http://i.imgur.com/mEEPD.png) 4 | 5 | Battery is a little bash script that uses [Spark](https://github.com/holman/spark) to display the battery status on your __tmux__ sessions or the __terminal__. 6 | 7 | ### Features 8 | 9 | * Changes color to reflect battery status (Green, Yellow, Red) 10 | * Displays battery percentage 11 | * Graph bar changes its values between 0 and 100% (thanks to spark) 12 | * If you don't like the default colors, you can specify the good, medium and warning battery status colors using flags (read usage). 13 | 14 | ### Requirements 15 | 16 | Right now, battery requires [Spark](https://github.com/holman/spark) to graph your battery status. 17 | Battery can run on both __Mac OS X__ and Linux. 18 | 19 | If you don't want to use Spark, you can use the `-a` flag, for ascii output: 20 | ![image](http://i.imgur.com/w9qtQeu.png) 21 | 22 | # Install - Mac 23 | 24 | ### Homebrew 25 | 26 | Just do (case sensitive) 27 | 28 | brew tap Goles/battery 29 | brew install battery 30 | 31 | ### One Liner 32 | (Cut & Paste on terminal to install on `/usr/bin`, btw, try to run from `~/` or other writable dir) 33 | 34 | brew install spark; curl -O https://raw.github.com/Goles/Battery/master/battery ; \ 35 | sudo mv battery /usr/bin; sudo chmod 755 /usr/bin/battery 36 | 37 | 38 | ### Step by Step 39 | 40 | * Install spark (with [Homebrew](https://github.com/mxcl/homebrew) on Mac OS X) 41 | 42 | ``` brew install spark``` 43 | 44 | * Copy battery somewhere in your path & fix permissions 45 | 46 | ``` sudo cp battery /usr/bin ``` 47 | 48 | ``` sudo chmod 755 /usr/bin/battery ``` 49 | 50 | # Install - Linux 51 | 52 | Linux support is still being tested. It ought to work properly in Debian and 53 | Ubuntu, but is largely untested in other distributions. Using linux requires 54 | `upower`, which should be included, or available, on most linux distributions. 55 | 56 | It's recommended to install this somewhere in your path that is writable, 57 | like `/usr/local/bin` 58 | 59 | ```bash 60 | # if you also want to use spark 61 | curl -O https://raw.githubusercontent.com/holman/spark/master/spark 62 | mv spark /usr/local/bin 63 | chmod u+x /usr/local/bin/spark 64 | 65 | curl -O https://raw.githubusercontent.com/goles/battery/master/battery 66 | mv battery /usr/local/bin 67 | chmod u+x /usr/local/bin/battery 68 | ``` 69 | __NOTE:__ This `spark` is *not* the same `spark` that you would install by doing 70 | 71 | ```bash 72 | $ sudo aptitude install spark 73 | ``` 74 | That is [Apache Spark](http://spark.apache.org), which is a general engine for 75 | large-scale data processing. 76 | 77 | 78 | # Usage 79 | 80 | ### Terminal 81 | 82 | * Run Battery (From the terminal) 83 | 84 | ``` battery ``` 85 | ###### You should see something like this: 86 | ![image](http://i.imgur.com/SLSBg.png) 87 | 88 | ### tmux 89 | 90 | * Be sure to make tmux display utf-8 characters by running it with the `-u` flag 91 | 92 | ```tmux -u``` 93 | 94 | * Add the following line to your `~/.tmux.conf` file 95 | 96 | ``` set -g status-right "#(/usr/bin/battery -t)"``` 97 | 98 | * reload the tmux config by running `tmux source-file ~/.tmux.conf`. 99 | 100 | ###### You should now see something like this at the bottom right corner: 101 | ![image](http://i.imgur.com/Eaajb.png) 102 | 103 | # Flags 104 | 105 | The flag `-b` will set a different battery path, the default value is `/sys/class/power_supply/BAT0`. You can specifiy the colors for __good__ battery level, __middle__ battery level, and __warning__ battery level with the flags ``` -g -m -w ```. 106 | __Note:__ You should use color names for when in tmux mode and [ascii colors](http://www.termsys.demon.co.uk/vtansi.htm#colors) in terminal mode. 107 | In Mac OS, you can specify to use pmset with the `-p` flag; without it, the program uses `ioreg`. In Linux, this flag is ignored, and always uses `upower`. 108 | 109 | Battery displays an emoji by default. You can disable this behaviour by passing the `-e` flag. 110 | 111 | The flag `-z` will add zsh escape characters to the output of the script. 112 | -------------------------------------------------------------------------------- /battery: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | usage() { 4 | cat < good battery level default: 1;32 | green | 64 16 | -m middle battery level default: 1;33 | yellow | 136 17 | -w warn battery level default: 0;31 | red | 160 18 | EOF 19 | } 20 | 21 | if [[ $1 == '-h' || $1 == '--help' || $1 == '-?' ]]; then 22 | usage 23 | exit 0 24 | fi 25 | 26 | # For default behavior 27 | setDefaults() { 28 | pmset_on=0 29 | output_tmux=0 30 | output_zsh=0 31 | ascii=0 32 | ascii_bar='==========' 33 | emoji=1 34 | good_color="1;32" 35 | middle_color="1;33" 36 | warn_color="0;31" 37 | connected=0 38 | battery_path=/sys/class/power_supply/BAT0 39 | } 40 | 41 | setDefaults 42 | 43 | # Determine battery charge state 44 | battery_charge() { 45 | case $(uname -s) in 46 | "Darwin") 47 | if ((pmset_on)) && command -v pmset &>/dev/null; then 48 | if [ "$(pmset -g batt | grep -o 'AC Power')" ]; then 49 | BATT_CONNECTED=1 50 | else 51 | BATT_CONNECTED=0 52 | fi 53 | BATT_PCT=$(pmset -g batt | grep -o '[0-9]*%' | tr -d %) 54 | else 55 | while read key value; do 56 | case $key in 57 | "MaxCapacity") 58 | maxcap=$value 59 | ;; 60 | "CurrentCapacity") 61 | curcap=$value 62 | ;; 63 | "ExternalConnected") 64 | if [ $value == "No" ]; then 65 | BATT_CONNECTED=0 66 | else 67 | BATT_CONNECTED=1 68 | fi 69 | ;; 70 | esac 71 | if [[ -n "$maxcap" && -n $curcap ]]; then 72 | BATT_PCT=$(( 100 * curcap / maxcap)) 73 | fi 74 | done < <(ioreg -n AppleSmartBattery -r | grep -o '"[^"]*" = [^ ]*' | sed -e 's/= //g' -e 's/"//g' | sort) 75 | fi 76 | ;; 77 | "Linux") 78 | case $(cat /etc/*-release) in 79 | *"Arch Linux"*|*"Ubuntu"*|*"openSUSE"*) 80 | battery_state=$(cat $battery_path/energy_now) 81 | battery_full=$battery_path/energy_full 82 | battery_current=$battery_path/energy_now 83 | ;; 84 | *) 85 | battery_state=$(cat $battery_path/status) 86 | battery_full=$battery_path/charge_full 87 | battery_current=$battery_path/charge_now 88 | ;; 89 | esac 90 | if [ $battery_state == 'Discharging' ]; then 91 | BATT_CONNECTED=0 92 | else 93 | BATT_CONNECTED=1 94 | fi 95 | now=$(cat $battery_current) 96 | full=$(cat $battery_full) 97 | BATT_PCT=$((100 * $now / $full)) 98 | ;; 99 | esac 100 | } 101 | 102 | # Apply the correct color to the battery status prompt 103 | apply_colors() { 104 | # Green 105 | if [[ $BATT_PCT -ge 75 ]]; then 106 | if ((output_tmux)); then 107 | COLOR="#[fg=$good_color]" 108 | elif ((output_zsh)); then 109 | COLOR="%F{$good_color}" 110 | else 111 | COLOR=$good_color 112 | fi 113 | 114 | # Yellow 115 | elif [[ $BATT_PCT -ge 25 ]] && [[ $BATT_PCT -lt 75 ]]; then 116 | if ((output_tmux)); then 117 | COLOR="#[fg=$middle_color]" 118 | elif ((output_zsh)); then 119 | COLOR="%F{$middle_color}" 120 | else 121 | COLOR=$middle_color 122 | fi 123 | 124 | # Red 125 | elif [[ $BATT_PCT -lt 25 ]]; then 126 | if ((output_tmux)); then 127 | COLOR="#[fg=$warn_color]" 128 | elif ((output_zsh)); then 129 | COLOR="%F{$warn_color}" 130 | else 131 | COLOR=$warn_color 132 | fi 133 | fi 134 | } 135 | 136 | # Print the battery status 137 | print_status() { 138 | if ((emoji)) && ((BATT_CONNECTED)); then 139 | GRAPH="⚡" 140 | else 141 | if command -v spark &>/dev/null; then 142 | sparks=$(spark 0 ${BATT_PCT} 100) 143 | GRAPH=${sparks:1:1} 144 | else 145 | ascii=1 146 | fi 147 | fi 148 | 149 | if ((ascii)); then 150 | barlength=${#ascii_bar} 151 | 152 | # Battery percentage rounded to the lenght of ascii_bar 153 | rounded_n=$(( $barlength * $BATT_PCT / 100 + 1)) 154 | 155 | # Creates the bar 156 | GRAPH=$(printf "[%-${barlength}s]" "${ascii_bar:0:rounded_n}") 157 | fi 158 | 159 | if ((output_tmux)); then 160 | printf "%s%s %s%s" "$COLOR" "[$BATT_PCT%]" "$GRAPH" "#[default]" 161 | elif ((output_zsh)); then 162 | printf "%%B%s%s %s" "$COLOR" "[$BATT_PCT%%]" "$GRAPH" 163 | else 164 | printf "\e[0;%sm%s %s \e[m\n" "$COLOR" "[$BATT_PCT%]" "$GRAPH" 165 | fi 166 | } 167 | 168 | # Read args 169 | while getopts ":g:m:w:tzeab:p" opt; do 170 | case $opt in 171 | g) 172 | good_color=$OPTARG 173 | ;; 174 | m) 175 | middle_color=$OPTARG 176 | ;; 177 | w) 178 | warn_color=$OPTARG 179 | ;; 180 | t) 181 | output_tmux=1 182 | good_color="green" 183 | middle_color="yellow" 184 | warn_color="red" 185 | ;; 186 | z) 187 | output_zsh=1 188 | good_color="64" 189 | middle_color="136" 190 | warn_color="160" 191 | ;; 192 | e) 193 | emoji=0 194 | ;; 195 | a) 196 | ascii=1 197 | ;; 198 | p) 199 | pmset_on=1 200 | ;; 201 | b) 202 | if [ -d $OPTARG ]; then 203 | battery_path=$OPTARG 204 | else 205 | >&2 echo "Battery not found, trying to use default path..." 206 | if [ ! -d $battery_path ]; then 207 | >&2 echo "Default battery path is also unreachable" 208 | exit 1 209 | fi 210 | fi 211 | ;; 212 | \?) 213 | echo "Invalid option: -$OPTARG" 214 | exit 1 215 | ;; 216 | :) 217 | echo "Option -$OPTARG requires an argument" 218 | exit 1 219 | ;; 220 | esac 221 | done 222 | 223 | battery_charge 224 | apply_colors 225 | print_status 226 | --------------------------------------------------------------------------------