Skip to content

Commit 325be88

Browse files
committed
core/command: add option to select newest matching instance
1 parent b289bfa commit 325be88

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/launch/command.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ int locateConfigFile(CommandState& cmd, QString& path) {
102102
return 0;
103103
}
104104

105-
void sortInstances(QVector<InstanceLockInfo>& list) {
106-
std::ranges::sort(list, [](const InstanceLockInfo& a, const InstanceLockInfo& b) {
107-
return a.instance.launchTime < b.instance.launchTime;
105+
void sortInstances(QVector<InstanceLockInfo>& list, bool newestFirst) {
106+
std::ranges::sort(list, [=](const InstanceLockInfo& a, const InstanceLockInfo& b) {
107+
auto r = a.instance.launchTime < b.instance.launchTime;
108+
return newestFirst ? !r : r;
108109
});
109110
};
110111

@@ -153,7 +154,7 @@ int selectInstance(CommandState& cmd, InstanceLockInfo* instance) {
153154
path = QDir(basePath->filePath("by-path")).filePath(pathId);
154155

155156
auto instances = QsPaths::collectInstances(path);
156-
sortInstances(instances);
157+
sortInstances(instances, cmd.config.newest);
157158

158159
if (instances.isEmpty()) {
159160
qCInfo(logBare) << "No running instances for" << configFilePath;
@@ -227,7 +228,7 @@ int listInstances(CommandState& cmd) {
227228
qCInfo(logBare) << "Use --all to list all instances.";
228229
}
229230
} else {
230-
sortInstances(instances);
231+
sortInstances(instances, cmd.config.newest);
231232

232233
if (cmd.output.json) {
233234
auto array = QJsonArray();

src/launch/launch_p.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct CommandState {
4949
QStringOption path;
5050
QStringOption manifest;
5151
QStringOption name;
52+
bool newest = false;
5253
} config;
5354

5455
struct {

src/launch/parsecommand.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
1616
.argv = argv,
1717
};
1818

19-
auto addConfigSelection = [&](CLI::App* cmd) {
19+
auto addConfigSelection = [&](CLI::App* cmd, bool withNewestOption = false) {
2020
auto* group = cmd->add_option_group("Config Selection")
2121
->description("If no options in this group are specified,\n"
2222
"$XDG_CONFIG_HOME/quickshell/shell.qml will be used.");
@@ -37,6 +37,11 @@ int parseCommand(int argc, char** argv, CommandState& state) {
3737
"otherwise it is the name of a folder in $XDG_CONFIG_HOME/quickshell.")
3838
->envname("QS_CONFIG_NAME");
3939

40+
if (withNewestOption) {
41+
group->add_flag("-n,--newest", state.config.newest)
42+
->description("Operate on the most recently launched instance instead of the oldest");
43+
}
44+
4045
return group;
4146
};
4247

@@ -146,7 +151,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
146151

147152
sub->add_flag("-j,--json", state.output.json, "Output the list as a json.");
148153

149-
addConfigSelection(sub)->excludes(all);
154+
addConfigSelection(sub, true)->excludes(all);
150155
addLoggingOptions(sub, false, true);
151156

152157
state.subcommand.list = sub;
@@ -156,7 +161,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
156161
auto* sub = cli->add_subcommand("kill", "Kill quickshell instances.");
157162
//sub->add_flag("-a,--all", "Kill all matching instances instead of just one.");
158163
auto* instance = addInstanceSelection(sub);
159-
addConfigSelection(sub)->excludes(instance);
164+
addConfigSelection(sub, true)->excludes(instance);
160165
addLoggingOptions(sub, false, true);
161166

162167
state.subcommand.kill = sub;
@@ -182,7 +187,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
182187
->excludes(arguments);
183188

184189
auto* instance = addInstanceSelection(sub);
185-
addConfigSelection(sub)->excludes(instance);
190+
addConfigSelection(sub, true)->excludes(instance);
186191
addLoggingOptions(sub, false, true);
187192

188193
sub->require_option();

0 commit comments

Comments
 (0)