mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Add a disk module
This commit is contained in:
parent
4667afaa98
commit
1d39ef5c8e
@ -13,6 +13,7 @@
|
|||||||
#include "modules/cpu.hpp"
|
#include "modules/cpu.hpp"
|
||||||
#include "modules/idle_inhibitor.hpp"
|
#include "modules/idle_inhibitor.hpp"
|
||||||
#include "modules/memory.hpp"
|
#include "modules/memory.hpp"
|
||||||
|
#include "modules/disk.hpp"
|
||||||
#if defined(HAVE_DBUSMENU) && !defined(NO_FILESYSTEM)
|
#if defined(HAVE_DBUSMENU) && !defined(NO_FILESYSTEM)
|
||||||
#include "modules/sni/tray.hpp"
|
#include "modules/sni/tray.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
23
include/modules/disk.hpp
Normal file
23
include/modules/disk.hpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sys/statvfs.h>
|
||||||
|
#include "ALabel.hpp"
|
||||||
|
#include "util/sleeper_thread.hpp"
|
||||||
|
#include "util/format.hpp"
|
||||||
|
|
||||||
|
namespace waybar::modules {
|
||||||
|
|
||||||
|
class Disk : public ALabel {
|
||||||
|
public:
|
||||||
|
Disk(const std::string&, const Json::Value&);
|
||||||
|
~Disk() = default;
|
||||||
|
auto update() -> void;
|
||||||
|
|
||||||
|
private:
|
||||||
|
util::SleeperThread thread_;
|
||||||
|
std::string path_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace waybar::modules
|
9
include/util/format.hpp
Normal file
9
include/util/format.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace waybar::util {
|
||||||
|
|
||||||
|
std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false);
|
||||||
|
|
||||||
|
}
|
@ -88,8 +88,10 @@ src_files = files(
|
|||||||
'src/modules/clock.cpp',
|
'src/modules/clock.cpp',
|
||||||
'src/modules/custom.cpp',
|
'src/modules/custom.cpp',
|
||||||
'src/modules/cpu.cpp',
|
'src/modules/cpu.cpp',
|
||||||
|
'src/modules/disk.cpp',
|
||||||
'src/modules/idle_inhibitor.cpp',
|
'src/modules/idle_inhibitor.cpp',
|
||||||
'src/modules/temperature.cpp',
|
'src/modules/temperature.cpp',
|
||||||
|
'src/util/format.cpp',
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
'src/bar.cpp',
|
'src/bar.cpp',
|
||||||
'src/client.cpp'
|
'src/client.cpp'
|
||||||
|
@ -35,6 +35,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
|
|||||||
if (ref == "clock") {
|
if (ref == "clock") {
|
||||||
return new waybar::modules::Clock(id, config_[name]);
|
return new waybar::modules::Clock(id, config_[name]);
|
||||||
}
|
}
|
||||||
|
if (ref == "disk") {
|
||||||
|
return new waybar::modules::Disk(id, config_[name]);
|
||||||
|
}
|
||||||
#if defined(HAVE_DBUSMENU) && !defined(NO_FILESYSTEM)
|
#if defined(HAVE_DBUSMENU) && !defined(NO_FILESYSTEM)
|
||||||
if (ref == "tray") {
|
if (ref == "tray") {
|
||||||
return new waybar::modules::SNI::Tray(id, bar_, config_[name]);
|
return new waybar::modules::SNI::Tray(id, bar_, config_[name]);
|
||||||
|
58
src/modules/disk.cpp
Normal file
58
src/modules/disk.cpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include "modules/disk.hpp"
|
||||||
|
|
||||||
|
using namespace waybar::util;
|
||||||
|
|
||||||
|
waybar::modules::Disk::Disk(const std::string& id, const Json::Value& config)
|
||||||
|
: ALabel(config, "disk", id, "{}%", 30)
|
||||||
|
, path_("/")
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}; */ stats;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
label_.set_markup(fmt::format(format_,
|
||||||
|
stats.f_bavail * 100 / stats.f_blocks,
|
||||||
|
fmt::arg("free", pow_format(stats.f_bavail * stats.f_bsize, "B", true)),
|
||||||
|
fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks),
|
||||||
|
fmt::arg("used", pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true)),
|
||||||
|
fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
|
||||||
|
));
|
||||||
|
if (tooltipEnabled()) {
|
||||||
|
label_.set_tooltip_text(fmt::format("{} used", pow_format(stats.f_bavail * stats.f_bsize, "B", true)));
|
||||||
|
}
|
||||||
|
event_box_.show();
|
||||||
|
}
|
@ -2,9 +2,13 @@
|
|||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <sys/eventfd.h>
|
#include <sys/eventfd.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include "util/format.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
using namespace waybar::util;
|
||||||
|
|
||||||
constexpr const char *NETSTAT_FILE =
|
constexpr const char *NETSTAT_FILE =
|
||||||
"/proc/net/netstat"; // std::ifstream does not take std::string_view as param
|
"/proc/net/netstat"; // std::ifstream does not take std::string_view as param
|
||||||
constexpr std::string_view BANDWIDTH_CATEGORY = "IpExt";
|
constexpr std::string_view BANDWIDTH_CATEGORY = "IpExt";
|
||||||
@ -259,26 +263,6 @@ auto waybar::modules::Network::update() -> void {
|
|||||||
}
|
}
|
||||||
getState(signal_strength_);
|
getState(signal_strength_);
|
||||||
|
|
||||||
auto pow_format = [](unsigned long long value, const std::string &unit) {
|
|
||||||
if (value > 2000ull * 1000ull * 1000ull) { // > 2G
|
|
||||||
auto go = value / (1000 * 1000 * 1000);
|
|
||||||
return std::to_string(go) + "." +
|
|
||||||
std::to_string((value - go * 1000 * 1000 * 1000) / (100 * 1000 * 1000)) + "G" + unit;
|
|
||||||
|
|
||||||
} else if (value > 2000ull * 1000ull) { // > 2M
|
|
||||||
auto mo = value / (1000 * 1000);
|
|
||||||
return std::to_string(mo) + "." + std::to_string((value - mo * 1000 * 1000) / (100 * 1000)) +
|
|
||||||
"M" + unit;
|
|
||||||
|
|
||||||
} else if (value > 2000ull) { // > 2k
|
|
||||||
auto ko = value / 1000;
|
|
||||||
return std::to_string(ko) + "." + std::to_string((value - ko * 1000) / 100) + "k" + unit;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return std::to_string(value) + unit;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
auto text = fmt::format(
|
auto text = fmt::format(
|
||||||
format_,
|
format_,
|
||||||
fmt::arg("essid", essid_),
|
fmt::arg("essid", essid_),
|
||||||
|
26
src/util/format.cpp
Normal file
26
src/util/format.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace waybar::util {
|
||||||
|
|
||||||
|
std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false) {
|
||||||
|
auto base = binary ? 1024ull : 1000ull;
|
||||||
|
const char* units[] = { "", "k", "M", "G", "T", "P", nullptr};
|
||||||
|
auto fraction = (double) value;
|
||||||
|
|
||||||
|
int pow;
|
||||||
|
for (pow = 0; units[pow+1] != nullptr && fraction / base >= 2; ++pow) {
|
||||||
|
fraction /= base;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream ss;
|
||||||
|
if (pow > 0) {
|
||||||
|
auto quotient = (unsigned long long) fraction;
|
||||||
|
auto remainder = (unsigned long long) ((fraction - quotient) * 10);
|
||||||
|
ss << quotient << "." << remainder << units[pow] << (binary ? "i" : "") << unit;
|
||||||
|
} else {
|
||||||
|
ss << value << unit;
|
||||||
|
}
|
||||||
|
return ss.str();
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user