Skip to content

Commit

Permalink
feat: Added Battery Widget (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: holesachin <holesachin07@gmail.com>
Co-authored-by: Daniel Porter <dpreid@gmail.com>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent d9d160a commit 0d8406e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ see documentation for installing [`GNU bc`](https://www.gnu.org/software/bc/) in
## Installation using TPM

In your `tmux.conf`:
```
```bash
set -g @plugin "janoamaral/tokyo-night-tmux"
```

Expand All @@ -43,15 +43,15 @@ set -g @plugin "janoamaral/tokyo-night-tmux"

Run this

```
```bash
tmux set @tokyo-night-tmux_window_id_style digital
tmux set @tokyo-night-tmux_pane_id_style hsquare
tmux set @tokyo-night-tmux_zoom_id_style dsquare
```

or add this lines to your `.tmux.conf`

```
```bash
set -g @tokyo-night-tmux_window_id_style digital
set -g @tokyo-night-tmux_pane_id_style hsquare
set -g @tokyo-night-tmux_zoom_id_style dsquare
Expand All @@ -70,9 +70,7 @@ For widgets add following lines in you `.tmux.conf`
set value 0 to disable
```bash
set -g @tokyo-night-tmux_show_datetime 0

set -g @tokyo-night-tmux_date_format MYD

set -g @tokyo-night-tmux_time_format 12H
```

Expand Down Expand Up @@ -105,6 +103,14 @@ set -g @tokyo-night-tmux_show_path 1
set -g @tokyo-night-tmux_path_format relative # 'relative' or 'full'
```

#### Battery Widget

```bash
set -g @tokyo-night-tmux_show_battery_widget 1
set -g @tokyo-night-tmux_battery_name "BAT1" # some linux distro have 'BAT0'
set -g @tokyo-night-tmux_battery_low_threshold 21 # default
```

set variables value `0` to disable the widget, Remember to restart the `tmux` after changing values.

## The styles:
Expand Down
68 changes: 68 additions & 0 deletions src/battery-widget.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

# check if not enabled
SHOW_BATTERY_WIDGET=$(tmux show-option -gv @tokyo-night-tmux_show_battery_widget 2>/dev/null)
if [ "${SHOW_BATTERY_WIDGET}" != "1" ]; then
exit 0
fi

# get value from tmux config
BATTERY_NAME=$(tmux show-option -gv @tokyo-night-tmux_battery_name 2>/dev/null) # default 'BAT1'
BATTERY_LOW=$(tmux show-option -gv @tokyo-night-tmux_battery_low_threshold 2>/dev/null) # default 21
RESET="#[fg=brightwhite,bg=#15161e,nobold,noitalics,nounderscore,nodim]"

DISCHARGING_ICONS=("󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹")
CHARGING_ICONS=("󰢜" "󰂆" "󰂇" "󰂈" "󰢝" "󰂉" "󰢞" "󰂊" "󰂋" "󰂅")
NOT_CHARGING_ICON="󰚥"
NO_BATTERY_ICON="󱉝"

default_show_battery_percentage=1
default_battery_low="21"
if [[ "$(uname)" == "Darwin" ]]; then
default_battery_name="InternalBattery-0"
else
default_battery_name="BAT1"
fi

BATTERY_NAME="${BATTERY_NAME:-$default_battery_name}"
BATTERY_LOW="${BATTERY_LOW:-$default_battery_low}"

# get battery stats
if [[ "$(uname)" == "Darwin" ]]; then
pmstat=$(pmset -g batt | grep $BATTERY_NAME)
BATTERY_STATUS=$(echo $pmstat | awk '{print $4}' | sed 's/[^a-z]*//g')
BATTERY_PERCENTAGE=$(echo $pmstat | awk '{print $3}' | sed 's/[^0-9]*//g')
else
BATTERY_STATUS=$(</sys/class/power_supply/${BATTERY_NAME}/status)
BATTERY_PERCENTAGE=$(</sys/class/power_supply/${BATTERY_NAME}/capacity)
fi

# set color and icon based on battery status
case "${BATTERY_STATUS}" in
"Charging" | "charging")
ICONS="${CHARGING_ICONS[$((BATTERY_PERCENTAGE / 10 - 1))]}"
;;
"Discharging" | "discharging")
ICONS="${DISCHARGING_ICONS[$((BATTERY_PERCENTAGE / 10 - 1))]}"
;;
"Not charging" | "AC")
ICONS="${NOT_CHARGING_ICON}"
;;
"Full" | "charged")
ICONS="${NOT_CHARGING_ICON}"
;;
*) ICONS="${NO_BATTERY_ICON}"
BATTERY_PERCENTAGE="0"
;;
esac

# set color on battery capacity
if [[ ${BATTERY_PERCENTAGE} -lt ${BATTERY_LOW} ]]; then
_color="#[fg=red,bg=default,bold]"
elif [[ ${BATTERY_PERCENTAGE} -ge 100 ]]; then
_color="#[fg=green,bg=default]"
else
_color="#[fg=yellow,bg=default]"
fi

echo "${_color}${ICONS}${RESET}#[bg=default] ${BATTERY_PERCENTAGE}% "
3 changes: 2 additions & 1 deletion tokyo-night.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ custom_pane="#($SCRIPTS_PATH/custom-number.sh #P $pane_id_style)"
zoom_number="#($SCRIPTS_PATH/custom-number.sh #P $zoom_id_style)"
date_and_time="$($SCRIPTS_PATH/datetime-widget.sh)"
current_path="#($SCRIPTS_PATH/path-widget.sh #{pane_current_path})"
battery_status="#($SCRIPTS_PATH/battery-widget.sh)"

#+--- Bars LEFT ---+
# Session name
Expand All @@ -63,5 +64,5 @@ tmux set -g window-status-current-format "$RESET#[fg=#44dfaf,bg=#1F2335]  $wi
tmux set -g window-status-format "$RESET#[fg=#c0caf5,bg=default,none,dim]  $window_number #W#[nobold,dim]#{?window_zoomed_flag, $zoom_number, $custom_pane} #[fg=yellow]#{?window_last_flag,󰁯 , } "

#+--- Bars RIGHT ---+
tmux set -g status-right "$current_path$cmus_status#[fg=#a9b1d6,bg=#24283B]$netspeed$git_status$wb_git_status$date_and_time"
tmux set -g status-right "$battery_status$current_path$cmus_status#[fg=#a9b1d6,bg=#24283B]$netspeed$git_status$wb_git_status$date_and_time"
tmux set -g window-status-separator ""

0 comments on commit 0d8406e

Please sign in to comment.