First commit

This commit is contained in:
Bruno Sutic 2014-05-18 16:36:18 +02:00
commit 79e702ac96
3 changed files with 114 additions and 0 deletions

51
battery_osx.tmux Executable file
View file

@ -0,0 +1,51 @@
# /usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
battery_percentage="#($CURRENT_DIR/scripts/battery_percentage_osx.sh)"
battery_icon="#($CURRENT_DIR/scripts/battery_icon_osx.sh)"
battery_percentage_interpolation="&bp"
battery_icon_interpolation="&bi"
get_status_right() {
local status_right=$(tmux show-option -gv status-right)
echo $status_right
}
# checks if any interpolation string exists.
needs_batt_interpolation() {
local string=$1
[[ "$string" =~ ($battery_percentage_interpolation|$battery_icon_interpolation) ]]
}
do_interpolation() {
local string=$1
local percentage_interpolated=${string/$battery_percentage_interpolation/$battery_percentage}
local all_interpolated=${percentage_interpolated/$battery_icon_interpolation/$battery_icon}
echo $all_interpolated
}
# If the `status-right` tmux option contains battery percentage or icon
# interpolation string, it is substituted with a battery command.
# If there's no interpolation string at all, battery command is prepended to
# the `status-right` option.
insert_battery_commands() {
local string=$1
if needs_batt_interpolation "$string"; then
do_interpolation "$string"
else
# prepend battery_command to the 'status-right'
echo "$battery_icon$battery_percentage $string"
fi
}
update_tmux_status_right() {
local new_status=$1
tmux set -gq status-right "$new_status"
}
main() {
local status_right=$(get_status_right)
local new_status_right=$(insert_battery_commands "$status_right")
update_tmux_status_right "$new_status_right"
}
main

52
scripts/battery_icon_osx.sh Executable file
View file

@ -0,0 +1,52 @@
# /usr/bin/env bash
# script global variables
charged_icon=""
charging_icon=""
discharging_icon=""
charged_default="🔋 "
charging_default="⚡️ "
discharging_default=""
# tmux show-option "q" (quiet) flag does not set return value to 1, even though
# the option does not exist. This function patches that.
get_tmux_option() {
option=$1
option_value=$(tmux show-option -gqv "$option")
if [ -z $option_value ]; then
return 1
else
echo $option_value
fi
}
# icons are set as script global variables
get_icon_settings() {
charged_icon=$(get_tmux_option "@batt_charged_icon" || echo "$charged_default")
charging_icon=$(get_tmux_option "@batt_charging_icon" || echo "$charging_default")
discharging_icon=$(get_tmux_option "@batt_discharging_icon" || echo "$discharging_default")
}
battery_status() {
# "charged", "charging" or "discharging" is the 3rd field on the second line
pmset -g batt | awk 'NR==2 { print $3 }'
}
print_icon() {
local status=$1
if [[ $status =~ (^charged) ]]; then
printf "$charged_icon"
elif [[ $status =~ (^charging) ]]; then
printf "$charging_icon"
elif [[ $status =~ (^discharging) ]]; then
printf "$discharging_icon"
fi
}
main() {
get_icon_settings
local status=$(battery_status)
print_icon "$status"
}
main

View file

@ -0,0 +1,11 @@
# /usr/bin/env bash
print_battery_percentage() {
# percentage displayed in the 2nd field of the 2nd row
pmset -g batt | awk 'NR==2 { gsub(/;/,""); print $2 }'
}
main() {
print_battery_percentage
}
main