1 #include "modules/disk.hpp"
2 
3 using namespace waybar::util;
4 
Disk(const std::string & id,const Json::Value & config)5 waybar::modules::Disk::Disk(const std::string& id, const Json::Value& config)
6     : ALabel(config, "disk", id, "{}%", 30)
7     , path_("/")
8 {
9   thread_ = [this] {
10     dp.emit();
11     thread_.sleep_for(interval_);
12   };
13   if (config["path"].isString()) {
14     path_ = config["path"].asString();
15   }
16 }
17 
update()18 auto waybar::modules::Disk::update() -> void {
19   struct statvfs /* {
20       unsigned long  f_bsize;    // filesystem block size
21       unsigned long  f_frsize;   // fragment size
22       fsblkcnt_t     f_blocks;   // size of fs in f_frsize units
23       fsblkcnt_t     f_bfree;    // # free blocks
24       fsblkcnt_t     f_bavail;   // # free blocks for unprivileged users
25       fsfilcnt_t     f_files;    // # inodes
26       fsfilcnt_t     f_ffree;    // # free inodes
27       fsfilcnt_t     f_favail;   // # free inodes for unprivileged users
28       unsigned long  f_fsid;     // filesystem ID
29       unsigned long  f_flag;     // mount flags
30       unsigned long  f_namemax;  // maximum filename length
31   }; */ stats;
32   int err = statvfs(path_.c_str(), &stats);
33 
34   /* Conky options
35     fs_bar - Bar that shows how much space is used
36     fs_free - Free space on a file system
37     fs_free_perc - Free percentage of space
38     fs_size - File system size
39     fs_used - File system used space
40   */
41 
42   if (err != 0) {
43     event_box_.hide();
44     return;
45   }
46 
47   auto free = pow_format(stats.f_bavail * stats.f_frsize, "B", true);
48   auto used = pow_format((stats.f_blocks - stats.f_bfree) * stats.f_frsize, "B", true);
49   auto total = pow_format(stats.f_blocks * stats.f_frsize, "B", true);
50   auto percentage_used = (stats.f_blocks - stats.f_bfree) * 100 / stats.f_blocks;
51 
52   auto format = format_;
53   auto state = getState(percentage_used);
54   if (!state.empty() && config_["format-" + state].isString()) {
55     format = config_["format-" + state].asString();
56   }
57 
58   if (format.empty()) {
59     event_box_.hide();
60   } else {
61     event_box_.show();
62     label_.set_markup(fmt::format(format
63         , stats.f_bavail * 100 / stats.f_blocks
64         , fmt::arg("free", free)
65         , fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
66         , fmt::arg("used", used)
67         , fmt::arg("percentage_used", percentage_used)
68         , fmt::arg("total", total)
69         , fmt::arg("path", path_)
70         ));
71   }
72 
73   if (tooltipEnabled()) {
74     std::string tooltip_format = "{used} used out of {total} on {path} ({percentage_used}%)";
75     if (config_["tooltip-format"].isString()) {
76       tooltip_format = config_["tooltip-format"].asString();
77     }
78     label_.set_tooltip_text(fmt::format(tooltip_format
79       , stats.f_bavail * 100 / stats.f_blocks
80       , fmt::arg("free", free)
81       , fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
82       , fmt::arg("used", used)
83       , fmt::arg("percentage_used", percentage_used)
84       , fmt::arg("total", total)
85       , fmt::arg("path", path_)
86       ));
87   }
88   // Call parent update
89   ALabel::update();
90 }
91