syncthingtray/cli/helper.h

79 lines
2.8 KiB
C
Raw Normal View History

2016-10-02 21:59:28 +02:00
#ifndef SYNCTHINGCTL_HELPER
#define SYNCTHINGCTL_HELPER
#include <c++utilities/application/commandlineutils.h>
#include <c++utilities/chrono/datetime.h>
#include <c++utilities/chrono/timespan.h>
#include <c++utilities/conversion/stringconversion.h>
2017-09-17 20:33:01 +02:00
#include <c++utilities/misc/traits.h>
2016-10-02 21:59:28 +02:00
#include <QString>
#include <QStringList>
#include <cstring>
2017-05-01 03:34:43 +02:00
#include <iostream>
2016-10-02 21:59:28 +02:00
namespace Cli {
2019-06-10 22:48:26 +02:00
inline void printProperty(const char *propName, const char *value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
2017-05-01 03:34:43 +02:00
if (*value) {
2021-03-20 22:39:40 +01:00
std::cout << indentation << propName << CppUtilities::Indentation(static_cast<unsigned char>(30 - strlen(propName))) << value;
2017-05-01 03:34:43 +02:00
if (suffix) {
2016-10-02 21:59:28 +02:00
std::cout << ' ' << suffix;
}
std::cout << '\n';
}
}
2019-06-10 22:48:26 +02:00
inline void printProperty(const char *propName, const QString &value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
printProperty(propName, value.toLocal8Bit().data(), suffix, indentation);
}
2019-06-10 22:48:26 +02:00
inline void printProperty(const char *propName, const std::string &value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2018-07-05 17:39:35 +02:00
{
printProperty(propName, value.data(), suffix, indentation);
}
2019-06-10 22:48:26 +02:00
inline void printProperty(const char *propName, const QStringList &value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
2017-05-01 03:34:43 +02:00
for (const QString &str : value) {
2016-10-02 21:59:28 +02:00
printProperty(propName, str, suffix, indentation);
propName = "";
}
}
2017-05-01 03:34:43 +02:00
inline void printProperty(
2019-06-10 22:48:26 +02:00
const char *propName, CppUtilities::TimeSpan timeSpan, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
2017-05-01 03:34:43 +02:00
if (!timeSpan.isNull()) {
2019-06-10 22:48:26 +02:00
printProperty(propName, timeSpan.toString(CppUtilities::TimeSpanOutputFormat::WithMeasures).data(), suffix, indentation);
2016-10-02 21:59:28 +02:00
}
}
2017-05-01 03:34:43 +02:00
inline void printProperty(
2019-06-10 22:48:26 +02:00
const char *propName, CppUtilities::DateTime dateTime, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
2017-05-01 03:34:43 +02:00
if (!dateTime.isNull()) {
2016-10-02 21:59:28 +02:00
printProperty(propName, dateTime.toString().data(), suffix, indentation);
}
}
2019-06-10 22:48:26 +02:00
inline void printProperty(const char *propName, bool value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
printProperty(propName, value ? "yes" : "no", suffix, indentation);
}
2019-06-10 22:48:26 +02:00
template <typename NumberType, CppUtilities::Traits::EnableIfAny<std::is_floating_point<NumberType>, std::is_integral<NumberType>> * = nullptr>
2017-05-01 03:34:43 +02:00
inline void printProperty(
2019-06-10 22:48:26 +02:00
const char *propName, const NumberType value, const char *suffix = nullptr, bool force = false, CppUtilities::Indentation indentation = 3)
2016-10-02 21:59:28 +02:00
{
if (value >= 0 || force) {
2019-06-10 22:48:26 +02:00
printProperty(propName, CppUtilities::numberToString<NumberType>(value).data(), suffix, indentation);
2016-10-02 21:59:28 +02:00
}
}
2017-09-17 20:33:01 +02:00
} // namespace Cli
2016-10-02 21:59:28 +02:00
#endif // SYNCTHINGCTL_HELPER