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

Adding options to allow disk outputs to always be in a specific unit. #2558

Merged
merged 3 commits into from
Oct 8, 2023
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
3 changes: 3 additions & 0 deletions include/modules/disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Disk : public ALabel {
private:
util::SleeperThread thread_;
std::string path_;
std::string unit_;

float calc_specific_divisor(const std::string divisor);
};

} // namespace waybar::modules
25 changes: 22 additions & 3 deletions man/waybar-disk.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,30 @@ Addressed by *disk*
default: "{used} out of {total} used ({percentage_used}%)" ++
The format of the information displayed in the tooltip.

*unit*: ++
typeof: string ++
Use with specific_free, specific_used, and speciifc_total to force calculation to always be in a certain unit. Accepts kB, kiB, MB, Mib, GB, GiB, TB, TiB.

# FORMAT REPLACEMENTS

*{percentage_used}*: Percentage of disk in use.

*{percentage_free}*: Percentage of free disk space

*{total}*: Total amount of space on the disk, partition or mountpoint.
*{total}*: Total amount of space on the disk, partition or mountpoint. Automatically selects unit based on size remaining.

*{used}*: Amount of used disk space.
*{used}*: Amount of used disk space. Automatically selects unit based on size remaining.

*{free}*: Amount of available disk space for normal users.
*{free}*: Amount of available disk space for normal users. Automatically selects unit based on size remaining.

*{path}*: The path specified in the configuration.

*{specific_total}*: Total amount of space on the disk, partition or mountpoint in a specific unit. Deaults to bytes.

*{specific_used}*: Amount of used disk space in a specific unit. Deaults to bytes.

*{specific_free}*: Amount of available disk space for normal users in a specific unit. Deaults to bytes.

# EXAMPLES

```
Expand All @@ -108,6 +118,15 @@ Addressed by *disk*
}
```

```
"disk": {
"interval": 30,
"format": "{specific_free:0.2f} GB out of {specific_total:0.2f} GB available. Alternatively {free} out of {total} available",
"unit": "GB"
// 1434.25 GB out of 2000.00 GB available. Alternatively 1.4TiB out of 1.9TiB available.
}
```

# STYLE

- *#disk*
38 changes: 36 additions & 2 deletions src/modules/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ waybar::modules::Disk::Disk(const std::string& id, const Json::Value& config)
if (config["path"].isString()) {
path_ = config["path"].asString();
}
if (config["unit"].isString()) {
unit_ = config["unit"].asString();
}
}

auto waybar::modules::Disk::update() -> void {
Expand Down Expand Up @@ -43,6 +46,13 @@ auto waybar::modules::Disk::update() -> void {
return;
}

float specific_free, specific_used, specific_total, divisor;

divisor = calc_specific_divisor(unit_);
specific_free = (stats.f_bavail * stats.f_frsize)/divisor;
specific_used = ((stats.f_blocks - stats.f_bfree) * stats.f_frsize)/divisor;
specific_total = (stats.f_blocks * stats.f_frsize)/divisor;

auto free = pow_format(stats.f_bavail * stats.f_frsize, "B", true);
auto used = pow_format((stats.f_blocks - stats.f_bfree) * stats.f_frsize, "B", true);
auto total = pow_format(stats.f_blocks * stats.f_frsize, "B", true);
Expand All @@ -62,7 +72,8 @@ auto waybar::modules::Disk::update() -> void {
fmt::runtime(format), stats.f_bavail * 100 / stats.f_blocks, fmt::arg("free", free),
fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks), fmt::arg("used", used),
fmt::arg("percentage_used", percentage_used), fmt::arg("total", total),
fmt::arg("path", path_)));
fmt::arg("path", path_), fmt::arg("specific_free", specific_free),
fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total)));
}

if (tooltipEnabled()) {
Expand All @@ -74,8 +85,31 @@ auto waybar::modules::Disk::update() -> void {
fmt::runtime(tooltip_format), stats.f_bavail * 100 / stats.f_blocks, fmt::arg("free", free),
fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks), fmt::arg("used", used),
fmt::arg("percentage_used", percentage_used), fmt::arg("total", total),
fmt::arg("path", path_)));
fmt::arg("path", path_), fmt::arg("specific_free", specific_free),
fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total)));
}
// Call parent update
ALabel::update();
}

float waybar::modules::Disk::calc_specific_divisor(std::string divisor) {
if (divisor == "kB") {
return 1000.0;
} else if (divisor == "kiB") {
return 1024.0;
} else if (divisor == "MB") {
return 1000.0 * 1000.0;
} else if (divisor == "MiB") {
return 1024.0 * 1024.0;
} else if (divisor == "GB") {
return 1000.0 * 1000.0 * 1000.0;
} else if (divisor == "GiB") {
return 1024.0 * 1024.0 * 1024.0;
} else if (divisor == "TB") {
return 1000.0 * 1000.0 * 1000.0 * 1000.0;
} else if (divisor == "TiB") {
return 1024.0 * 1024.0 * 1024.0 * 1024.0;
} else { //default to Bytes if it is anything that we don't recongnise
return 1.0;
}
}