diff --git a/man/waybar-memory.5.scd b/man/waybar-memory.5.scd index 0639c07c..d960ffd2 100644 --- a/man/waybar-memory.5.scd +++ b/man/waybar-memory.5.scd @@ -84,12 +84,20 @@ Addressed by *memory* *{percentage}*: Percentage of memory in use. +*{swapPercentage}*: Percentage of swap in use. + *{total}*: Amount of total memory available in GiB. +*{swapTotal}*: Amount of total swap available in GiB. + *{used}*: Amount of used memory in GiB. +*{swapUsed}*: Amount of used swap in GiB. + *{avail}*: Amount of available memory in GiB. +*{swapAvail}*: Amount of available swap in GiB. + # EXAMPLES ``` diff --git a/src/modules/memory/common.cpp b/src/modules/memory/common.cpp index 75e05302..a0bf13fa 100644 --- a/src/modules/memory/common.cpp +++ b/src/modules/memory/common.cpp @@ -12,7 +12,15 @@ auto waybar::modules::Memory::update() -> void { parseMeminfo(); unsigned long memtotal = meminfo_["MemTotal"]; + unsigned long swaptotal = 0; + if (meminfo_.count("SwapTotal")) { + swaptotal = meminfo_["SwapTotal"]; + } unsigned long memfree; + unsigned long swapfree = 0; + if (meminfo_.count("SwapFree")) { + swapfree = meminfo_["SwapFree"]; + } if (meminfo_.count("MemAvailable")) { // New kernels (3.4+) have an accurate available memory field. memfree = meminfo_["MemAvailable"] + meminfo_["zfs_size"]; @@ -24,9 +32,16 @@ auto waybar::modules::Memory::update() -> void { if (memtotal > 0 && memfree >= 0) { auto total_ram_gigabytes = memtotal / std::pow(1024, 2); + auto total_swap_gigabytes = swaptotal / std::pow(1024, 2); int used_ram_percentage = 100 * (memtotal - memfree) / memtotal; + int used_swap_percentage = 0; + if (swaptotal && swapfree) { + used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal; + } auto used_ram_gigabytes = (memtotal - memfree) / std::pow(1024, 2); + auto used_swap_gigabytes = (swaptotal - swapfree) / std::pow(1024, 2); auto available_ram_gigabytes = memfree / std::pow(1024, 2); + auto available_swap_gigabytes = swapfree / std::pow(1024, 2); auto format = format_; auto state = getState(used_ram_percentage); @@ -43,9 +58,13 @@ auto waybar::modules::Memory::update() -> void { used_ram_percentage, fmt::arg("icon", getIcon(used_ram_percentage, icons)), fmt::arg("total", total_ram_gigabytes), + fmt::arg("swapTotal", total_swap_gigabytes), fmt::arg("percentage", used_ram_percentage), + fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram_gigabytes), - fmt::arg("avail", available_ram_gigabytes))); + fmt::arg("swapUsed", used_swap_gigabytes), + fmt::arg("avail", available_ram_gigabytes), + fmt::arg("swapAvail", available_swap_gigabytes))); } if (tooltipEnabled()) { @@ -54,9 +73,13 @@ auto waybar::modules::Memory::update() -> void { label_.set_tooltip_text(fmt::format(tooltip_format, used_ram_percentage, fmt::arg("total", total_ram_gigabytes), + fmt::arg("swapTotal", total_swap_gigabytes), fmt::arg("percentage", used_ram_percentage), + fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram_gigabytes), - fmt::arg("avail", available_ram_gigabytes))); + fmt::arg("swapUsed", used_swap_gigabytes), + fmt::arg("avail", available_ram_gigabytes), + fmt::arg("swapAvail", available_swap_gigabytes))); } else { label_.set_tooltip_text(fmt::format("{:.{}f}GiB used", used_ram_gigabytes, 1)); }