Skip to content

Commit

Permalink
Merge pull request #34 from cameronr/main
Browse files Browse the repository at this point in the history
Add battery plugin
  • Loading branch information
fabioluciano authored Jul 3, 2024
2 parents 7950a7b + 5b51288 commit 2f80299
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- **Spt** - Show Spotify;
- **Homebrew** - Show Homebrew;
- **yay** - Show yay;
- **battery** - Show battery;

## Screenshots

Expand Down Expand Up @@ -94,6 +95,23 @@ Hit <kbd>prefix</kbd> + <kbd>I</kbd> to fetch the plugin and source it. You can
| `@theme_plugin_playerctl_accent_color_icon` | | | |
| `@theme_plugin_playerctl_format` | | | |

### Battery

Shows battery charging status (charging or discharging) and battery percentage.

| Configuration | Description | Avaliable Options | Default |
| ------------------------------------------------ | ---------------------------------- | ----------------- | -------- |
| `@theme_plugin_battery_charging_icon` | Icon to display when charging | Any character ||
| `@theme_plugin_battery_discharging` | Icon to display when on battery | Any character | 󰁹 |
| `@theme_plugin_battery_red_threshold` | Show in red when below this % | 0-100 | 10 |
| `@theme_plugin_battery_yellow_threshold` | Show in yellow when below this % | 0-100 | 30 |
| `@theme_plugin_battery_red_accent_color` | Color when < red threshold | Palette color | red |
| `@theme_plugin_battery_red_accent_color_icon` | Icon color when < red threshold | Palette color | magenta2 |
| `@theme_plugin_battery_yellow_accent_color` | Color when < yellow threshold | Palette color | yellow |
| `@theme_plugin_battery_yellow_accent_color_icon` | Icon color when < yellow threshold | Palette color | orange |
| `@theme_plugin_battery_green_accent_color` | Color when > yellow threshold | Palette color | blue7 |
| `@theme_plugin_battery_green_accent_color_icon` | Icon color when > yellow threshold | Palette color | blue0 |

### Example configuration

tmux.conf
Expand Down
141 changes: 141 additions & 0 deletions src/plugin/battery.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env bash
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$ROOT_DIR/../utils.sh"

# Battery querying code from https://github.com/tmux-plugins/tmux-battery
#
# Copyright (C) 2014 Bruno Sutic
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

s_osx() {
[ $(uname) == "Darwin" ]
}

is_chrome() {
chrome="/sys/class/chromeos/cros_ec"
if [ -d "$chrome" ]; then
return 0
else
return 1
fi
}

is_wsl() {
if [ ! -f /proc/version ]; then
return 1
fi

version=$(</proc/version)
if [[ "$version" == *"Microsoft"* || "$version" == *"microsoft"* ]]; then
return 0
else
return 1
fi
}

command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
}

battery_status() {
if is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/status | tail -n1)
awk '{print tolower($0);}' "$battery"
elif command_exists "pmset"; then
pmset -g batt | awk -F '; *' 'NR==2 { print $2 }'
elif command_exists "acpi"; then
acpi -b | awk '{gsub(/,/, ""); print tolower($3); exit}'
elif command_exists "upower"; then
local battery
battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
upower -i $battery | awk '/state/ {print $2}'
elif command_exists "termux-battery-status"; then
termux-battery-status | jq -r '.status' | awk '{printf("%s%", tolower($1))}'
elif command_exists "apm"; then
local battery
battery=$(apm -a)
if [ $battery -eq 0 ]; then
echo "discharging"
elif [ $battery -eq 1 ]; then
echo "charging"
fi
fi
}

print_battery_percentage() {
# percentage displayed in the 2nd field of the 2nd row
if is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
cat "$battery"
elif command_exists "pmset"; then
pmset -g batt | grep -o "[0-9]\{1,3\}%"
elif command_exists "acpi"; then
acpi -b | grep -m 1 -Eo "[0-9]+%"
elif command_exists "upower"; then
# use DisplayDevice if available otherwise battery
local battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
if [ -z "$battery" ]; then
return
fi
local percentage=$(upower -i $battery | awk '/percentage:/ {print $2}')
if [ "$percentage" ]; then
echo ${percentage%.*%}
return
fi
local energy
local energy_full
energy=$(upower -i $battery | awk -v nrg="$energy" '/energy:/ {print nrg+$2}')
energy_full=$(upower -i $battery | awk -v nrgfull="$energy_full" '/energy-full:/ {print nrgfull+$2}')
if [ -n "$energy" ] && [ -n "$energy_full" ]; then
echo $energy $energy_full | awk '{printf("%d%%", ($1/$2)*100)}'
fi
elif command_exists "termux-battery-status"; then
termux-battery-status | jq -r '.percentage' | awk '{printf("%d%%", $1)}'
elif command_exists "apm"; then
apm -l
fi
}
##################################################

battery_percentage=$(print_battery_percentage)
charging_status=$(battery_status)

if [ "$charging_status" == "charging" ] || [ "$charging_status" == "charged" ]; then
plugin_battery_icon=$(get_tmux_option "@theme_plugin_battery_charging_icon" "")
else
plugin_battery_icon=$(get_tmux_option "@theme_plugin_battery_discharging_icon" "󰁹 ")
fi

battery_number="${battery_percentage//%/}"

if [ "$battery_number" -lt $(get_tmux_option "@theme_plugin_battery_red_threshold" "10") ]; then
plugin_battery_accent_color=$(get_tmux_option "@theme_plugin_battery_red_accent_color" "red")
plugin_battery_accent_color_icon=$(get_tmux_option "@theme_plugin_battery_red_accent_color_icon" "magenta2")
elif [ "$battery_number" -lt $(get_tmux_option "@theme_plugin_battery_yellow_threshold" "30") ]; then
plugin_battery_accent_color=$(get_tmux_option "@theme_plugin_battery_yellow_accent_color" "yellow")
plugin_battery_accent_color_icon=$(get_tmux_option "@theme_plugin_battery_yellow_accent_color_icon" "orange")
else
plugin_battery_accent_color=$(get_tmux_option "@theme_plugin_battery_green_accent_color" "blue7")
plugin_battery_accent_color_icon=$(get_tmux_option "@theme_plugin_battery_green_accent_color_icon" "blue0")
fi

export plugin_battery_icon plugin_battery_accent_color plugin_battery_accent_color_icon

echo $battery_percentage

0 comments on commit 2f80299

Please sign in to comment.