Skip to content

Commit

Permalink
Merge pull request #152 from ruiiiijiiiiang/feature/added-memory-supp…
Browse files Browse the repository at this point in the history
…ort-for-mac

added support for fetching memory on mac os
  • Loading branch information
iinsertNameHere authored Nov 12, 2024
2 parents 773c2a8 + a192306 commit 2623f97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ shell = {icon = " ", name = "shell", color = "(MA)"}
# gpu = {icon = "󱔐 ", name = "gpu", color = "(MA)"} # WARNING: Resource Intensive
# cpu = {icon = " ", name = "cpu", color = "(RD)"}
# battery = {icon = " ", name = "battery", color = "(GN)"}
# memory = {icon = " ", name = "memory", color = "(YW)"}
# The following stats do not work on MacOS and the BSDs.
# disk_0 = {icon = " ", name = "disk", color = "(GN)"}
# memory = {icon = " ", name = "memory", color = "(YW)"}
sep_color = "SEPARATOR"
colors = {icon = "", name = "colors", color = "!DT!", symbol = ""}

Expand Down
23 changes: 18 additions & 5 deletions src/catnaplib/platform/probe.nim
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ proc getDesktop*(): string =

proc getMemory*(mb: bool = true): string =
# Returns statistics about the memory
let
dividend: uint = if mb: 1000 else: 1024
suffix: string = if mb: "MB" else: "MiB"
if defined(linux) or defined(bsd):
let
fileSeq: seq[string] = "/proc/meminfo".readLines(3)

dividend: uint = if mb: 1000 else: 1024
suffix: string = if mb: "MB" else: "MiB"

memTotalString = fileSeq[0].split(" ")[^2]
memAvailableString = fileSeq[2].split(" ")[^2]

Expand All @@ -175,9 +175,22 @@ proc getMemory*(mb: bool = true): string =
percentage = ((int(memUsedInt) / int(memTotalInt)) * 100).round().int()

result = &"{memUsedInt} / {memTotalInt} {suffix} ({percentage}%)"
elif defined(macosx):
# The computation of free memory is very subjective on MacOS; multiple system utilites, ie vm_stat, top, memory_pressure, sysctl, all give different results
# Here memory_pressure is used since it shows the most resemblance to the graph in the Activity Monitor
let
memPressureRaw = execProcess("memory_pressure").split("\n")

memTotalString = memPressureRaw[0].split(" ")[3]
freePercenString = memPressureRaw[^2].split(" ")[^1]

memTotalInt = memTotalString.parseUInt div 1024 div dividend
freePercentInt = parseUInt(freePercenString[0..^2]) # This string comes with a % sign at the end
memUsedInt = memTotalInt * (100 - freePercentInt) div 100

result = &"{memUsedInt} / {memTotalInt} {suffix} ({100 - freePercentInt}%)"
else:
#TODO: add macos support
result = ""
result = "Unknown"

proc getBattery*(): string =
if defined(linux) or defined(bsd):
Expand Down

0 comments on commit 2623f97

Please sign in to comment.