Compare commits

...

57 commits

Author SHA1 Message Date
Martin Beentjes 43832651ed
Release v2.0.0 (#121) 2025-12-30 16:13:39 +01:00
Jean-Philippe Leconte 5c47191df8
Add plugin_interpolated_options support (#103) 2025-12-30 16:04:39 +01:00
David Gamero 26491f0c03
add mac charging watts (#109)
* add mac charging watts

* update readme
2025-12-30 16:02:13 +01:00
purajit c2eafd79f2
return appropriate icon for 'finishing charge' (#115) 2025-12-30 16:02:01 +01:00
Julian Grinblat e3b7fe0641
feat: add battery_enabled script (#118) 2025-12-30 15:58:56 +01:00
Norbert Szivós 65b0c5261b
Fix battery percentage output when using upower (#89)
Upower output was 98.4789% which mess up the icon and graph view as
well.
2025-12-30 15:57:23 +01:00
wzy 661aa4d25e
Support print_battery_remain() for Linux/WSL (#119) 2025-12-30 15:57:05 +01:00
Julian 3a1a711ba1
Fix battery detection on systems with multiple ACPI batteries by filtering phantom batteries with 0% (#120) 2025-12-30 15:56:48 +01:00
wzy 91b05110cc
Fix termux (#114) 2025-06-10 14:14:31 +02:00
Tomasz Kania-Orzeł 48fae59ba4
fix(battery-remain): fix with acpi and @batt_remain_short option (#112) 2023-12-01 09:37:22 +01:00
Jediah Katz 5c52d4f7f8
Add support for WSL2 and for alternate powersupply names (#100) 2021-01-02 17:38:28 +01:00
Abhijeet Viswam f865b04f78
Added support for WSL (#95) 2020-12-29 20:48:32 +01:00
Dan Cassidy f8b8e84519 Readme variable fix (#85)
* Fixed variable names inside README file

* More fixed variables.

* Updated some verbiage

This should help to make some of the descriptions more clear.
2019-07-04 12:42:38 +02:00
Dan Cassidy 86952b21cf Fixed variable names inside README file (#84) 2019-07-03 10:27:19 +02:00
tsutomu 0272f2fc94 Modified not to print under the decimal points. (#83) 2019-06-16 12:15:46 +02:00
Dan Cassidy 8f1626fe95 Color icon upgrade (#80)
* Upgrade color and icons to have higher granularity

This is a large overhaul of the colors and icons in the plugin to enable
higher granularity. All efforts have been made to have sane defaults.

Additionally, a new 'status' has been added that is a catch-all
'unknown' in case a battery tool acts up or fails to report something
meaningful.

Added 8 new format strings:
* #{battery_color_bg} - will set the background color of the status bar
  based on the battery charge level and status
* #{battery_color_fg} - will set the foreground color of the status bar
  based on the battery charge level and status
* #{battery_color_charge_bg} - will set the background color of the
  status bar based solely on the battery charge level
* #{battery_color_charge_fg} - will set the foreground color of the
  status bar based solely on the battery charge level
* #{battery_color_status_bg} - will set the background color of the
  status bar based solely on the battery status
* #{battery_color_status_fg} - will set the foreground color of the
  status bar based solely on the battery status
* #{battery_icon_charge} - will display a battery charge icon
* #{battery_icon_status} - will display a battery status icon

Removed 2 format strings:
* #{battery_status_bg}
* #{battery_status_fg}

Added 39 new options:
* @batt_color_charge_primary_tier8 - primary color to show at [95%-100%]
  battery charge
* @batt_color_charge_primary_tier7 - primary color to show at [80%-95%)
  battery charge
* @batt_color_charge_primary_tier6 - primary color to show at [65%-90%)
  battery charge
* @batt_color_charge_primary_tier5 - primary color to show at [50%-65%)
  battery charge
* @batt_color_charge_primary_tier4 - primary color to show at [35%-50%)
  battery charge
* @batt_color_charge_primary_tier3 - primary color to show at [20%-35%)
  battery charge
* @batt_color_charge_primary_tier2 - primary color to show at [5%-20%)
  battery charge
* @batt_color_charge_primary_tier1 - primary color to show at [0%-5%)
  battery charge
* @batt_color_charge_secondary_tier8 - secondary color to show at [95%-
  100%] battery charge
* @batt_color_charge_secondary_tier7 - secondary color to show at [80%-
  95%) battery charge
* @batt_color_charge_secondary_tier6 - secondary color to show at [65%-
  90%) battery charge
* @batt_color_charge_secondary_tier5 - secondary color to show at [50%-
  65%) battery charge
* @batt_color_charge_secondary_tier4 - secondary color to show at [35%-
  50%) battery charge
* @batt_color_charge_secondary_tier3 - secondary color to show at [20%-
  35%) battery charge
* @batt_color_charge_secondary_tier2 - secondary color to show at [5%-
  20%) battery charge
* @batt_color_charge_secondary_tier1 - secondary color to show at [0%-
  5%) battery charge
* @batt_color_status_primary_charged - primary color to show for a
  battery in 'fully-charged' status
* @batt_color_status_primary_charging - primary color to show for a
  battery in 'charging' status
* @batt_color_status_primary_discharging - primary color to show for a
  battery in 'discharging' status
* @batt_color_status_primary_attached - primary color to show for a
  battery in 'attached' status
* @batt_color_status_primary_unknown - primary color to show for a
  battery in an unknown status
* @batt_color_status_secondary_charged - secondary color to show for a
  battery in 'fully-charged' status
* @batt_color_status_secondary_charging - secondary color to show for a
  battery in 'charging' status
* @batt_color_status_secondary_discharging - secondary color to show for
  a battery in 'discharging' status
* @batt_color_status_secondary_attached - secondary color to show for a
  battery in 'attached' status
* @batt_color_status_secondary_unknown - secondary color to show for a
  battery in an unknown status
* @batt_icon_charge_tier8 - icon to show at [95%-100%] battery charge
* @batt_icon_charge_tier7 - icon to show at [80%-95%) battery charge
* @batt_icon_charge_tier6 - icon to show at [65%-90%) battery charge
* @batt_icon_charge_tier5 - icon to show at [50%-65%) battery charge
* @batt_icon_charge_tier4 - icon to show at [35%-50%) battery charge
* @batt_icon_charge_tier3 - icon to show at [20%-35%) battery charge
* @batt_icon_charge_tier2 - icon to show at [5%-20%) battery charge
* @batt_icon_charge_tier1 - icon to show at [0%-5%) battery charge
* @batt_icon_status_charged - icon to show for a battery in 'fully-
  charged' status
* @batt_icon_status_charging - icon to show for a battery in 'charging'
  status
* @batt_icon_status_discharging - icon to show for a battery in
  'discharging' status
* @batt_icon_status_attached - icon to show for a battery in 'attached'
  status
* @batt_icon_status_unknown - icon to show for a battery in an unknown
  status

Removed 12 options:
* @batt_charged_icon
* @batt_charging_icon
* @batt_attached_icon
* @batt_full_charge_icon
* @batt_high_charge_icon
* @batt_medium_charge_icon
* @batt_low_charge_icon
* @batt_color_full_charge
* @batt_color_high_charge
* @batt_color_medium_charge
* @batt_color_low_charge
* @batt_color_charging

Format string notes:
* #{battery_color_bg} and #{battery_color_fg} replace
  #{battery_status_bg} and #{battery_status_fg}, respectively and
  continue to act the same way
* #{battery_color_charge_bg/fg} changes colors based ONLY on the battery
  charge level
* #{battery_color_status_bg/fg} changes colors based ONLY on the battery
  status
* #{battery_icon_charge} displays ONLY an icon for the charge level
* #{battery_icon_status} displays ONLY an icon for the charge status
* #{battery_icon} continues to display both

Option notes:
* @batt_color_..._primary options are what will be displayed in the main
  bg or fg you choose - e.g. if you use #{battery_color_bg}, the primary
  color will be the background
* Likewise, the corresponding @batt_color_..._secondary option will be
  the foreground in the example above

* Fixed #{battery_graph} format string

Previously, the #{battery_graph} format string wouldn't show an actual
graph. This has been fixed. The graph is also 5 characters wide now,
regardless of battery charge level.

* Clarified some language in README.md

* Changed a word because words are hard
2019-05-31 15:38:32 +02:00
Bruno Sutic 8e5f5ab4c6
Update README.md 2019-04-19 12:43:26 +02:00
Martin Beentjes ac1c768e07
Update README.md 2019-04-13 14:00:31 +02:00
Dan Cassidy d163ef37a6 Changed acpi to be preferred over upower (#78) 2019-03-27 21:29:14 +01:00
Franz Bettag 588a941cde adds support for OpenBSD apm. (#77) 2019-03-11 11:05:40 +01:00
Malcolm Scott cc18faec34 If upower reports a percentage, just use that (#76)
I have a device (Gemini PDA) whose energy and energy-full are both zero, but the percentage is correct so just use that.  Upower probably knows best.  Probably also fixes #65.
2019-03-11 10:53:25 +01:00
Diego Ximenes c60c41c826 Fix batt_remain_short behavior when using upower. (#73) 2019-01-14 11:28:20 +01:00
Yigong Wang 5a3d235119 Fix the "charged" icon for some environments (#72) 2018-12-09 12:56:22 +01:00
Yishen Miao 8cb3cce426 Adding status_bg for charging (#71)
a follow-up to  #68. The background color is added for charging status.
2018-11-26 14:31:34 +01:00
hulkk c2cd5c8687 fix time to charge display on mac, closes #28 (#68)
* fix time to charge display on mac, closes #28
* add charging colour to battery_status_fg
* fix incorrect colours when discharging
* change charging colour default to green
* add text to show charged when applicable instead of 0:00
2018-08-03 10:03:23 +02:00
Johannes Wikner 8d5adf92fd Use DisplayDevice if available (#67)
For laptops with multiple batteries, DisplayDevice should be
  available, otherwise stick to the old behaviour.

  See https://upower.freedesktop.org/docs/UPower.html#id-1.2.3.8.5
2018-07-25 21:36:53 +02:00
Martin Beentjes db0698ea86
Update README.md 2018-06-12 15:27:53 +02:00
Igor de Alcantara Barroso 24f088fe5b Add dependencies to the readme (#66) 2018-06-12 15:23:10 +02:00
Aaron Zeng 09be78c35e Substitute all occurrences of variables (#63) (#64)
This commit addresses #63 by making sure all occurrences of each
variables in the status line are substituted.
2017-10-24 08:59:56 +02:00
Michael Foley b4cf61da23 Remove deprecated icon setting in README (#62)
`batt_discharging_icon` was removed in 215de6d
2017-10-10 09:32:07 +02:00
Thomas Gordon Lowrey IV 8da22116eb termux initial support (#61) 2017-10-01 18:12:26 +02:00
Cédric Michaux 267d960601 Allow to put colors in discharging icons (#60) 2017-09-29 08:43:27 +02:00
Dima 215de6dcd2 Use battery percentage icons (#59)
* Add dynamic battery level icons
* Update README to use correct battery icon variables
2017-09-23 23:23:12 +02:00
hanfried b630ec8707 Assume no battery percentage as ok, assume it might be a desktop (#58) 2017-08-28 14:10:44 +02:00
Philipp Schmitt 51499f1cc7 Fix multiple batteries on linux (#57) 2017-08-22 11:40:34 +02:00
Johan Bleuzen 7810c8d7ec Fix: Don't process empty batteries variable (#56) 2017-07-13 10:12:49 +02:00
Justin Campbell d64c7fecc8 Fix missing argument to awk (#53)
Fixes time remaining when short=true
2017-07-03 16:54:57 +02:00
Justin Campbell 3d7cfed467 Add @batt_remain_short variable (#52)
* Remove format string count
* Add variable @batt_remain_short

Hides output if battery is charging or charged, and shortens the discharging output to ~H:MM.
2017-06-26 16:54:27 +02:00
Camille TJHOA 8fa3b627b6 Fix typo (#51) 2017-05-08 10:46:59 +02:00
David Vass 9a4e6ef8e4 Show time to charge battery on Mac OS (#46)
* Show time to charge battery on Mac OS

* Rename  variable to
2017-04-20 22:59:10 +02:00
Martin Beentjes 49ff2993be Update README.md
Added Aleksandar Djurdjic to the list of contributors.
2017-01-30 21:14:48 +01:00
Martin Beentjes 06688f8a64 Update README.md
Added the foreground color option
2017-01-30 21:12:11 +01:00
Aleksandar Djurdjic c7034799bc Adapted script files to also print colored fg sequence (#47) 2017-01-30 21:11:00 +01:00
Martin Beentjes f121a2368c Update README.md 2017-01-28 12:32:47 +01:00
Martin Beentjes 0a2d6d02e4 Update README.md
Restructured the README page. The example pictures will be resized to a constant size.
2017-01-28 12:03:50 +01:00
Martin Beentjes a3a955e782 Update README.md
Added a list of the contributors to the README.md
2017-01-02 15:31:09 +01:00
Tom Levens 629902d532 Fix simplified interpolation in bash < 4.0 (#39)
Bash versions < 4.0 do not support associative arrays and MacOS Sierra
still ships with Bash 3.2. This splits the interpolation text and
command into two lists. While this is not as nice, it is supported by
any version of bash.
2016-10-21 11:59:39 +02:00
Tom Levens af7b4be994 Fixed graph when no battery available (#38) 2016-10-21 11:59:32 +02:00
Tom Levens af907c2ceb Added graph formatting option, simplify interpolation (#37)
* Added prefix/suffix scripts and graph using Spark
* Reformatted indents, updated README.md
* Updated variable names & docs
* Change to bash C-style for loop
* Fix spaces to tabs
* Fixed spaces to tabs in battery_graph.sh
* Added missing ""
2016-10-20 00:12:03 +02:00
Martin Beentjes 1df74d095d Update CHANGELOG.md 2016-10-05 11:29:55 +02:00
Joseph fe54f2eb66 Chromebook Support for battery stat (#22)
* chromebook check by /sys/class/chromeos/cros_ecs

By checking the existence of the file `/sys/class/chromeos/cros_ecs` (Chrome OS Embedded Controller), one can determine if the machine is a Chromebook. Every Chromebook using the armv7 and x86 architecture will have this file.
2016-10-05 11:29:04 +02:00
Martin Beentjes 926bfd51fd Update CHANGELOG.md 2016-10-02 17:13:11 +02:00
Evan N-D e70f252bda Fix multi-battery output with upower (#36)
Given multiple batteries in a machine, we will combine the energy and
energy-full values from each battery, then divide the total energy by
the total energy-full, and multiple by 100 to get a percentage. We use
awk to avoid introducing new dependencies, and to also handle the
floating point arithmetic and eventual truncation. This code will always
round your current percentage down.
2016-10-02 17:12:37 +02:00
Martin Beentjes 77760e09af Update README.md 2016-10-01 16:37:28 +02:00
Martin Beentjes 1f7686e0ae Update CHANGELOG.md 2016-10-01 16:14:38 +02:00
Ryan Frantz ce8d85440e Adds support for changing bg color based on battery percentage. (#29)
* Adds support for changing bg color based on battery percentage.

- If #{battery_status_bg} is used, the background of this plugin's status bar will be:
- 'green' for 100% charged
- 'yellow' for 99% to 51% charge
- orange ('colour208') for 50% to 16% charge
- 'red' for 15% to 0% charge

* Adds support for background colors set in user's tmux config.

- The background colors can be configured via 'batt_color_full_charge', 'batt_color_high_charge', 'batt_color_medium_charge', and 'batt_color_low_charge'.
- Sane defaults are provided as well.
2016-10-01 16:12:14 +02:00
Martin Beentjes ae8deefe57 Update README.md 2016-09-28 22:41:23 +02:00
38 changed files with 887 additions and 138 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.swp

View file

@ -1,7 +1,20 @@
# Changelog
### master
### v2.0.0, 2025-12-30
- Added `#{battery_charging_watts}` format string for displaying current watts supplied on macOS
- Added `#{battery_color_bg}`, `#{battery_color_fg}` format strings for dynamic color based on charge/status
- Added `#{battery_color_charge_bg}`, `#{battery_color_charge_fg}` for colors based solely on charge level
- Added `#{battery_color_status_bg}`, `#{battery_color_status_fg}` for colors based solely on battery status
- Added `#{battery_icon_charge}`, `#{battery_icon_status}` for granular icon control
- Added 8-tier charge level icons and colors with full customization support
- Added `#{battery_graph}` to display battery as a bar graph
- Added `@batt_remain_short` option to shorten remaining time display
- Added WSL (Windows Subsystem for Linux) support
- Added OpenBSD `apm` support
- Added `battery_enabled.tmux` script for conditionally showing battery
- Fixed battery graph display
- Removed deprecated `#{battery_status_bg}` and `#{battery_status_fg}` format strings
- Changed preferred order of utility applications to: pmset → acpi → upower → termux-battery-status due to CPU usage issues with upower
### v1.2.0, 2016-09-24
- show output for `#{battery_remain}` interpolation only if the battery is

293
README.md
View file

@ -2,39 +2,26 @@
Enables displaying battery percentage and status icon in tmux status-right.
Battery full (for OS X):<br/>
![battery full](/screenshots/battery_full.png)
## Installation
Battery discharging, custom discharge icon:<br/>
![battery discharging, custom icon](/screenshots/battery_discharging.png)
In order to read the battery status, this plugin depends on having one of the following applications installed:
- pmset (MacOS only)
- acpi
- upower
- termux-battery-status
- apm
Battery charging:<br/>
![battery charging](/screenshots/battery_charging.png)
Battery remain:<br/>
![battery remain](/screenshots/battery_remain.png)
This is done by introducing 3 new format strings that can be added to
`status-right` option:
- `#{battery_icon}` - will display a battery status icon
- `#{battery_percentage}` - will show battery percentage
- `#{battery_remain}` - will show remaining time of battery charge
### Usage
Add `#{battery_icon}`, `#{battery_percentage}` or `#{battery_remain}` format
strings to existing `status-right` tmux option. Example:
# in .tmux.conf
set -g status-right 'Batt: #{battery_icon} #{battery_percentage} #{battery_remain} | %a %h-%d %H:%M '
In a normal situation, one of the above should be installed on your system by default and thus it should not be necessary to specifically install one of them. That being said, the `acpi` utility is currently recommended for use over `upower` where possible due to ongoing CPU usage issues.
### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended)
Add plugin to the list of TPM plugins in `.tmux.conf`:
set -g @plugin 'tmux-plugins/tmux-battery'
```tmux
set -g @plugin 'tmux-plugins/tmux-battery'
```
Hit `prefix + I` to fetch the plugin and source it.
Hit `<prefix> + I` to fetch the plugin and source it.
If format strings are added to `status-right`, they should now be visible.
@ -42,67 +29,245 @@ If format strings are added to `status-right`, they should now be visible.
Clone the repo:
$ git clone https://github.com/tmux-plugins/tmux-battery ~/clone/path
```shell
git clone https://github.com/tmux-plugins/tmux-battery ~/clone/path
```
Add this line to the bottom of `.tmux.conf`:
run-shell ~/clone/path/battery.tmux
```tmux
run-shell ~/clone/path/battery.tmux
```
Reload TMUX environment:
From the terminal, reload TMUX environment:
# type this in terminal
$ tmux source-file ~/.tmux.conf
```shell
tmux source-file ~/.tmux.conf
```
If format strings are added to `status-right`, they should now be visible.
### Changing icons
## Usage
By default, these icons are displayed:
Add any of the supported format strings (see below) to the `status-right` tmux option in `.tmux.conf`. Example:
- charged: ":battery:" ("❇ " when not on OS X)
- charging: ":zap:"
- discharging: (nothing shown)
- attached but not charging: ":warning:"
```tmux
set -g status-right '#{battery_color_bg} Batt: #{battery_icon} #{battery_percentage} #{battery_remain} | %a %h-%d %H:%M '
```
You can change these defaults by adding the following to `.tmux.conf` (the
following lines are not in the code block so that emojis can be seen):
### Supported Format Strings
- set -g @batt_charged_icon ":sunglasses:"
- set -g @batt_charging_icon ":+1:"
- set -g @batt_discharging_icon ":thumbsdown:"
- set -g @batt_attached_icon ":neutral_face:"
- `#{battery_color_bg}` - will set the background color of the status bar based on the battery charge level if discharging and status otherwise
- `#{battery_color_fg}` - will set the foreground color of the status bar based on the battery charge level if discharging and status otherwise
- `#{battery_color_charge_bg}` - will set the background color of the status bar based solely on the battery charge level
- `#{battery_color_charge_fg}` - will set the foreground color of the status bar based solely on the battery charge level
- `#{battery_color_status_bg}` - will set the background color of the status bar based solely on the battery status
- `#{battery_color_status_fg}` - will set the foreground color of the status bar based solely on the battery status
- `#{battery_graph}` - will show battery percentage as a bar graph: ▁▂▄▆█
- `#{battery_icon}` - will display a battery status/charge icon
- `#{battery_icon_charge}` - will display a battery charge icon
- `#{battery_icon_status}` - will display a battery status icon
- `#{battery_percentage}` - will show battery percentage
- `#{battery_remain}` - will show remaining time of battery charge\*
- `#{battery_charging_watts}` - will display the current watts supplied (currently supported only on macOS)
Don't forget to reload tmux environment (`$ tmux source-file ~/.tmux.conf`)
after you do this.
\* These format strings can be further customized via options as described below.
### Limitations
#### Options
- Battery icon change most likely won't be instant.<br/>
When you un-plug power cord it will take some time (15 - 60 seconds) for the
icon to change. This depends on the `status-interval` tmux option. Setting it
to 15 seconds should be good enough.
`#{battery_remain}`
### Other goodies
- `@batt_remain_short`: 'true' / 'false' - This will shorten the time remaining (when charging or discharging) to `~H:MM`.
You might also find these useful:
### Defaults
- [resurrect](https://github.com/tmux-plugins/tmux-resurrect) - restore tmux
environment after system restart
- [logging](https://github.com/tmux-plugins/tmux-logging) - easy logging and
screen capturing
- [online status](https://github.com/tmux-plugins/tmux-online-status) - online status
indicator in tmux `status-right`. Useful when on flaky connection to see if
you're online.
#### Options
You might want to follow [@brunosutic](https://twitter.com/brunosutic) on
twitter if you want to hear about new tmux plugins or feature updates.
- `@batt_remain_short`: 'false'
#### Icons/Colors
By default, the following colors and icons are used. (The exact colors displayed depends on your terminal / X11 config.)
Please be aware that the 'level of charge' as noted below (e.g. `[80%-95%)`) uses interval notation. If you are unfamiliar with it, <a href="https://en.wikipedia.org/wiki/Bracket_(mathematics)#Intervals">check it out here</a>.
Also, a note about the `@batt_color_...` options: `@batt_color_..._primary_...` options are what will be displayed in the main `bg` or `fg` format strings you choose - e.g. if you use `#{battery_color_bg}`, the `@batt_color_..._primary_...` colors you choose will be the background. Likewise, the corresponding `@batt_color_..._secondary_...` color will be the foreground.
Level of Charge Colors:
- primary tier 8 \[95%-100%] (`@batt_color_charge_primary_tier8`): '#00ff00'
- primary tier 7 \[80%-95%) (`@batt_color_charge_primary_tier7`): '#55ff00'
- primary tier 6 \[65%-80%) (`@batt_color_charge_primary_tier6`): '#aaff00'
- primary tier 5 \[50%-65%) (`@batt_color_charge_primary_tier5`): '#ffff00'
- primary tier 4 \[35%-50%) (`@batt_color_charge_primary_tier4`): '#ffc000'
- primary tier 3 \[20%-35%) (`@batt_color_charge_primary_tier3`): '#ff8000'
- primary tier 2 (5%-20%) (`@batt_color_charge_primary_tier2`): '#ff4000'
- primary tier 1 \[0%-5%] (`@batt_color_charge_primary_tier1`): '#ff0000'
- secondary tier 8 \[95%-100%] (`@batt_color_charge_secondary_tier8`): 'colour0'
- secondary tier 7 \[80%-95%) (`@batt_color_charge_secondary_tier7`): 'colour0'
- secondary tier 6 \[65%-80%) (`@batt_color_charge_secondary_tier6`): 'colour0'
- secondary tier 5 \[50%-65%) (`@batt_color_charge_secondary_tier5`): 'colour0'
- secondary tier 4 \[35%-50%) (`@batt_color_charge_secondary_tier4`): 'colour0'
- secondary tier 3 \[20%-35%) (`@batt_color_charge_secondary_tier3`): 'colour0'
- secondary tier 2 (5%-20%) (`@batt_color_charge_secondary_tier2`): 'colour0'
- secondary tier 1 \[0%-5%] (`@batt_color_charge_secondary_tier1`): 'colour0'
Status Colors:
- primary charged (`@batt_color_status_primary_charged`): 'colour33'
- primary charging (`@batt_color_status_primary_charging`): 'colour33'
- primary discharging (`@batt_color_status_primary_discharging`): 'colour14'
- primary attached (`@batt_color_status_primary_attached`): 'colour201'
- primary unknown (`@batt_color_status_primary_unknown`): 'colour7'
- secondary charged (`@batt_color_status_secondary_charged`): 'colour0'
- secondary charging (`@batt_color_status_secondary_charging`): 'colour0'
- secondary discharging (`@batt_color_status_secondary_discharging`): 'colour0'
- secondary attached (`@batt_color_status_secondary_attached`): 'colour0'
- secondary unknown (`@batt_color_status_secondary_unknown`): 'colour0'
Level of Charge Icons:
- tier 8 \[95%-100%] (`@batt_icon_charge_tier8`): '█'
- tier 7 \[80%-95%) (`@batt_icon_charge_tier7`): '▇'
- tier 6 \[65%-80%) (`@batt_icon_charge_tier6`): '▆'
- tier 5 \[50%-65%) (`@batt_icon_charge_tier5`): '▅'
- tier 4 \[35%-50%) (`@batt_icon_charge_tier4`): '▄'
- tier 3 \[20%-35%) (`@batt_icon_charge_tier3`): '▃'
- tier 2 (5%-20%) (`@batt_icon_charge_tier2`): '▂'
- tier 1 \[0%-5%] (`@batt_icon_charge_tier1`): '▁'
Status Icons:
- charged (`@batt_icon_status_charged`): '🔌'
- charged - OS X (`@batt_icon_status_charged`): '🔌'
- charging (`@batt_icon_status_charging`): '🔌'
- discharging (`@batt_icon_status_discharging`): '🔋'
- attached (`@batt_icon_status_attached`): '⚠️'
- unknown (`@batt_icon_status_unknown`): '?'
#### Enabled
If you run `battery_enabled.tmux` from the top-level of the repo, it will setup a `@battery_enabled` variable, that can be used to selectively display the battery based on whether or not a battery is available or not.
```tmux
run-shell ~/clone/path/battery_enabled.tmux
# for example, if using catppuccin:
set -agF status-right "#{?#{#{==:#{E:@battery_exists},true}},#{E:@catppuccin_status_battery},}"
run-shell ~/clone/path/battery.tmux
```
#### Changing the Defaults
All efforts have been made to make sane defaults, but if you wish to change any of them, add the option to `.tmux.conf`. For example:
```tmux
set -g @batt_icon_charge_tier8 '🌕'
set -g @batt_icon_charge_tier7 '🌖'
set -g @batt_icon_charge_tier6 '🌖'
set -g @batt_icon_charge_tier5 '🌗'
set -g @batt_icon_charge_tier4 '🌗'
set -g @batt_icon_charge_tier3 '🌘'
set -g @batt_icon_charge_tier2 '🌘'
set -g @batt_icon_charge_tier1 '🌑'
set -g @batt_icon_status_charged '🔋'
set -g @batt_icon_status_charging '⚡'
set -g @batt_icon_status_discharging '👎'
set -g @batt_color_status_primary_charged '#3daee9'
set -g @batt_color_status_primary_charging '#3daee9'
```
Don't forget to reload the tmux environment after you do this by either hitting `<prefix> + I` if tmux battery is installed via the tmux plugin manager, or by typing `tmux source-file ~/.tmux.conf` in the terminal if tmux battery is manually installed.
*Warning*: The battery icon change most likely will not be instant. When you un-plug the power cord, it will take some time (15 - 60 seconds) for the icon to change. This depends on the `status-interval` tmux option. Setting it to 15 seconds should be good enough.
## Examples
These are all examples of the default plugin color and icon schemes paired with the default tmux color scheme using the following `status-right` and `status-right-length` settings in `.tmux.conf`
```tmux
set -g status-right 'Colors: #{battery_color_bg}bg#[default] #{battery_color_fg}fg#[default] #{battery_color_charge_bg}charge_bg#[default] #{battery_color_charge_fg}charge_fg#[default] #{battery_color_status_bg}status_bg#[default] #{battery_color_status_fg}status_fg#[default] | Graph: #{battery_graph} | Icon: #{battery_icon} | Charge Icon: #{battery_icon_charge} | Status Icon: #{battery_icon_status} | Percent: #{battery_percentage} | Remain: #{battery_remain}'
set -g status-right-length '150'
```
Battery charging at tier 8 \[95%-100%]:<br>
![battery-charging-tier8](./screenshots/battery_charging_tier8.png)
Battery charging at tier 7 \[80%-95%):<br>
![battery-charging-tier7](./screenshots/battery_charging_tier7.png)
Battery charging at tier 6 \[65%-80%):<br>
![battery-charging-tier6](./screenshots/battery_charging_tier6.png)
Battery charging at tier 5 \[50%-65%):<br>
![battery-charging-tier5](./screenshots/battery_charging_tier5.png)
Battery charging at tier 4 \[35%-50%):<br>
![battery-charging-tier4](./screenshots/battery_charging_tier4.png)
Battery charging at tier 3 \[20%-35%):<br>
![battery-charging-tier3](./screenshots/battery_charging_tier3.png)
Battery charging at tier 2 (5%-20%):<br>
![battery-charging-tier2](./screenshots/battery_charging_tier2.png)
Battery charging at tier 1 \[0%-5%]:<br>
![battery-charging-tier1](./screenshots/battery_charging_tier1.png)
Battery discharging at tier 8 \[95%-100%]:<br>
![battery-discharging-tier8](./screenshots/battery_discharging_tier8.png)
Battery discharging at tier 7 \[80%-95%):<br>
![battery-discharging-tier7](./screenshots/battery_discharging_tier7.png)
Battery discharging at tier 6 \[65%-80%):<br>
![battery-discharging-tier6](./screenshots/battery_discharging_tier6.png)
Battery discharging at tier 5 \[50%-65%):<br>
![battery-discharging-tier5](./screenshots/battery_discharging_tier5.png)
Battery discharging at tier 4 \[35%-50%):<br>
![battery-discharging-tier4](./screenshots/battery_discharging_tier4.png)
Battery discharging at tier 3 \[20%-35%):<br>
![battery-discharging-tier3](./screenshots/battery_discharging_tier3.png)
Battery discharging at tier 2 (5%-20%):<br>
![battery-discharging-tier2](./screenshots/battery_discharging_tier2.png)
Battery discharging at tier 1 \[0%-5%]:<br>
![battery-discharging-tier1](./screenshots/battery_discharging_tier1.png)
Battery in 'attached' status:<br>
![battery-status-attached](./screenshots/battery_status_attached.png)
Battery in an unknown status:<br>
![battery-status-unknown](./screenshots/battery_status_unknown.png)
### Tmux Plugins
This plugin is part of the [tmux-plugins](https://github.com/tmux-plugins) organisation. Checkout plugins as [resurrect](https://github.com/tmux-plugins/tmux-resurrect), [logging](https://github.com/tmux-plugins/tmux-logging), [online status](https://github.com/tmux-plugins/tmux-online-status), and many more over at the [tmux-plugins](https://github.com/tmux-plugins) organisation page.
### Maintainer
- [Martin Beentjes](https://github.com/martinbeentjes)
### Contributors
- [@jgeralnik](https://github.com/jgeralnik)
- [@m1foley](https://github.com/m1foley)
- [@asethwright](https://github.com/asethwright)
- [@JanAhrens](https://github.com/JanAhrens)
- Adam Biggs
- Aleksandar Djurdjic
- Bruno Sutic
- Caleb
- Dan Cassidy
- Diego Ximenes
- Evan N-D
- Jan Ahrens
- Joey Geralnik
- HyunJong (Joseph) Lee
- Martin Beentjes
- Mike Foley
- Ryan Frantz
- Seth Wright
- Tom Levens
### License

View file

@ -4,12 +4,37 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
battery_percentage="#($CURRENT_DIR/scripts/battery_percentage.sh)"
battery_remain="#($CURRENT_DIR/scripts/battery_remain.sh)"
battery_icon="#($CURRENT_DIR/scripts/battery_icon.sh)"
battery_percentage_interpolation="\#{battery_percentage}"
battery_remain_interpolation="\#{battery_remain}"
battery_icon_interpolation="\#{battery_icon}"
battery_interpolation=(
"\#{battery_color_bg}"
"\#{battery_color_fg}"
"\#{battery_color_charge_bg}"
"\#{battery_color_charge_fg}"
"\#{battery_color_status_bg}"
"\#{battery_color_status_fg}"
"\#{battery_graph}"
"\#{battery_icon}"
"\#{battery_icon_charge}"
"\#{battery_icon_status}"
"\#{battery_percentage}"
"\#{battery_remain}"
"\#{battery_charging_watts}"
)
battery_commands=(
"#($CURRENT_DIR/scripts/battery_color.sh bg)"
"#($CURRENT_DIR/scripts/battery_color.sh fg)"
"#($CURRENT_DIR/scripts/battery_color_charge.sh bg)"
"#($CURRENT_DIR/scripts/battery_color_charge.sh fg)"
"#($CURRENT_DIR/scripts/battery_color_status.sh bg)"
"#($CURRENT_DIR/scripts/battery_color_status.sh fg)"
"#($CURRENT_DIR/scripts/battery_graph.sh)"
"#($CURRENT_DIR/scripts/battery_icon.sh)"
"#($CURRENT_DIR/scripts/battery_icon_charge.sh)"
"#($CURRENT_DIR/scripts/battery_icon_status.sh)"
"#($CURRENT_DIR/scripts/battery_percentage.sh)"
"#($CURRENT_DIR/scripts/battery_remain.sh)"
"#($CURRENT_DIR/scripts/battery_charging_watts.sh)"
)
set_tmux_option() {
local option="$1"
@ -18,10 +43,10 @@ set_tmux_option() {
}
do_interpolation() {
local string="$1"
local percentage_interpolated="${string/$battery_percentage_interpolation/$battery_percentage}"
local remain_interpolated="${percentage_interpolated/$battery_remain_interpolation/$battery_remain}"
local all_interpolated="${remain_interpolated/$battery_icon_interpolation/$battery_icon}"
local all_interpolated="$1"
for ((i=0; i<${#battery_commands[@]}; i++)); do
all_interpolated=${all_interpolated//${battery_interpolation[$i]}/${battery_commands[$i]}}
done
echo "$all_interpolated"
}
@ -33,7 +58,10 @@ update_tmux_option() {
}
main() {
update_tmux_option "status-right"
update_tmux_option "status-left"
local interpolated_options="$(get_tmux_option "@plugin_interpolated_options" "status-right status-left")"
for interpolated_option in $interpolated_options
do
update_tmux_option $interpolated_option
done
}
main

10
battery_enabled.tmux Executable file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
main() {
tmux set-option -gq "@battery_exists" "$( [ -n "$(battery_status)" ] && echo true || echo false )"
}
main

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
print_battery_charging_watts() {
if is_osx; then
watts=$(system_profiler SPPowerDataType | grep Wattage | awk '{print $3}')
if [ -z "$watts" ]; then
watts="0"
fi
echo "${watts}W"
else
echo ""
fi
}
main() {
print_battery_charging_watts
}
main

23
scripts/battery_color.sh Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
print_color() {
local plane="$1"
local status="$2"
if [ "$status" == "discharging" ]; then
$CURRENT_DIR/battery_color_charge.sh "$plane"
else
$CURRENT_DIR/battery_color_status.sh "$plane" "$status"
fi
}
main() {
local status="$(battery_status)"
local plane="$1"
print_color "$plane" "$status"
}
main $@

98
scripts/battery_color_charge.sh Executable file
View file

@ -0,0 +1,98 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
# script global variables
color_charge_primary_tier8=''
color_charge_primary_tier7=''
color_charge_primary_tier6=''
color_charge_primary_tier5=''
color_charge_primary_tier4=''
color_charge_primary_tier3=''
color_charge_primary_tier2=''
color_charge_primary_tier1=''
color_charge_secondary_tier8=''
color_charge_secondary_tier7=''
color_charge_secondary_tier6=''
color_charge_secondary_tier5=''
color_charge_secondary_tier4=''
color_charge_secondary_tier3=''
color_charge_secondary_tier2=''
color_charge_secondary_tier1=''
# script default variables
color_charge_primary_tier8_default='#00ff00'
color_charge_primary_tier7_default='#55ff00'
color_charge_primary_tier6_default='#aaff00'
color_charge_primary_tier5_default='#ffff00'
color_charge_primary_tier4_default='#ffc000'
color_charge_primary_tier3_default='#ff8000'
color_charge_primary_tier2_default='#ff4000'
color_charge_primary_tier1_default='#ff0000'
color_charge_secondary_tier8_default='colour0'
color_charge_secondary_tier7_default='colour0'
color_charge_secondary_tier6_default='colour0'
color_charge_secondary_tier5_default='colour0'
color_charge_secondary_tier4_default='colour0'
color_charge_secondary_tier3_default='colour0'
color_charge_secondary_tier2_default='colour0'
color_charge_secondary_tier1_default='colour0'
# colors are set as script global variables
get_color_charge_settings() {
color_charge_primary_tier8=$(get_tmux_option "@batt_color_charge_primary_tier8" "$color_charge_primary_tier8_default")
color_charge_primary_tier7=$(get_tmux_option "@batt_color_charge_primary_tier7" "$color_charge_primary_tier7_default")
color_charge_primary_tier6=$(get_tmux_option "@batt_color_charge_primary_tier6" "$color_charge_primary_tier6_default")
color_charge_primary_tier5=$(get_tmux_option "@batt_color_charge_primary_tier5" "$color_charge_primary_tier5_default")
color_charge_primary_tier4=$(get_tmux_option "@batt_color_charge_primary_tier4" "$color_charge_primary_tier4_default")
color_charge_primary_tier3=$(get_tmux_option "@batt_color_charge_primary_tier3" "$color_charge_primary_tier3_default")
color_charge_primary_tier2=$(get_tmux_option "@batt_color_charge_primary_tier2" "$color_charge_primary_tier2_default")
color_charge_primary_tier1=$(get_tmux_option "@batt_color_charge_primary_tier1" "$color_charge_primary_tier1_default")
color_charge_secondary_tier8=$(get_tmux_option "@batt_color_charge_secondary_tier8" "$color_charge_secondary_tier8_default")
color_charge_secondary_tier7=$(get_tmux_option "@batt_color_charge_secondary_tier7" "$color_charge_secondary_tier7_default")
color_charge_secondary_tier6=$(get_tmux_option "@batt_color_charge_secondary_tier6" "$color_charge_secondary_tier6_default")
color_charge_secondary_tier5=$(get_tmux_option "@batt_color_charge_secondary_tier5" "$color_charge_secondary_tier5_default")
color_charge_secondary_tier4=$(get_tmux_option "@batt_color_charge_secondary_tier4" "$color_charge_secondary_tier4_default")
color_charge_secondary_tier3=$(get_tmux_option "@batt_color_charge_secondary_tier3" "$color_charge_secondary_tier3_default")
color_charge_secondary_tier2=$(get_tmux_option "@batt_color_charge_secondary_tier2" "$color_charge_secondary_tier2_default")
color_charge_secondary_tier1=$(get_tmux_option "@batt_color_charge_secondary_tier1" "$color_charge_secondary_tier1_default")
}
print_color_charge() {
local primary_plane="$1"
local secondary_plane=""
if [ "$primary_plane" == "bg" ]; then
secondary_plane="fg"
else
secondary_plane="bg"
fi
percentage=$($CURRENT_DIR/battery_percentage.sh | sed -e 's/%//')
if [ $percentage -ge 95 -o "$percentage" == "" ]; then
# if percentage is empty, assume it's a desktop
printf "#[$primary_plane=$color_charge_primary_tier8${color_charge_secondary_tier8:+",$secondary_plane=$color_charge_secondary_tier8"}]"
elif [ $percentage -ge 80 ]; then
printf "#[$primary_plane=$color_charge_primary_tier7${color_charge_secondary_tier7:+",$secondary_plane=$color_charge_secondary_tier7"}]"
elif [ $percentage -ge 65 ]; then
printf "#[$primary_plane=$color_charge_primary_tier6${color_charge_secondary_tier6:+",$secondary_plane=$color_charge_secondary_tier6"}]"
elif [ $percentage -ge 50 ]; then
printf "#[$primary_plane=$color_charge_primary_tier5${color_charge_secondary_tier5:+",$secondary_plane=$color_charge_secondary_tier5"}]"
elif [ $percentage -ge 35 ]; then
printf "#[$primary_plane=$color_charge_primary_tier4${color_charge_secondary_tier4:+",$secondary_plane=$color_charge_secondary_tier4"}]"
elif [ $percentage -ge 20 ]; then
printf "#[$primary_plane=$color_charge_primary_tier3${color_charge_secondary_tier3:+",$secondary_plane=$color_charge_secondary_tier3"}]"
elif [ $percentage -gt 5 ]; then
printf "#[$primary_plane=$color_charge_primary_tier2${color_charge_secondary_tier2:+",$secondary_plane=$color_charge_secondary_tier2"}]"
else
printf "#[$primary_plane=$color_charge_primary_tier1${color_charge_secondary_tier1:+",$secondary_plane=$color_charge_secondary_tier1"}]"
fi
}
main() {
local plane="$1"
get_color_charge_settings
print_color_charge "$plane"
}
main $@

74
scripts/battery_color_status.sh Executable file
View file

@ -0,0 +1,74 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
# script global variables
color_status_primary_charged=''
color_status_primary_charging=''
color_status_primary_discharging=''
color_status_primary_attached=''
color_status_primary_unknown=''
color_status_secondary_charged=''
color_status_secondary_charging=''
color_status_secondary_discharging=''
color_status_secondary_attached=''
color_status_secondary_unknown=''
# script default variables
color_status_primary_charged_default='colour33'
color_status_primary_charging_default='colour33'
color_status_primary_discharging_default='colour14'
color_status_primary_attached_default='colour201'
color_status_primary_unknown_default='colour7'
color_status_secondary_charged_default='colour0'
color_status_secondary_charging_default='colour0'
color_status_secondary_discharging_default='colour0'
color_status_secondary_attached_default='colour0'
color_status_secondary_unknown_default='colour0'
# colors are set as script global variables
get_color_status_settings() {
color_status_primary_charged=$(get_tmux_option "@batt_color_status_primary_charged" "$color_status_primary_charged_default")
color_status_primary_charging=$(get_tmux_option "@batt_color_status_primary_charging" "$color_status_primary_charging_default")
color_status_primary_discharging=$(get_tmux_option "@batt_color_status_primary_discharging" "$color_status_primary_discharging_default")
color_status_primary_attached=$(get_tmux_option "@batt_color_status_primary_attached" "$color_status_primary_attached_default")
color_status_primary_unknown=$(get_tmux_option "@batt_color_status_primary_unknown" "$color_status_primary_unknown_default")
color_status_secondary_charged=$(get_tmux_option "@batt_color_status_secondary_charged" "$color_status_secondary_charged_default")
color_status_secondary_charging=$(get_tmux_option "@batt_color_status_secondary_charging" "$color_status_secondary_charging_default")
color_status_secondary_discharging=$(get_tmux_option "@batt_color_status_secondary_discharging" "$color_status_secondary_discharging_default")
color_status_secondary_attached=$(get_tmux_option "@batt_color_status_secondary_attached" "$color_status_secondary_attached_default")
color_status_secondary_unknown=$(get_tmux_option "@batt_color_status_secondary_unknown" "$color_status_secondary_unknown_default")
}
print_color_status() {
local plane_primary="$1"
local plane_secondary=""
if [ "$plane_primary" == "bg" ]; then
plane_secondary="fg"
else
plane_secondary="bg"
fi
local status="$2"
if [[ $status =~ (charged) || $status =~ (full) ]]; then
printf "#[$plane_primary=$color_status_primary_charged${color_status_secondary_charged:+",$plane_secondary=$color_status_secondary_charged"}]"
elif [[ $status =~ (^charging) ]]; then
printf "#[$plane_primary=$color_status_primary_charging${color_status_secondary_charging:+",$plane_secondary=$color_status_secondary_charging"}]"
elif [[ $status =~ (^discharging) ]]; then
printf "#[$plane_primary=$color_status_primary_discharging${color_status_secondary_discharging:+",$plane_secondary=$color_status_secondary_discharging"}]"
elif [[ $status =~ (attached) ]]; then
printf "#[$plane_primary=$color_status_primary_attached${color_status_secondary_attached:+",$plane_secondary=$color_status_secondary_attached"}]"
else
printf "#[$plane_primary=$color_status_primary_unknown${color_status_secondary_unknown:+",$plane_secondary=$color_status_secondary_unknown"}]"
fi
}
main() {
local plane="$1"
local status=${2:-$(battery_status)}
get_color_status_settings
print_color_status "$plane" "$status"
}
main $@

45
scripts/battery_graph.sh Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
print_graph() {
local percentage=$1
if [ $percentage -gt 5 ]; then
printf "▁"
else
printf " "
fi
if [ $percentage -ge 25 ]; then
printf "▂"
else
printf " "
fi
if [ $percentage -ge 50 ]; then
printf "▄"
else
printf " "
fi
if [ $percentage -ge 75 ]; then
printf "▆"
else
printf " "
fi
if [ $percentage -ge 95 ]; then
printf "█"
else
printf " "
echo "▇"
fi
}
main() {
local percentage=$($CURRENT_DIR/battery_percentage.sh)
if [ "$(uname)" == "OpenBSD" ]; then
print_graph ${percentage}
else
print_graph ${percentage%?}
fi
}
main

View file

@ -4,50 +4,18 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
# script global variables
charged_icon=""
charging_icon=""
attached_icon=""
discharging_icon=""
charged_default="❇ "
charged_default_osx="🔋 "
charging_default="⚡️ "
attached_default="⚠️ "
discharging_default=""
charged_default() {
if is_osx; then
echo "$charged_default_osx"
else
echo "$charged_default"
fi
}
# icons are set as script global variables
get_icon_settings() {
charged_icon=$(get_tmux_option "@batt_charged_icon" "$(charged_default)")
charging_icon=$(get_tmux_option "@batt_charging_icon" "$charging_default")
attached_icon=$(get_tmux_option "@batt_attached_icon" "$attached_default")
discharging_icon=$(get_tmux_option "@batt_discharging_icon" "$discharging_default")
}
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"
elif [[ $status =~ (attached) ]]; then
printf "$attached_icon"
if [ "$status" == "discharging" ]; then
$CURRENT_DIR/battery_icon_charge.sh
else
$CURRENT_DIR/battery_icon_status.sh "$status"
fi
}
main() {
get_icon_settings
local status=$(battery_status)
print_icon "$status"
}
main

66
scripts/battery_icon_charge.sh Executable file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
# script global variables
icon_charge_tier8=''
icon_charge_tier7=''
icon_charge_tier6=''
icon_charge_tier5=''
icon_charge_tier4=''
icon_charge_tier3=''
icon_charge_tier2=''
icon_charge_tier1=''
# script default variables
icon_charge_tier8_default='█'
icon_charge_tier7_default='▇'
icon_charge_tier6_default='▆'
icon_charge_tier5_default='▅'
icon_charge_tier4_default='▄'
icon_charge_tier3_default='▃'
icon_charge_tier2_default='▂'
icon_charge_tier1_default='▁'
# icons are set as script global variables
get_icon_charge_settings() {
icon_charge_tier8=$(get_tmux_option "@batt_icon_charge_tier8" "$icon_charge_tier8_default")
icon_charge_tier7=$(get_tmux_option "@batt_icon_charge_tier7" "$icon_charge_tier7_default")
icon_charge_tier6=$(get_tmux_option "@batt_icon_charge_tier6" "$icon_charge_tier6_default")
icon_charge_tier5=$(get_tmux_option "@batt_icon_charge_tier5" "$icon_charge_tier5_default")
icon_charge_tier4=$(get_tmux_option "@batt_icon_charge_tier4" "$icon_charge_tier4_default")
icon_charge_tier3=$(get_tmux_option "@batt_icon_charge_tier3" "$icon_charge_tier3_default")
icon_charge_tier2=$(get_tmux_option "@batt_icon_charge_tier2" "$icon_charge_tier2_default")
icon_charge_tier1=$(get_tmux_option "@batt_icon_charge_tier1" "$icon_charge_tier1_default")
}
print_icon_charge() {
percentage=$($CURRENT_DIR/battery_percentage.sh | sed -e 's/%//')
if [ $percentage -ge 95 -o "$percentage" == "" ]; then
# if percentage is empty, assume it's a desktop
printf "$icon_charge_tier8"
elif [ $percentage -ge 80 ]; then
printf "$icon_charge_tier7"
elif [ $percentage -ge 65 ]; then
printf "$icon_charge_tier6"
elif [ $percentage -ge 50 ]; then
printf "$icon_charge_tier5"
elif [ $percentage -ge 35 ]; then
printf "$icon_charge_tier4"
elif [ $percentage -ge 20 ]; then
printf "$icon_charge_tier3"
elif [ $percentage -gt 5 ]; then
printf "$icon_charge_tier2"
else
printf "$icon_charge_tier1"
fi
}
main() {
get_icon_charge_settings
print_icon_charge
}
main

61
scripts/battery_icon_status.sh Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
# script global variables
icon_status_charged=''
icon_status_charging=''
icon_status_discharging=''
icon_status_attached=''
icon_status_unknown=''
# script default variables
icon_status_charged_default='🔌'
icon_status_charged_default_osx='🔌'
icon_status_charging_default='🔌'
icon_status_discharging_default='🔋'
icon_status_attached_default='⚠️'
icon_status_unknown_default='?'
# determine which charged_default variable to use
get_icon_status_charged_default() {
if is_osx; then
printf "$icon_status_charged_default_osx"
else
printf "$icon_status_charged_default"
fi
}
# icons are set as script global variables
get_icon_status_settings() {
icon_status_charged=$(get_tmux_option "@batt_icon_status_charged" "$(get_icon_status_charged_default)")
icon_status_charging=$(get_tmux_option "@batt_icon_status_charging" "$icon_status_charging_default")
icon_status_discharging=$(get_tmux_option "@batt_icon_status_discharging" "$icon_status_discharging_default")
icon_status_attached=$(get_tmux_option "@batt_icon_status_attached" "$icon_status_attached_default")
icon_status_unknown=$(get_tmux_option "@batt_icon_status_unknown" "$icon_status_unknown_default")
}
print_icon_status() {
local status=$1
if [[ $status =~ (charged) || $status =~ (full) || $status =~ (^finishing charge) ]]; then
printf "$icon_status_charged"
elif [[ $status =~ (^charging) ]]; then
printf "$icon_status_charging"
elif [[ $status =~ (^discharging) ]]; then
printf "$icon_status_discharging"
elif [[ $status =~ (attached) ]]; then
printf "$icon_status_attached"
else
printf "$icon_status_unknown"
fi
}
main() {
get_icon_status_settings
local status=${1:-$(battery_status)}
print_icon_status "$status"
}
main

View file

@ -8,12 +8,36 @@ print_battery_percentage() {
# percentage displayed in the 2nd field of the 2nd row
if command_exists "pmset"; then
pmset -g batt | grep -o "[0-9]\{1,3\}%"
elif command_exists "upower"; then
for battery in $(upower -e | grep battery); do
upower -i $battery | grep percentage | awk '{print $2}'
done | xargs echo
elif command_exists "acpi"; then
acpi -b | grep -Eo "[0-9]+%"
acpi -b | grep -v " 0%" | 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 "$(float2int $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
elif command_exists "termux-battery-status" "jq"; then
termux-battery-status | jq -er '"\(.percentage)%"'
elif is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
cat "$battery"
fi
}

View file

@ -4,29 +4,136 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
short=false
get_remain_settings() {
short=$(get_tmux_option "@batt_remain_short" false)
}
battery_discharging() {
local status="$(battery_status)"
[[ $status =~ (discharging) ]]
}
pmset_battery_remaining_time() {
pmset -g batt | grep -o '[0-9]\{1,2\}:[0-9]\{1,2\}'
battery_charged() {
local status="$(battery_status)"
[[ $status =~ (charged) || $status =~ (full) ]]
}
convertmins() {
((h=${1}/60))
((m=${1}%60))
printf "%02d:%02d\n" $h $m $s
}
apm_battery_remaining_time() {
local remaining_time="$(convertmins $(apm -m))"
if battery_discharging; then
if $short; then
echo $remaining_time | awk '{printf "~%s", $1}'
else
echo $remaining_time | awk '{printf "- %s left", $1}'
fi
elif battery_charged; then
if $short; then
echo $remaining_time | awk '{printf "charged", $1}'
else
echo $remaining_time | awk '{printf "fully charged", $1}'
fi
else
echo "charging"
fi
}
pmset_battery_remaining_time() {
local status="$(pmset -g batt)"
if echo $status | grep 'no estimate' >/dev/null 2>&1; then
if $short; then
echo '~?:??'
else
echo '- Calculating estimate...'
fi
else
local remaining_time="$(echo $status | grep -o '[0-9]\{1,2\}:[0-9]\{1,2\}')"
if battery_discharging; then
if $short; then
echo $remaining_time | awk '{printf "~%s", $1}'
else
echo $remaining_time | awk '{printf "- %s left", $1}'
fi
elif battery_charged; then
if $short; then
echo $remaining_time | awk '{printf "charged", $1}'
else
echo $remaining_time | awk '{printf "fully charged", $1}'
fi
else
if $short; then
echo $remaining_time | awk '{printf "~%s", $1}'
else
echo $remaining_time | awk '{printf "- %s till full", $1}'
fi
fi
fi
}
upower_battery_remaining_time() {
battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
if battery_discharging; then
local remaining_time
remaining_time=$(upower -i "$battery" | grep -E '(remain|time to empty)')
if $short; then
echo "$remaining_time" | awk '{printf "%s %s", $(NF-1), $(NF)}'
else
echo "$remaining_time" | awk '{printf "%s %s left", $(NF-1), $(NF)}'
fi
elif battery_charged; then
if $short; then
echo ""
else
echo "charged"
fi
else
local remaining_time
remaining_time=$(upower -i "$battery" | grep -E 'time to full')
if $short; then
echo "$remaining_time" | awk '{printf "%s %s", $(NF-1), $(NF)}'
else
echo "$remaining_time" | awk '{printf "%s %s to full", $(NF-1), $(NF)}'
fi
fi
}
acpi_battery_remaining_time() {
regex="[0-9]+:[0-9]+"
if ! $short; then
regex="$regex:[0-9]+"
fi
acpi -b | grep -v " 0%" | grep -m 1 -Eo "$regex"
}
print_battery_remain() {
if command_exists "pmset"; then
if is_wsl; then
local charge_full charge_now current_now
charge_full=$(find /sys/class/power_supply/*/charge_full | tail -n1 | xargs cat)
charge_now=$(find /sys/class/power_supply/*/charge_now | tail -n1 | xargs cat)
current_now=$(find /sys/class/power_supply/*/current_now | tail -n1 | xargs cat)
echo $charge_full $charge_now $current_now | awk '{num=($1-$2)/$3; printf "%02d:%02d:%02d", int(num), int(60*num%60), int(3600*num%60)}'
elif command_exists "pmset"; then
pmset_battery_remaining_time
elif command_exists "upower"; then
battery=$(upower -e | grep battery | head -1)
upower -i $battery | grep remain | awk '{print $4}'
elif command_exists "acpi"; then
acpi -b | grep -Eo "[0-9]+:[0-9]+:[0-9]+"
acpi_battery_remaining_time
elif command_exists "upower"; then
upower_battery_remaining_time
elif command_exists "apm"; then
apm_battery_remaining_time
fi
}
main() {
if battery_discharging; then
print_battery_remain
fi
get_remain_settings
print_battery_remain
}
main

60
scripts/helpers.sh Normal file → Executable file
View file

@ -13,20 +13,64 @@ is_osx() {
[ $(uname) == "Darwin" ]
}
is_chrome() {
chrome="/sys/class/chromeos/cros_ec"
if [ -d "$chrome" ]; then
return 0
else
return 1
fi
}
is_wsl() {
version=$(</proc/version)
if [[ "$version" == *"Linux"* || "$version" == *"Microsoft"* || "$version" == *"microsoft"* ]]; then
return 0
else
return 1
fi
}
command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
local command
for command; do
type "$command" >/dev/null 2>&1 || return $?
done
}
battery_status() {
if command_exists "pmset"; then
pmset -g batt | awk -F '; *' 'NR==2 { print $2 }'
elif command_exists "upower"; then
# sort order: attached, charging, discharging
for battery in $(upower -e | grep battery); do
upower -i $battery | grep state | awk '{print $2}'
done | sort | head -1
elif command_exists "acpi"; then
acpi -b | grep -oi 'discharging' | awk '{print tolower($0)}'
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 "apm"; then
local battery
battery=$(apm -a)
if [ $battery -eq 0 ]; then
echo "discharging"
elif [ $battery -eq 1 ]; then
echo "charging"
fi
elif command_exists "termux-battery-status" "jq"; then
termux-battery-status | jq -er '.status | ascii_downcase'
elif is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/status | tail -n1)
awk '{print tolower($0);}' "$battery"
fi
}
float2int() {
local float="$1"
if [[ $float =~ "," ]]; then
echo ${float%,*}
elif [[ $float =~ "." ]]; then
echo ${float%.*}
else
echo ${float//%}
fi
}