From dcb5523e88389a669f5315d31f05a066530e8b9e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 13 Feb 2017 19:36:36 +0100 Subject: Support a property-per-line printing style in list Otherwise this becomes completely unreadable. Currently only used with --showall. With this "sinksh show" should no longer be necessary. --- sinksh/syntax_modules/sink_list.cpp | 71 +++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 26 deletions(-) (limited to 'sinksh') diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp index f6cae6b..94e6f00 100644 --- a/sinksh/syntax_modules/sink_list.cpp +++ b/sinksh/syntax_modules/sink_list.cpp @@ -61,6 +61,32 @@ QByteArray baIfAvailable(const QStringList &list) return list.first().toUtf8(); } +QStringList printToList(const Sink::ApplicationDomain::ApplicationDomainType &o, bool compact, const QByteArrayList &toPrint) +{ + QStringList line; + line << compressId(compact, o.resourceInstanceIdentifier()); + line << compressId(compact, o.identifier()); + for (const auto &prop: toPrint) { + const auto value = o.getProperty(prop); + if (value.isValid()) { + if (value.canConvert()) { + line << compressId(compact, value.toByteArray()); + } else if (value.canConvert()) { + line << value.toString(); + } else if (value.canConvert()) { + line << value.toByteArray(); + } else if (value.canConvert()) { + line << value.value().join(", "); + } else { + line << QString("Unprintable type: %1").arg(value.typeName()); + } + } else { + line << QString{}; + } + } + return line; +} + bool list(const QStringList &args_, State &state) { if (args_.isEmpty()) { @@ -72,6 +98,8 @@ bool list(const QStringList &args_, State &state) auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); + bool asLine = true; + Sink::Query query; query.setId("list"); if (!SinkshUtils::applyFilter(query, options)) { @@ -97,48 +125,39 @@ bool list(const QStringList &args_, State &state) } else { query.requestedProperties = SinkshUtils::requestedProperties(type); } + } else { + asLine = false; } - QStringList tableLine; QByteArrayList toPrint; + QStringList tableLine; for (const auto &o : SinkshUtils::getStore(type).read(query)) { if (tableLine.isEmpty()) { tableLine << QObject::tr("Resource") << QObject::tr("Identifier"); if (query.requestedProperties.isEmpty()) { - auto in = o.availableProperties(); toPrint = o.availableProperties(); - std::transform(in.constBegin(), in.constEnd(), std::back_inserter(tableLine), [] (const QByteArray &s) -> QString { return s; }); + std::sort(toPrint.begin(), toPrint.end()); } else { - auto in = query.requestedProperties; toPrint = query.requestedProperties; + std::sort(toPrint.begin(), toPrint.end()); + } + if (asLine) { + auto in = toPrint; std::transform(in.constBegin(), in.constEnd(), std::back_inserter(tableLine), [] (const QByteArray &s) -> QString { return s; }); + state.stageTableLine(tableLine); } - state.stageTableLine(tableLine); } - - QStringList line; - line << compressId(compact, o.resourceInstanceIdentifier()); - line << compressId(compact, o.identifier()); - for (const auto &prop: toPrint) { - const auto value = o.getProperty(prop); - if (value.isValid()) { - if (value.canConvert()) { - line << compressId(compact, value.toByteArray()); - } else if (value.canConvert()) { - line << value.toString(); - } else if (value.canConvert()) { - line << value.toByteArray(); - } else if (value.canConvert()) { - line << value.value().join(", "); - } else { - line << QString("Unprintable type: %1").arg(value.typeName()); - } - } else { - line << QString{}; + if (asLine) { + state.stageTableLine(printToList(o, compact, toPrint)); + } else { + auto list = printToList(o, compact, toPrint); + state.stageTableLine(QStringList() << "Resource: " << list.value(0)); + state.stageTableLine(QStringList() << "Identifier: " << list.value(1)); + for (int i = 0; i < (list.size() - 2); i++) { + state.stageTableLine(QStringList() << toPrint.value(i) << list.value(i + 2)); } } - state.stageTableLine(line); } state.flushTable(); state.commandFinished(); -- cgit v1.2.3