Display overall status info in CLI

This commit is contained in:
Martchus 2018-07-05 17:39:35 +02:00
parent 321d6fdd60
commit 7166f6532a
6 changed files with 33 additions and 7 deletions

View File

@ -15,6 +15,7 @@
#include <c++utilities/application/failure.h>
#include <c++utilities/chrono/timespan.h>
#include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/ansiescapecodes.h>
@ -465,8 +466,9 @@ void Application::findRelevantDirsAndDevs(OperationType operationType)
}
}
// when displaying status information and no dirs/devs have been specified, just print information for all
const bool displayEverything = operationType == OperationType::Status && m_relevantDirs.empty() && m_relevantDevs.empty();
// when displaying status information and no stats and no dirs/devs have been specified, just print information for all
const bool displayEverything
= operationType == OperationType::Status && !m_args.stats.isPresent() && m_relevantDirs.empty() && m_relevantDevs.empty();
if (allDirs || (!allDevs && displayEverything)) {
m_relevantDirs.reserve(m_connection.dirInfo().size());
for (const SyncthingDir &dir : m_connection.dirInfo()) {
@ -564,6 +566,17 @@ void Application::printStatus(const ArgumentOccurrence &)
{
findRelevantDirsAndDevs(OperationType::Status);
// display stats
if (m_args.stats.isPresent() || (!m_args.dir.isPresent() && !m_args.dev.isPresent())) {
cout << TextAttribute::Bold << "Overall statistics\n" << TextAttribute::Reset;
printProperty("Incoming traffic", trafficString(m_connection.totalIncomingTraffic(), m_connection.totalIncomingRate()));
printProperty("Outgoing traffic", trafficString(m_connection.totalOutgoingTraffic(), m_connection.totalOutgoingRate()));
const auto &connectedDevices(m_connection.connectedDevices());
printProperty("Connected to", argsToString(connectedDevices.size(), ' ', connectedDevices.size() == 1 ? "device" : "devices", ':'));
printProperty("", displayNames(connectedDevices));
cout << '\n';
}
// display dirs
if (!m_relevantDirs.empty()) {
cout << TextAttribute::Bold << "Directories\n" << TextAttribute::Reset;

View File

@ -6,7 +6,7 @@ namespace Cli {
Args::Args()
: help(parser)
, status("status", 's', "shows the status (for all dirs and devs if none specified)")
, status("status", 's', "shows the overall status and/or directory/device specific status")
, log("log", 'l', "shows the Syncthing log")
, stop("stop", '\0', "stops Syncthing")
, restart("restart", '\0', "restarts Syncthing")
@ -25,6 +25,7 @@ Args::Args()
, script("script", '\0', "runs the specified UTF-8 encoded ECMAScript on the configuration rather than opening an editor", { "path" })
, jsLines("js-lines", '\0', "runs the specified ECMAScript lines on the configuration rather than opening an editor", { "line" })
, dryRun("dry-run", '\0', "writes the altered configuration to stdout instead of posting it to Syncthing")
, stats("stats", '\0', "shows overall statistics")
, dir("dir", 'd', "specifies a directory by ID", { "ID" })
, dev("dev", '\0', "specifies a device by ID or name", { "ID/name" })
, allDirs("all-dirs", '\0', "applies the operation for all directories")
@ -44,7 +45,7 @@ Args::Args()
ValueCompletionBehavior::PreDefinedValues | ValueCompletionBehavior::Directories | ValueCompletionBehavior::InvokeCallback);
dev.setConstraints(0, Argument::varValueCount);
dev.setValueCompletionBehavior(ValueCompletionBehavior::PreDefinedValues | ValueCompletionBehavior::InvokeCallback);
status.setSubArguments({ &dir, &dev, &allDirs, &allDevs });
status.setSubArguments({ &stats, &dir, &dev, &allDirs, &allDevs });
status.setExample(PROJECT_NAME " status # shows all dirs and devs\n" PROJECT_NAME " status --dir dir1 --dir dir2 --dev dev1 --dev dev2");
waitForIdle.setSubArguments({ &dir, &dev, &allDirs, &allDevs, &atLeast, &timeout });
waitForIdle.setExample(PROJECT_NAME " wait-for-idle --timeout 1800000 --at-least 5000 && systemctl poweroff\n" PROJECT_NAME

View File

@ -15,7 +15,7 @@ struct Args {
OperationArgument status, log, stop, restart, rescan, rescanAll, pause, resume, waitForIdle, pwd, cat, edit;
OperationArgument statusPwd, rescanPwd, pausePwd, resumePwd;
ConfigValueArgument script, jsLines, dryRun;
ConfigValueArgument dir, dev, allDirs, allDevs;
ConfigValueArgument stats, dir, dev, allDirs, allDevs;
ConfigValueArgument atLeast, timeout;
ConfigValueArgument editor;
ConfigValueArgument configFile, apiKey, url, credentials, certificate;

View File

@ -31,6 +31,12 @@ inline void printProperty(const char *propName, const QString &value, const char
printProperty(propName, value.toLocal8Bit().data(), suffix, indentation);
}
inline void printProperty(
const char *propName, const std::string &value, const char *suffix = nullptr, ApplicationUtilities::Indentation indentation = 3)
{
printProperty(propName, value.data(), suffix, indentation);
}
inline void printProperty(
const char *propName, const QStringList &value, const char *suffix = nullptr, ApplicationUtilities::Indentation indentation = 3)
{

View File

@ -46,7 +46,7 @@ set(TS_FILES
)
# find c++utilities
find_package(c++utilities 4.9.0 REQUIRED)
find_package(c++utilities 4.15.0 REQUIRED)
use_cpp_utilities()
set(META_PUBLIC_SHARED_LIB_DEPENDS c++utilities)
set(META_PUBLIC_STATIC_LIB_DEPENDS c++utilities_static)

View File

@ -57,8 +57,14 @@ template <class Objects, class Accessor> QStringList LIB_SYNCTHING_CONNECTOR_EXP
template <class Objects> QStringList LIB_SYNCTHING_CONNECTOR_EXPORT ids(const Objects &objects)
{
return things(objects, [](const auto &object) { return object.id; });
return things(objects, [](const auto &object) { return Traits::dereferenceMaybe(object).id; });
}
template <class Objects> QStringList LIB_SYNCTHING_CONNECTOR_EXPORT displayNames(const Objects &objects)
{
return things(objects, [](const auto &object) { return Traits::dereferenceMaybe(object).displayName(); });
}
} // namespace Data
#endif // DATA_UTILS_H