diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-02-13 19:36:36 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-02-13 19:42:39 +0100 |
commit | dcb5523e88389a669f5315d31f05a066530e8b9e (patch) | |
tree | ab95391e4787705c199494f4cee1bc0fbb0389c9 | |
parent | 30f873e8633340cc2f1d7aee4bb82526c673d639 (diff) | |
download | sink-dcb5523e88389a669f5315d31f05a066530e8b9e.tar.gz sink-dcb5523e88389a669f5315d31f05a066530e8b9e.zip |
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.
-rw-r--r-- | sinksh/syntax_modules/sink_list.cpp | 71 |
1 files changed, 45 insertions, 26 deletions
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) | |||
61 | return list.first().toUtf8(); | 61 | return list.first().toUtf8(); |
62 | } | 62 | } |
63 | 63 | ||
64 | QStringList printToList(const Sink::ApplicationDomain::ApplicationDomainType &o, bool compact, const QByteArrayList &toPrint) | ||
65 | { | ||
66 | QStringList line; | ||
67 | line << compressId(compact, o.resourceInstanceIdentifier()); | ||
68 | line << compressId(compact, o.identifier()); | ||
69 | for (const auto &prop: toPrint) { | ||
70 | const auto value = o.getProperty(prop); | ||
71 | if (value.isValid()) { | ||
72 | if (value.canConvert<Sink::ApplicationDomain::Reference>()) { | ||
73 | line << compressId(compact, value.toByteArray()); | ||
74 | } else if (value.canConvert<QString>()) { | ||
75 | line << value.toString(); | ||
76 | } else if (value.canConvert<QByteArray>()) { | ||
77 | line << value.toByteArray(); | ||
78 | } else if (value.canConvert<QByteArrayList>()) { | ||
79 | line << value.value<QByteArrayList>().join(", "); | ||
80 | } else { | ||
81 | line << QString("Unprintable type: %1").arg(value.typeName()); | ||
82 | } | ||
83 | } else { | ||
84 | line << QString{}; | ||
85 | } | ||
86 | } | ||
87 | return line; | ||
88 | } | ||
89 | |||
64 | bool list(const QStringList &args_, State &state) | 90 | bool list(const QStringList &args_, State &state) |
65 | { | 91 | { |
66 | if (args_.isEmpty()) { | 92 | if (args_.isEmpty()) { |
@@ -72,6 +98,8 @@ bool list(const QStringList &args_, State &state) | |||
72 | 98 | ||
73 | auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); | 99 | auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); |
74 | 100 | ||
101 | bool asLine = true; | ||
102 | |||
75 | Sink::Query query; | 103 | Sink::Query query; |
76 | query.setId("list"); | 104 | query.setId("list"); |
77 | if (!SinkshUtils::applyFilter(query, options)) { | 105 | if (!SinkshUtils::applyFilter(query, options)) { |
@@ -97,48 +125,39 @@ bool list(const QStringList &args_, State &state) | |||
97 | } else { | 125 | } else { |
98 | query.requestedProperties = SinkshUtils::requestedProperties(type); | 126 | query.requestedProperties = SinkshUtils::requestedProperties(type); |
99 | } | 127 | } |
128 | } else { | ||
129 | asLine = false; | ||
100 | } | 130 | } |
101 | 131 | ||
102 | QStringList tableLine; | ||
103 | QByteArrayList toPrint; | 132 | QByteArrayList toPrint; |
133 | QStringList tableLine; | ||
104 | 134 | ||
105 | for (const auto &o : SinkshUtils::getStore(type).read(query)) { | 135 | for (const auto &o : SinkshUtils::getStore(type).read(query)) { |
106 | if (tableLine.isEmpty()) { | 136 | if (tableLine.isEmpty()) { |
107 | tableLine << QObject::tr("Resource") << QObject::tr("Identifier"); | 137 | tableLine << QObject::tr("Resource") << QObject::tr("Identifier"); |
108 | if (query.requestedProperties.isEmpty()) { | 138 | if (query.requestedProperties.isEmpty()) { |
109 | auto in = o.availableProperties(); | ||
110 | toPrint = o.availableProperties(); | 139 | toPrint = o.availableProperties(); |
111 | std::transform(in.constBegin(), in.constEnd(), std::back_inserter(tableLine), [] (const QByteArray &s) -> QString { return s; }); | 140 | std::sort(toPrint.begin(), toPrint.end()); |
112 | } else { | 141 | } else { |
113 | auto in = query.requestedProperties; | ||
114 | toPrint = query.requestedProperties; | 142 | toPrint = query.requestedProperties; |
143 | std::sort(toPrint.begin(), toPrint.end()); | ||
144 | } | ||
145 | if (asLine) { | ||
146 | auto in = toPrint; | ||
115 | std::transform(in.constBegin(), in.constEnd(), std::back_inserter(tableLine), [] (const QByteArray &s) -> QString { return s; }); | 147 | std::transform(in.constBegin(), in.constEnd(), std::back_inserter(tableLine), [] (const QByteArray &s) -> QString { return s; }); |
148 | state.stageTableLine(tableLine); | ||
116 | } | 149 | } |
117 | state.stageTableLine(tableLine); | ||
118 | } | 150 | } |
119 | 151 | if (asLine) { | |
120 | QStringList line; | 152 | state.stageTableLine(printToList(o, compact, toPrint)); |
121 | line << compressId(compact, o.resourceInstanceIdentifier()); | 153 | } else { |
122 | line << compressId(compact, o.identifier()); | 154 | auto list = printToList(o, compact, toPrint); |
123 | for (const auto &prop: toPrint) { | 155 | state.stageTableLine(QStringList() << "Resource: " << list.value(0)); |
124 | const auto value = o.getProperty(prop); | 156 | state.stageTableLine(QStringList() << "Identifier: " << list.value(1)); |
125 | if (value.isValid()) { | 157 | for (int i = 0; i < (list.size() - 2); i++) { |
126 | if (value.canConvert<Sink::ApplicationDomain::Reference>()) { | 158 | state.stageTableLine(QStringList() << toPrint.value(i) << list.value(i + 2)); |
127 | line << compressId(compact, value.toByteArray()); | ||
128 | } else if (value.canConvert<QString>()) { | ||
129 | line << value.toString(); | ||
130 | } else if (value.canConvert<QByteArray>()) { | ||
131 | line << value.toByteArray(); | ||
132 | } else if (value.canConvert<QByteArrayList>()) { | ||
133 | line << value.value<QByteArrayList>().join(", "); | ||
134 | } else { | ||
135 | line << QString("Unprintable type: %1").arg(value.typeName()); | ||
136 | } | ||
137 | } else { | ||
138 | line << QString{}; | ||
139 | } | 159 | } |
140 | } | 160 | } |
141 | state.stageTableLine(line); | ||
142 | } | 161 | } |
143 | state.flushTable(); | 162 | state.flushTable(); |
144 | state.commandFinished(); | 163 | state.commandFinished(); |