33#include < cerrno>
44#include < cstdio>
55#include < cstring>
6+ #include < utility>
67
78#include < qconfig.h>
89#include < qcontainerfwd.h>
@@ -177,14 +178,33 @@ int selectInstance(CommandState& cmd, InstanceLockInfo* instance, bool deadFallb
177178 }
178179 } else if (!cmd.instance .id ->isEmpty ()) {
179180 path = basePath->filePath (" by-pid" );
180- auto instances = QsPaths::collectInstances (path, deadFallback );
181+ auto [liveInstances, deadInstances] = QsPaths::collectInstances (path);
181182
182- instances .removeIf ([&](const InstanceLockInfo& info) {
183+ liveInstances .removeIf ([&](const InstanceLockInfo& info) {
183184 return !info.instance .instanceId .startsWith (*cmd.instance .id );
184185 });
185186
187+ deadInstances.removeIf ([&](const InstanceLockInfo& info) {
188+ return !info.instance .instanceId .startsWith (*cmd.instance .id );
189+ });
190+
191+ auto instances = liveInstances.isEmpty () && deadFallback ? deadInstances : liveInstances;
192+
186193 if (instances.isEmpty ()) {
187- qCInfo (logBare) << " No running instances start with" << *cmd.instance .id ;
194+ if (deadFallback) {
195+ qCInfo (logBare) << " No instances start with" << *cmd.instance .id ;
196+ } else {
197+ qCInfo (logBare) << " No running instances start with" << *cmd.instance .id ;
198+
199+ if (!deadInstances.isEmpty ()) {
200+ qCInfo (logBare) << " Some dead instances match:" ;
201+
202+ for (auto & instance: deadInstances) {
203+ qCInfo (logBare).noquote () << " -" << instance.instance .instanceId ;
204+ }
205+ }
206+ }
207+
188208 return -1 ;
189209 } else if (instances.length () != 1 ) {
190210 qCInfo (logBare) << " More than one instance starts with" << *cmd.instance .id ;
@@ -208,14 +228,29 @@ int selectInstance(CommandState& cmd, InstanceLockInfo* instance, bool deadFallb
208228
209229 path = QDir (basePath->filePath (" by-path" )).filePath (pathId);
210230
211- auto instances = QsPaths::collectInstances (path, deadFallback);
231+ auto [liveInstances, deadInstances] = QsPaths::collectInstances (path);
232+
233+ auto instances = liveInstances;
234+ if (instances.isEmpty () && deadFallback) {
235+ instances = deadInstances;
236+ }
237+
212238 sortInstances (
213239 instances,
214240 cmd.config .newest || (!instances.empty () && instances.first ().pid == -1 )
215241 );
216242
217243 if (instances.isEmpty ()) {
218- qCInfo (logBare) << " No running instances for" << configFilePath;
244+ if (liveInstances.isEmpty () && deadInstances.length () > 1 ) {
245+ qCInfo (logBare) << " No running instances for" << configFilePath;
246+ qCInfo (logBare) << " Dead instances:" ;
247+ sortInstances (deadInstances, cmd.config .newest );
248+ for (auto & instance: deadInstances) {
249+ qCInfo (logBare).noquote () << " -" << instance.instance .instanceId ;
250+ }
251+ } else {
252+ qCInfo (logBare) << " No running instances for" << configFilePath;
253+ }
219254 return -1 ;
220255 }
221256
@@ -276,7 +311,18 @@ int listInstances(CommandState& cmd) {
276311 path = QDir (basePath->filePath (" by-path" )).filePath (pathId);
277312 }
278313
279- auto instances = QsPaths::collectInstances (path);
314+ auto [liveInstances, deadInstances] = QsPaths::collectInstances (path);
315+
316+ sortInstances (liveInstances, cmd.config .newest );
317+
318+ QList<InstanceLockInfo> instances;
319+ if (cmd.instance .includeDead ) {
320+ sortInstances (deadInstances, cmd.config .newest );
321+ instances = std::move (deadInstances);
322+ instances.append (liveInstances);
323+ } else {
324+ instances = std::move (liveInstances);
325+ }
280326
281327 if (instances.isEmpty ()) {
282328 if (cmd.instance .all ) {
@@ -286,7 +332,6 @@ int listInstances(CommandState& cmd) {
286332 qCInfo (logBare) << " Use --all to list all instances." ;
287333 }
288334 } else {
289- sortInstances (instances, cmd.config .newest );
290335
291336 if (cmd.output .json ) {
292337 auto array = QJsonArray ();
@@ -295,7 +340,7 @@ int listInstances(CommandState& cmd) {
295340 auto json = QJsonObject ();
296341
297342 json[" id" ] = instance.instance .instanceId ;
298- json[" pid" ] = instance.pid ;
343+ json[" pid" ] = instance.instance . pid ;
299344 json[" shell_id" ] = instance.instance .shellId ;
300345 json[" config_path" ] = instance.instance .configPath ;
301346 json[" launch_time" ] = instance.instance .launchTime .toString (Qt::ISODate);
@@ -319,12 +364,18 @@ int listInstances(CommandState& cmd) {
319364 .arg (remMinutes)
320365 .arg (remSeconds);
321366
367+ auto isDead = instance.pid == -1 ;
368+ auto gray = !cmd.log .noColor && isDead;
369+
322370 qCInfo (logBare).noquote ().nospace ()
323- << " Instance " << instance.instance .instanceId << " :\n "
324- << " Process ID: " << instance.pid << ' \n '
371+ << (gray ? " \033 [90m" : " " ) << " Instance " << instance.instance .instanceId
372+ << (isDead ? " (dead)" : " " ) << " :\n "
373+ << " Process ID: " << instance.instance .pid << ' \n '
325374 << " Shell ID: " << instance.instance .shellId << ' \n '
326375 << " Config path: " << instance.instance .configPath << ' \n '
327- << " Launch time: " << launchTimeStr << " (running for " << runtimeStr << " )\n " ;
376+ << " Launch time: " << launchTimeStr
377+ << (isDead ? " " : " (running for " + runtimeStr + " )" ) << ' \n '
378+ << (gray ? " \033 [0m" : " " );
328379 }
329380 }
330381 }
0 commit comments