Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: lib/battery: rename plugin/battery #2085

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ completion/available/vuejs.completion.bash
completion/available/wpscan.completion.bash

# libraries
lib/battery.bash
lib/colors.bash
lib/helpers.bash
lib/log.bash
Expand All @@ -94,7 +95,6 @@ plugins/available/alias-completion.plugin.bash
plugins/available/autojump.plugin.bash
plugins/available/base.plugin.bash
plugins/available/basher.plugin.bash
plugins/available/battery.plugin.bash
plugins/available/blesh.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash
plugins/available/colors.plugin.bash
Expand Down
125 changes: 125 additions & 0 deletions lib/battery.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# shellcheck shell=bash
about-plugin 'display info about your battery charge level'

function ac_adapter_connected() {
if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'charging\|fully-charged'
elif _command_exists acpi; then
acpi -a | grep -q "on-line"
elif _command_exists pmset; then
pmset -g batt | grep -q 'AC Power'
elif _command_exists ioreg; then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
elif _command_exists WMIC; then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
fi
}

function ac_adapter_disconnected() {
if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'discharging'
elif _command_exists acpi; then
acpi -a | grep -q "off-line"
elif _command_exists pmset; then
pmset -g batt | grep -q 'Battery Power'
elif _command_exists ioreg; then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
elif _command_exists WMIC; then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
fi
}

function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)'
group 'battery'

local command_output="no"

if _command_exists upower; then
command_output=$(upower --show-info "$(upower --enumerate | grep -i BAT)" | grep percentage | grep -o "[0-9]\+" | head -1)
elif _command_exists acpi; then
command_output=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}')
elif _command_exists pmset; then
command_output=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1)
elif _command_exists ioreg; then
command_output=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}' | grep -o "[0-9]\+" | head -1)
elif _command_exists WMIC; then
command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else
command_output="no"
fi

if [[ "${command_output}" != "no" ]]; then
printf "%02d" "${command_output:--1}"
else
echo "${command_output}"
fi
}

function battery_charge() {
about 'graphical display of your battery charge'
group 'battery'

# Full char
local f_c='▸'
# Depleted char
local d_c='▹'
local depleted_color="${normal?}"
local full_color="${green?}"
local half_color="${yellow?}"
local danger_color="${red?}"
#local battery_output="${depleted_color}${d_c}${d_c}${d_c}${d_c}${d_c}"
local battery_percentage
battery_percentage=$(battery_percentage)

case $battery_percentage in
no)
echo ""
;;
9*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${f_c}${normal?}"
;;
8*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${half_color}${f_c}${normal?}"
;;
7*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${depleted_color}${d_c}${normal?}"
;;
6*)
echo "${full_color}${f_c}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${normal?}"
;;
5*)
echo "${full_color}${f_c}${f_c}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;;
4*)
echo "${full_color}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;;
3*)
echo "${full_color}${f_c}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;;
2*)
echo "${full_color}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;;
1*)
echo "${full_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
05)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
04)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
03)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
02)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
0*)
echo "${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
*)
echo "${danger_color}UNPLG${normal?}"
;;
esac
}
125 changes: 2 additions & 123 deletions plugins/available/battery.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,125 +1,4 @@
# shellcheck shell=bash
about-plugin 'display info about your battery charge level'
# stub for renamed file

function ac_adapter_connected() {
if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'charging\|fully-charged'
elif _command_exists acpi; then
acpi -a | grep -q "on-line"
elif _command_exists pmset; then
pmset -g batt | grep -q 'AC Power'
elif _command_exists ioreg; then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
elif _command_exists WMIC; then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
fi
}

function ac_adapter_disconnected() {
if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'discharging'
elif _command_exists acpi; then
acpi -a | grep -q "off-line"
elif _command_exists pmset; then
pmset -g batt | grep -q 'Battery Power'
elif _command_exists ioreg; then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
elif _command_exists WMIC; then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
fi
}

function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)'
group 'battery'

local command_output="no"

if _command_exists upower; then
command_output=$(upower --show-info "$(upower --enumerate | grep -i BAT)" | grep percentage | grep -o "[0-9]\+" | head -1)
elif _command_exists acpi; then
command_output=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}')
elif _command_exists pmset; then
command_output=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1)
elif _command_exists ioreg; then
command_output=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}' | grep -o "[0-9]\+" | head -1)
elif _command_exists WMIC; then
command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else
command_output="no"
fi

if [[ "${command_output}" != "no" ]]; then
printf "%02d" "${command_output:--1}"
else
echo "${command_output}"
fi
}

function battery_charge() {
about 'graphical display of your battery charge'
group 'battery'

# Full char
local f_c='▸'
# Depleted char
local d_c='▹'
local depleted_color="${normal?}"
local full_color="${green?}"
local half_color="${yellow?}"
local danger_color="${red?}"
#local battery_output="${depleted_color}${d_c}${d_c}${d_c}${d_c}${d_c}"
local battery_percentage
battery_percentage=$(battery_percentage)

case $battery_percentage in
no)
echo ""
;;
9*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${f_c}${normal?}"
;;
8*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${half_color}${f_c}${normal?}"
;;
7*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${depleted_color}${d_c}${normal?}"
;;
6*)
echo "${full_color}${f_c}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${normal?}"
;;
5*)
echo "${full_color}${f_c}${f_c}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;;
4*)
echo "${full_color}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;;
3*)
echo "${full_color}${f_c}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;;
2*)
echo "${full_color}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;;
1*)
echo "${full_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
05)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
04)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
03)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
02)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
0*)
echo "${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
*)
echo "${danger_color}UNPLG${normal?}"
;;
esac
}
_disable-plugin battery
Loading