fix: resource size units & mismatch
This commit is contained in:
parent
66eb394490
commit
d7378ba5dd
8 changed files with 38 additions and 73 deletions
|
|
@ -84,9 +84,8 @@ StyledRect {
|
|||
StyledText {
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
text: {
|
||||
const usedFmt = Memory.formatKib(Memory.used);
|
||||
const totalFmt = Memory.formatKib(Memory.total);
|
||||
return `${usedFmt.value.toFixed(1)} / ${Math.floor(totalFmt.value)} ${totalFmt.unit}`;
|
||||
const fmt = UsageFmt.formatKib(Memory.used, Memory.total);
|
||||
return `${fmt.value.toFixed(1)} / ${Math.floor(fmt.total)} ${fmt.unit}`;
|
||||
}
|
||||
font: Tokens.font.body.medium
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,9 +92,8 @@ StyledRect {
|
|||
if (!Storage.primaryDisk)
|
||||
return qsTr("No disks detected");
|
||||
|
||||
const usedFmt = Storage.formatKib(Storage.primaryDisk.used);
|
||||
const totalFmt = Storage.formatKib(Storage.primaryDisk.total);
|
||||
return `${usedFmt.value.toFixed(1)} / ${Math.floor(totalFmt.value)} ${totalFmt.unit}`;
|
||||
const fmt = UsageFmt.formatKib(Storage.primaryDisk.used, Storage.primaryDisk.total);
|
||||
return `${fmt.value.toFixed(1)} / ${Math.floor(fmt.total)} ${fmt.unit}`;
|
||||
}
|
||||
font: Tokens.font.body.large
|
||||
color: root.accent
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
#include "memory.hpp"
|
||||
|
||||
#include "usagefmt.hpp"
|
||||
|
||||
#include <qfile.h>
|
||||
#include <qregularexpression.h>
|
||||
|
||||
|
|
@ -22,10 +20,6 @@ qreal Memory::percentage() const {
|
|||
return m_total > 0.0 ? m_used / m_total : 0.0;
|
||||
}
|
||||
|
||||
QVariantMap Memory::formatKib(qreal kib) const {
|
||||
return usagefmt::formatKib(kib);
|
||||
}
|
||||
|
||||
void Memory::tick() {
|
||||
QFile f(QStringLiteral("/proc/meminfo"));
|
||||
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
|
|
|
|||
|
|
@ -23,8 +23,6 @@ public:
|
|||
[[nodiscard]] qreal total() const;
|
||||
[[nodiscard]] qreal percentage() const;
|
||||
|
||||
Q_INVOKABLE [[nodiscard]] QVariantMap formatKib(qreal kib) const;
|
||||
|
||||
signals:
|
||||
void changed();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
#include "storage.hpp"
|
||||
|
||||
#include "usagefmt.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <qdir.h>
|
||||
|
|
@ -156,10 +154,6 @@ DiskInfo* Storage::primaryDisk() const {
|
|||
return m_disks.isEmpty() ? nullptr : m_disks.first();
|
||||
}
|
||||
|
||||
QVariantMap Storage::formatKib(qreal kib) const {
|
||||
return usagefmt::formatKib(kib);
|
||||
}
|
||||
|
||||
bool Storage::isPseudoFs(QByteArrayView fsType) {
|
||||
static constexpr const char* kPseudo[] = {
|
||||
"tmpfs",
|
||||
|
|
|
|||
|
|
@ -30,8 +30,6 @@ public:
|
|||
void setManualPrimaryDisk(DiskInfo* disk);
|
||||
[[nodiscard]] DiskInfo* primaryDisk() const;
|
||||
|
||||
Q_INVOKABLE [[nodiscard]] QVariantMap formatKib(qreal kib) const;
|
||||
|
||||
signals:
|
||||
void disksChanged();
|
||||
void percentageChanged();
|
||||
|
|
|
|||
|
|
@ -1,19 +1,10 @@
|
|||
#include "usagefmt.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace caelestia::services::usagefmt {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr qreal kKib = 1024.0;
|
||||
constexpr qreal kMib = kKib * 1024.0;
|
||||
constexpr qreal kGib = kMib * 1024.0;
|
||||
constexpr qreal kTib = kGib * 1024.0;
|
||||
|
||||
QVariantMap make(qreal value, const char* unit) {
|
||||
return QVariantMap{ { QStringLiteral("value"), value }, { QStringLiteral("unit"), QString::fromLatin1(unit) } };
|
||||
}
|
||||
|
||||
bool finitePositive(qreal v) {
|
||||
return std::isfinite(v) && v >= 0.0;
|
||||
|
|
@ -21,49 +12,22 @@ bool finitePositive(qreal v) {
|
|||
|
||||
} // namespace
|
||||
|
||||
QVariantMap formatKib(qreal kib) {
|
||||
if (kib >= kTib) {
|
||||
return make(kib / kTib, "TiB");
|
||||
}
|
||||
if (kib >= kGib) {
|
||||
return make(kib / kGib, "GiB");
|
||||
}
|
||||
if (kib >= kMib) {
|
||||
return make(kib / kMib, "MiB");
|
||||
}
|
||||
return make(kib, "KiB");
|
||||
}
|
||||
namespace caelestia::services::usagefmt {
|
||||
|
||||
QVariantMap formatBytes(qreal bytes) {
|
||||
if (!finitePositive(bytes)) {
|
||||
return make(0.0, "B/s");
|
||||
FormatResult UsageFmt::formatKib(qreal kib, qreal total) const {
|
||||
if (!finitePositive(kib) || !finitePositive(total)) {
|
||||
return { 0.0, 0.0, "KiB" };
|
||||
}
|
||||
if (bytes < kKib) {
|
||||
return make(bytes, "B/s");
|
||||
if (total >= kGib) {
|
||||
return { kib / kGib, total / kGib, "TiB" };
|
||||
}
|
||||
if (bytes < kMib) {
|
||||
return make(bytes / kKib, "KB/s");
|
||||
if (total >= kMib) {
|
||||
return { kib / kMib, total / kMib, "GiB" };
|
||||
}
|
||||
if (bytes < kGib) {
|
||||
return make(bytes / kMib, "MB/s");
|
||||
if (total >= kKib) {
|
||||
return { kib / kKib, total / kKib, "MiB" };
|
||||
}
|
||||
return make(bytes / kGib, "GB/s");
|
||||
}
|
||||
|
||||
QVariantMap formatBytesTotal(qreal bytes) {
|
||||
if (!finitePositive(bytes)) {
|
||||
return make(0.0, "B");
|
||||
}
|
||||
if (bytes < kKib) {
|
||||
return make(bytes, "B");
|
||||
}
|
||||
if (bytes < kMib) {
|
||||
return make(bytes / kKib, "KB");
|
||||
}
|
||||
if (bytes < kGib) {
|
||||
return make(bytes / kMib, "MB");
|
||||
}
|
||||
return make(bytes / kGib, "GB");
|
||||
return { kib, total, "KiB" };
|
||||
}
|
||||
|
||||
} // namespace caelestia::services::usagefmt
|
||||
|
|
|
|||
|
|
@ -1,11 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <qvariant.h>
|
||||
#include <qqmlintegration.h>
|
||||
|
||||
namespace caelestia::services::usagefmt {
|
||||
|
||||
[[nodiscard]] QVariantMap formatKib(qreal kib);
|
||||
[[nodiscard]] QVariantMap formatBytes(qreal bytes);
|
||||
[[nodiscard]] QVariantMap formatBytesTotal(qreal bytes);
|
||||
struct FormatResult {
|
||||
Q_GADGET
|
||||
QML_ANONYMOUS
|
||||
|
||||
Q_PROPERTY(qreal value MEMBER value CONSTANT)
|
||||
Q_PROPERTY(qreal total MEMBER total CONSTANT)
|
||||
Q_PROPERTY(QString unit MEMBER unit CONSTANT)
|
||||
|
||||
public:
|
||||
qreal value;
|
||||
qreal total;
|
||||
QString unit;
|
||||
};
|
||||
|
||||
class UsageFmt : public QObject {
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
|
||||
public:
|
||||
Q_INVOKABLE [[nodiscard]] FormatResult formatKib(qreal kib, qreal total) const;
|
||||
};
|
||||
|
||||
} // namespace caelestia::services::usagefmt
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue