added batmon usage for battery output

This commit is contained in:
Paul Wilde 2025-01-27 14:03:26 +00:00
parent 6de0aff1dc
commit 2cff444a6b
2 changed files with 28 additions and 6 deletions

BIN
nimsuggest.core Normal file

Binary file not shown.

View file

@ -1,4 +1,6 @@
import strutils import strutils
import osproc
import jsony
import ../common import ../common
import ../common/colours import ../common/colours
@ -17,6 +19,14 @@ const alert_bg = yellow
const med_fg = green const med_fg = green
#const med_bg = black #const med_bg = black
type
Battery = object
charge: float
status: string
charging: bool
proc postHook(b: var Battery) =
b.charging = b.status == "Charging"
proc batteryExists(): bool = proc batteryExists(): bool =
try: try:
@ -36,17 +46,17 @@ proc isCharging(): bool =
echo "Error getting charging status : " & getCurrentExceptionMsg() echo "Error getting charging status : " & getCurrentExceptionMsg()
return false return false
proc getCharge(): int = proc getCharge(): float =
var charge = 0 var charge = 0.0
try: try:
let chg = strip(readFile("/sys/class/power_supply/" & battery & "/capacity")) let chg = strip(readFile("/sys/class/power_supply/" & battery & "/capacity"))
if chg != "": if chg != "":
charge = parseInt(chg) charge = parseFloat(chg)
except: except:
echo "Error getting battery level : " & getCurrentExceptionMsg() echo "Error getting battery level : " & getCurrentExceptionMsg()
return charge return charge
proc getDesign(charge: int, state: bool): string = proc getDesign(charge: float, state: bool): string =
var icon = "" var icon = ""
var icon_colour = ok_fg var icon_colour = ok_fg
var col = default_fg var col = default_fg
@ -87,7 +97,7 @@ proc getDesign(charge: int, state: bool): string =
return main_text return main_text
proc getOutput(charge: int, state: bool): Info = proc getOutput(charge: float, state: bool): Info =
let main_text = get_design(charge, state) let main_text = get_design(charge, state)
var data = newInfo("Batturry") var data = newInfo("Batturry")
# TODO check if html text works with rofi # TODO check if html text works with rofi
@ -101,8 +111,20 @@ proc getBatteryInfo() =
let data = getoutput(charge, state) let data = getoutput(charge, state)
outputData(data) outputData(data)
proc getBatteryInfoBSD() =
try:
let batmon = osproc.execCmdEx("batmon -o")
if batmon.exitCode == 0:
let bat = batmon.output.fromJson(Battery)
let data = getOutput(bat.charge, bat.charging)
outputData(data)
except:
echo getCurrentExceptionMsg()
proc go*() = proc go*() =
if batteryExists(): if hostOS == "freebsd":
getBatteryInfoBSD();
elif batteryExists():
getBatteryInfo() getBatteryInfo()