2019-09-23 20:25:54 +00:00
|
|
|
#include "modules/disk.hpp"
|
|
|
|
|
|
|
|
using namespace waybar::util;
|
|
|
|
|
|
|
|
waybar::modules::Disk::Disk(const std::string& id, const Json::Value& config)
|
2022-04-06 06:37:19 +00:00
|
|
|
: ALabel(config, "disk", id, "{}%", 30), path_("/") {
|
2019-09-23 20:25:54 +00:00
|
|
|
thread_ = [this] {
|
|
|
|
dp.emit();
|
|
|
|
thread_.sleep_for(interval_);
|
|
|
|
};
|
|
|
|
if (config["path"].isString()) {
|
|
|
|
path_ = config["path"].asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::Disk::update() -> void {
|
|
|
|
struct statvfs /* {
|
|
|
|
unsigned long f_bsize; // filesystem block size
|
|
|
|
unsigned long f_frsize; // fragment size
|
|
|
|
fsblkcnt_t f_blocks; // size of fs in f_frsize units
|
|
|
|
fsblkcnt_t f_bfree; // # free blocks
|
|
|
|
fsblkcnt_t f_bavail; // # free blocks for unprivileged users
|
|
|
|
fsfilcnt_t f_files; // # inodes
|
|
|
|
fsfilcnt_t f_ffree; // # free inodes
|
|
|
|
fsfilcnt_t f_favail; // # free inodes for unprivileged users
|
|
|
|
unsigned long f_fsid; // filesystem ID
|
|
|
|
unsigned long f_flag; // mount flags
|
|
|
|
unsigned long f_namemax; // maximum filename length
|
2022-04-06 06:37:19 +00:00
|
|
|
}; */
|
|
|
|
stats;
|
2019-09-23 20:25:54 +00:00
|
|
|
int err = statvfs(path_.c_str(), &stats);
|
|
|
|
|
|
|
|
/* Conky options
|
|
|
|
fs_bar - Bar that shows how much space is used
|
|
|
|
fs_free - Free space on a file system
|
|
|
|
fs_free_perc - Free percentage of space
|
|
|
|
fs_size - File system size
|
|
|
|
fs_used - File system used space
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
event_box_.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:40:36 +00:00
|
|
|
auto free = pow_format(stats.f_bavail * stats.f_frsize, "B", true);
|
2020-05-26 07:29:54 +00:00
|
|
|
auto used = pow_format((stats.f_blocks - stats.f_bfree) * stats.f_frsize, "B", true);
|
2019-09-30 21:40:36 +00:00
|
|
|
auto total = pow_format(stats.f_blocks * stats.f_frsize, "B", true);
|
2021-11-23 12:54:17 +00:00
|
|
|
auto percentage_used = (stats.f_blocks - stats.f_bfree) * 100 / stats.f_blocks;
|
2019-09-24 09:21:28 +00:00
|
|
|
|
2020-10-12 00:05:26 +00:00
|
|
|
auto format = format_;
|
|
|
|
auto state = getState(percentage_used);
|
|
|
|
if (!state.empty() && config_["format-" + state].isString()) {
|
|
|
|
format = config_["format-" + state].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format.empty()) {
|
|
|
|
event_box_.hide();
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
2022-04-06 06:37:19 +00:00
|
|
|
label_.set_markup(
|
|
|
|
fmt::format(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_)));
|
2020-10-12 00:05:26 +00:00
|
|
|
}
|
2019-09-24 09:21:28 +00:00
|
|
|
|
2019-09-23 20:25:54 +00:00
|
|
|
if (tooltipEnabled()) {
|
2019-09-24 09:21:28 +00:00
|
|
|
std::string tooltip_format = "{used} used out of {total} on {path} ({percentage_used}%)";
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
label_.set_tooltip_text(
|
|
|
|
fmt::format(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_)));
|
2019-09-23 20:25:54 +00:00
|
|
|
}
|
2020-04-12 16:30:21 +00:00
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
2019-09-23 20:25:54 +00:00
|
|
|
}
|