File: helper.h

package info (click to toggle)
syncthingtray 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,124 kB
  • sloc: cpp: 34,081; xml: 1,705; java: 1,258; sh: 97; javascript: 54; makefile: 25
file content (78 lines) | stat: -rw-r--r-- 2,850 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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>
#include <c++utilities/misc/traits.h>

#include <QString>
#include <QStringList>

#include <cstring>
#include <iostream>

namespace Cli {

inline void printProperty(const char *propName, const char *value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    if (*value) {
        std::cout << indentation << propName << CppUtilities::Indentation(static_cast<unsigned char>(30 - strlen(propName))) << value;
        if (suffix) {
            std::cout << ' ' << suffix;
        }
        std::cout << '\n';
    }
}

inline void printProperty(const char *propName, const QString &value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    printProperty(propName, value.toLocal8Bit().data(), suffix, indentation);
}

inline void printProperty(const char *propName, const std::string &value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    printProperty(propName, value.data(), suffix, indentation);
}

inline void printProperty(const char *propName, const QStringList &value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    for (const QString &str : value) {
        printProperty(propName, str, suffix, indentation);
        propName = "";
    }
}

inline void printProperty(
    const char *propName, CppUtilities::TimeSpan timeSpan, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    if (!timeSpan.isNull()) {
        printProperty(propName, timeSpan.toString(CppUtilities::TimeSpanOutputFormat::WithMeasures).data(), suffix, indentation);
    }
}

inline void printProperty(
    const char *propName, CppUtilities::DateTime dateTime, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    if (!dateTime.isNull()) {
        printProperty(propName, dateTime.toString().data(), suffix, indentation);
    }
}

inline void printProperty(const char *propName, bool value, const char *suffix = nullptr, CppUtilities::Indentation indentation = 3)
{
    printProperty(propName, value ? "yes" : "no", suffix, indentation);
}

template <typename NumberType, CppUtilities::Traits::EnableIfAny<std::is_floating_point<NumberType>, std::is_integral<NumberType>> * = nullptr>
inline void printProperty(
    const char *propName, const NumberType value, const char *suffix = nullptr, bool force = false, CppUtilities::Indentation indentation = 3)
{
    if (value >= 0 || force) {
        printProperty(propName, CppUtilities::numberToString<NumberType>(value).data(), suffix, indentation);
    }
}
} // namespace Cli

#endif // SYNCTHINGCTL_HELPER