refactor: debug count and popup (#4921)
* Moved implementation of the methods to the `cpp` file. * Added `DebugCount::Flag(s)` and `DebugCount::configure(name, flags)`. * Moved from `QMap` to `std::map` (order is important here). * Used `QStringBuilder` for concatenations. * Used `QLocale` for formatting (adds separators). * Added `DebugCount::Flag::DataSize` for data sizes in bytes (and fixed language to English). * Used `DataSize` for image sizes (maybe this should be moved somewhere else?). * Added copy button to popup. * Fixed Image usage reporting being eight times too large (could be another PR, but honestly it's four characters).
This commit is contained in:
+109
-2
@@ -1,7 +1,114 @@
|
||||
#include "DebugCount.hpp"
|
||||
#include "util/DebugCount.hpp"
|
||||
|
||||
#include "common/UniqueAccess.hpp"
|
||||
|
||||
#include <QLocale>
|
||||
#include <QStringBuilder>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace chatterino;
|
||||
|
||||
struct Count {
|
||||
int64_t value = 0;
|
||||
DebugCount::Flags flags = DebugCount::Flag::None;
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
UniqueAccess<std::map<QString, Count>> COUNTS;
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
UniqueAccess<QMap<QString, int64_t>> DebugCount::counts_;
|
||||
void DebugCount::configure(const QString &name, Flags flags)
|
||||
{
|
||||
auto counts = COUNTS.access();
|
||||
|
||||
auto it = counts->find(name);
|
||||
if (it == counts->end())
|
||||
{
|
||||
counts->emplace(name, Count{.flags = flags});
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.flags = flags;
|
||||
}
|
||||
}
|
||||
|
||||
void DebugCount::set(const QString &name, const int64_t &amount)
|
||||
{
|
||||
auto counts = COUNTS.access();
|
||||
|
||||
auto it = counts->find(name);
|
||||
if (it == counts->end())
|
||||
{
|
||||
counts->emplace(name, Count{amount});
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.value = amount;
|
||||
}
|
||||
}
|
||||
|
||||
void DebugCount::increase(const QString &name, const int64_t &amount)
|
||||
{
|
||||
auto counts = COUNTS.access();
|
||||
|
||||
auto it = counts->find(name);
|
||||
if (it == counts->end())
|
||||
{
|
||||
counts->emplace(name, Count{amount});
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.value += amount;
|
||||
}
|
||||
}
|
||||
|
||||
void DebugCount::decrease(const QString &name, const int64_t &amount)
|
||||
{
|
||||
auto counts = COUNTS.access();
|
||||
|
||||
auto it = counts->find(name);
|
||||
if (it == counts->end())
|
||||
{
|
||||
counts->emplace(name, Count{-amount});
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.value -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
QString DebugCount::getDebugText()
|
||||
{
|
||||
#if QT_VERSION > QT_VERSION_CHECK(5, 13, 0)
|
||||
static const QLocale locale(QLocale::English);
|
||||
#else
|
||||
static QLocale locale(QLocale::English);
|
||||
#endif
|
||||
|
||||
auto counts = COUNTS.access();
|
||||
|
||||
QString text;
|
||||
for (const auto &[key, count] : *counts)
|
||||
{
|
||||
QString formatted;
|
||||
if (count.flags.has(Flag::DataSize))
|
||||
{
|
||||
formatted = locale.formattedDataSize(count.value);
|
||||
}
|
||||
else
|
||||
{
|
||||
formatted = locale.toString(static_cast<qlonglong>(count.value));
|
||||
}
|
||||
|
||||
text += key % ": " % formatted % '\n';
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user