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

Add Battery Stat #117

Merged
merged 5 commits into from
Jul 3, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ I created `Catnap🌿` (originally known as `Catnip`) as a playful, simple syste
<li>shell</li>
<li>terminal</li>
<li>memory</li>
<li>battery</li>
<li>disk space</li>
<li>cpu info</li>
<li>packages</li>
Expand Down
3 changes: 2 additions & 1 deletion config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ kernel = {icon = " ", name = "kernel", color = "(MA)"}
desktop = {icon = " ", name = "desktop", color = "(CN)"}
# terminal = {icon = " ", name = "term", color = "(RD)"}
# shell = {icon = " ", name = "shell", color = "(MA)"}
packages = {icon = " ", name = "packages", color = "(GN)"} # WARNING: Resource Intensive
# packages = {icon = " ", name = "packages", color = "(RD)"} # WARNING: Resource Intensive
# gpu = {icon = "󱔐 ", name = "gpu", color = "(MA)"} # WARNING: Resource Intensive
# cpu = {icon = " ", name = "cpu", color = "(RD)"}
disk_0 = {icon = " ", name = "disk", color = "(GN)"}
memory = {icon = " ", name = "memory", color = "(YW)"}
# battery = {icon = " ", name = "battery", color = "(GN)"}
# weather = {icon = " ", name = "weather", color = "(BE)"} # Requires curl and an emoji font. | WARNING: Resource Intensive
sep_color = "SEPARATOR"
colors = {icon = " ", name = "colors", color = "!DT!", symbol = ""}
Expand Down
2 changes: 1 addition & 1 deletion src/catnaplib/global/definitions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const

# Stats
STATNAMES* = @["username", "hostname", "uptime", "distro",
"kernel", "desktop", "shell", "memory", "terminal",
"kernel", "desktop", "shell", "memory", "battery", "terminal",
"cpu", "gpu", "packages", "weather", "colors"]
STATKEYS* = @["icon", "name", "color"]

Expand Down
1 change: 1 addition & 0 deletions src/catnaplib/platform/fetch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ proc fetchSystemInfo*(config: Config, distroId: string = "nil"): FetchInfo =
result.list["terminal"] = proc(): string = return probe.getTerminal()
result.list["shell"] = proc(): string = return probe.getShell()
result.list["memory"] = proc(): string = return probe.getMemory()
result.list["battery"] = proc(): string = return probe.getBattery()
result.list["cpu"] = proc(): string = return probe.getCpu()
result.list["gpu"] = proc(): string = return probe.getGpu()
result.list["packages"] = proc(): string = return probe.getPackages()
Expand Down
24 changes: 22 additions & 2 deletions src/catnaplib/platform/probe.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import parsecfg
import posix_utils
import tables
import osproc
import re
from unicode import toLower
from "../global/definitions" import DistroId, PKGMANAGERS, PKGCOUNTCOMMANDS, toTmpPath
import "../terminal/logging"
Expand Down Expand Up @@ -121,6 +122,26 @@ proc getMemory*(mb: bool = true): string =

result = &"{memUsedInt} / {memTotalInt} {suffix} ({percentage}%)"

proc getBattery*(): string =
# Credits to https://gitlab.com/prashere/battinfo for regex implementation.
var batteryPath = ""

let
BATTERY_REGEX = re"^BAT\d+$"
powerPath = "/sys/class/power_supply/"

for dir in os.walk_dir(powerPath):
if re.match(os.last_path_part(dir.path), BATTERY_REGEX):
batteryPath = dir.path & "/"
else:
logError("No battery detected!")

let
batteryCapacity = readFile(batteryPath & "capacity").strip()
batteryStatus = readFile(batteryPath & "status").strip()

result = &"{batteryCapacity}% ({batteryStatus})"

proc getMounts*(): seq[string] =
proc getMountPoints(): cstring {.importc, varargs, header: "getDisk.h".}

Expand Down Expand Up @@ -242,5 +263,4 @@ proc getWeather*(): string =
logError("Failed to fetch weather!")

# Returns the weather and discards the annoying newline.
let weather = readFile(tmpFile)
result = $weather.replace("\n", "")
result = readFile(tmpFile).strip()