From 5b5128826e054881dfa10b430eaeeb31715ee586 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 1 Jul 2024 23:05:04 -0700 Subject: [PATCH] Add battery plugin Pulls in some code from https://github.com/tmux-plugins/tmux-battery --- README.md | 18 ++++++ src/plugin/battery.sh | 141 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100755 src/plugin/battery.sh diff --git a/README.md b/README.md index aa637ee..08e370d 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ - **Spt** - Show Spotify; - **Homebrew** - Show Homebrew; - **yay** - Show yay; +- **battery** - Show battery; ## Screenshots @@ -94,6 +95,23 @@ Hit prefix + I 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 diff --git a/src/plugin/battery.sh b/src/plugin/battery.sh new file mode 100755 index 0000000..1e7c079 --- /dev/null +++ b/src/plugin/battery.sh @@ -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=$(/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