218 lines
6.9 KiB
Bash
Executable File
218 lines
6.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# This file is part of I38.
|
|
|
|
# I38 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
|
|
# either version 3 of the License, or (at your option) any later version.
|
|
|
|
# I38 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
# PURPOSE. See the GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License along with I38. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
# Parse command line arguments
|
|
detailedMode=0
|
|
if [[ "$1" == "-d" ]] || [[ "$1" == "--detailed" ]]; then
|
|
detailedMode=1
|
|
fi
|
|
|
|
get_battery_details() {
|
|
local batteryPath="$1"
|
|
local batteryName="${batteryPath##*/}"
|
|
local modelName=""
|
|
local manufacturer=""
|
|
local capacity=""
|
|
local status=""
|
|
local health=""
|
|
local cycleCount=""
|
|
local technology=""
|
|
local voltage=""
|
|
local current=""
|
|
local power=""
|
|
local output=""
|
|
|
|
# Get model name if available
|
|
if [[ -f "${batteryPath}/model_name" ]]; then
|
|
modelName="$(cat "${batteryPath}/model_name" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get manufacturer if available
|
|
if [[ -f "${batteryPath}/manufacturer" ]]; then
|
|
manufacturer="$(cat "${batteryPath}/manufacturer" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get capacity (charge percentage)
|
|
if [[ -f "${batteryPath}/capacity" ]]; then
|
|
capacity="$(cat "${batteryPath}/capacity" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get status (Charging, Discharging, Full, etc.)
|
|
if [[ -f "${batteryPath}/status" ]]; then
|
|
status="$(cat "${batteryPath}/status" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get health/capacity level if available
|
|
if [[ -f "${batteryPath}/capacity_level" ]]; then
|
|
health="$(cat "${batteryPath}/capacity_level" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get cycle count if available
|
|
if [[ -f "${batteryPath}/cycle_count" ]]; then
|
|
cycleCount="$(cat "${batteryPath}/cycle_count" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get technology (Li-ion, Li-poly, etc.)
|
|
if [[ -f "${batteryPath}/technology" ]]; then
|
|
technology="$(cat "${batteryPath}/technology" 2>/dev/null | tr -d '\n')"
|
|
fi
|
|
|
|
# Get voltage (in microvolts, convert to volts)
|
|
if [[ -f "${batteryPath}/voltage_now" ]]; then
|
|
local voltageUv
|
|
voltageUv="$(cat "${batteryPath}/voltage_now" 2>/dev/null | tr -d '\n')"
|
|
if [[ -n "$voltageUv" ]] && [[ "$voltageUv" =~ ^[0-9]+$ ]]; then
|
|
voltage="$(awk "BEGIN {printf \"%.2f\", $voltageUv/1000000}")"
|
|
fi
|
|
fi
|
|
|
|
# Get current (in microamps, convert to amps)
|
|
if [[ -f "${batteryPath}/current_now" ]]; then
|
|
local currentUa
|
|
currentUa="$(cat "${batteryPath}/current_now" 2>/dev/null | tr -d '\n')"
|
|
if [[ -n "$currentUa" ]] && [[ "$currentUa" =~ ^[0-9]+$ ]]; then
|
|
current="$(awk "BEGIN {printf \"%.2f\", $currentUa/1000000}")"
|
|
fi
|
|
fi
|
|
|
|
# Get power (in microwatts, convert to watts)
|
|
if [[ -f "${batteryPath}/power_now" ]]; then
|
|
local powerUw
|
|
powerUw="$(cat "${batteryPath}/power_now" 2>/dev/null | tr -d '\n')"
|
|
if [[ -n "$powerUw" ]] && [[ "$powerUw" =~ ^[0-9]+$ ]]; then
|
|
power="$(awk "BEGIN {printf \"%.2f\", $powerUw/1000000}")"
|
|
fi
|
|
fi
|
|
|
|
# Build readable device name
|
|
local deviceName="$batteryName"
|
|
if [[ -n "$manufacturer" ]] && [[ -n "$modelName" ]]; then
|
|
deviceName="$manufacturer $modelName"
|
|
elif [[ -n "$modelName" ]]; then
|
|
deviceName="$modelName"
|
|
elif [[ -n "$manufacturer" ]]; then
|
|
deviceName="$manufacturer Battery"
|
|
else
|
|
# Try to make the battery name more readable
|
|
deviceName="${batteryName//BAT/Battery }"
|
|
deviceName="${deviceName//hid-/}"
|
|
deviceName="${deviceName//_/ }"
|
|
fi
|
|
|
|
# Build the output string
|
|
output="$deviceName"
|
|
|
|
if [[ -n "$capacity" ]]; then
|
|
output="$output, ${capacity}%"
|
|
fi
|
|
|
|
if [[ -n "$status" ]]; then
|
|
output="$output, ${status,,}"
|
|
fi
|
|
|
|
if [[ -n "$health" ]]; then
|
|
output="$output, health: ${health,,}"
|
|
fi
|
|
|
|
if [[ -n "$technology" ]]; then
|
|
output="$output, type: $technology"
|
|
fi
|
|
|
|
if [[ -n "$voltage" ]]; then
|
|
output="$output, voltage: ${voltage}V"
|
|
fi
|
|
|
|
if [[ -n "$current" ]]; then
|
|
output="$output, current: ${current}A"
|
|
fi
|
|
|
|
if [[ -n "$power" ]]; then
|
|
output="$output, power: ${power}W"
|
|
fi
|
|
|
|
if [[ -n "$cycleCount" ]]; then
|
|
output="$output, cycles: $cycleCount"
|
|
fi
|
|
|
|
echo "$output"
|
|
}
|
|
|
|
# Simple battery status (default mode)
|
|
get_battery_simple() {
|
|
local batteryPath="$1"
|
|
local bat="${batteryPath##*/}"
|
|
bat="${bat//BAT/Battery }"
|
|
bat="${bat}: $( { cat "${batteryPath}/status";echo -n ", "; cat "${batteryPath}/capacity"; } | tr -d \\n) percent."
|
|
echo "$bat"
|
|
}
|
|
|
|
# Main script
|
|
if [[ $detailedMode -eq 1 ]]; then
|
|
# Detailed mode
|
|
if command -v acpi &> /dev/null; then
|
|
# Try acpi first for more detailed info
|
|
batteryInfo=""
|
|
while IFS= read -r line; do
|
|
if [[ -n "$batteryInfo" ]]; then
|
|
batteryInfo="${batteryInfo}. "
|
|
fi
|
|
batteryInfo="${batteryInfo}${line}"
|
|
done < <(acpi -V 2>/dev/null | grep -i battery)
|
|
|
|
if [[ -n "$batteryInfo" ]]; then
|
|
spd-say -P important -Cw "$batteryInfo"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# Fallback to /sys/class/power_supply for detailed info
|
|
batteryCount=0
|
|
batteryOutput=""
|
|
|
|
while IFS= read -r batteryPath; do
|
|
if [[ -e "${batteryPath}/capacity" ]] || [[ -e "${batteryPath}/status" ]]; then
|
|
batteryDetails="$(get_battery_details "$batteryPath")"
|
|
|
|
if [[ -n "$batteryDetails" ]]; then
|
|
((batteryCount++))
|
|
if [[ -n "$batteryOutput" ]]; then
|
|
batteryOutput="${batteryOutput}. "
|
|
fi
|
|
batteryOutput="${batteryOutput}${batteryDetails}"
|
|
fi
|
|
fi
|
|
done < <(find /sys/class/power_supply -type l 2>/dev/null)
|
|
|
|
if [[ $batteryCount -eq 0 ]]; then
|
|
spd-say -P important -Cw "No battery information available"
|
|
else
|
|
spd-say -P important -Cw "$batteryOutput"
|
|
fi
|
|
else
|
|
# Simple mode (default)
|
|
if command -v acpi &> /dev/null; then
|
|
bat="$(acpi -b)"
|
|
spd-say -P important -Cw "$bat"
|
|
else
|
|
find /sys/class/power_supply -type l -exec bash -c '
|
|
for i ; do
|
|
if [[ -e "$i/capacity" ]]; then
|
|
bat="${i##*/}"
|
|
bat="${bat//BAT/Battery }"
|
|
bat="${bat}: $( { cat "${i}/status";echo -n ", "; cat "${i}/capacity"; } | tr -d \\n) percent."
|
|
spd-say -P important -Cw "$bat"
|
|
fi
|
|
done
|
|
' _ {} \;
|
|
fi
|
|
fi
|