forked from quickshell/quickshell
core/command: add option to select newest matching instance
This commit is contained in:
parent
b289bfa504
commit
325be8857c
src/launch
|
@ -102,9 +102,10 @@ int locateConfigFile(CommandState& cmd, QString& path) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sortInstances(QVector<InstanceLockInfo>& list) {
|
void sortInstances(QVector<InstanceLockInfo>& list, bool newestFirst) {
|
||||||
std::ranges::sort(list, [](const InstanceLockInfo& a, const InstanceLockInfo& b) {
|
std::ranges::sort(list, [=](const InstanceLockInfo& a, const InstanceLockInfo& b) {
|
||||||
return a.instance.launchTime < b.instance.launchTime;
|
auto r = a.instance.launchTime < b.instance.launchTime;
|
||||||
|
return newestFirst ? !r : r;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -153,7 +154,7 @@ int selectInstance(CommandState& cmd, InstanceLockInfo* instance) {
|
||||||
path = QDir(basePath->filePath("by-path")).filePath(pathId);
|
path = QDir(basePath->filePath("by-path")).filePath(pathId);
|
||||||
|
|
||||||
auto instances = QsPaths::collectInstances(path);
|
auto instances = QsPaths::collectInstances(path);
|
||||||
sortInstances(instances);
|
sortInstances(instances, cmd.config.newest);
|
||||||
|
|
||||||
if (instances.isEmpty()) {
|
if (instances.isEmpty()) {
|
||||||
qCInfo(logBare) << "No running instances for" << configFilePath;
|
qCInfo(logBare) << "No running instances for" << configFilePath;
|
||||||
|
@ -227,7 +228,7 @@ int listInstances(CommandState& cmd) {
|
||||||
qCInfo(logBare) << "Use --all to list all instances.";
|
qCInfo(logBare) << "Use --all to list all instances.";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sortInstances(instances);
|
sortInstances(instances, cmd.config.newest);
|
||||||
|
|
||||||
if (cmd.output.json) {
|
if (cmd.output.json) {
|
||||||
auto array = QJsonArray();
|
auto array = QJsonArray();
|
||||||
|
|
|
@ -49,6 +49,7 @@ struct CommandState {
|
||||||
QStringOption path;
|
QStringOption path;
|
||||||
QStringOption manifest;
|
QStringOption manifest;
|
||||||
QStringOption name;
|
QStringOption name;
|
||||||
|
bool newest = false;
|
||||||
} config;
|
} config;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
|
|
@ -16,7 +16,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
|
||||||
.argv = argv,
|
.argv = argv,
|
||||||
};
|
};
|
||||||
|
|
||||||
auto addConfigSelection = [&](CLI::App* cmd) {
|
auto addConfigSelection = [&](CLI::App* cmd, bool withNewestOption = false) {
|
||||||
auto* group = cmd->add_option_group("Config Selection")
|
auto* group = cmd->add_option_group("Config Selection")
|
||||||
->description("If no options in this group are specified,\n"
|
->description("If no options in this group are specified,\n"
|
||||||
"$XDG_CONFIG_HOME/quickshell/shell.qml will be used.");
|
"$XDG_CONFIG_HOME/quickshell/shell.qml will be used.");
|
||||||
|
@ -37,6 +37,11 @@ int parseCommand(int argc, char** argv, CommandState& state) {
|
||||||
"otherwise it is the name of a folder in $XDG_CONFIG_HOME/quickshell.")
|
"otherwise it is the name of a folder in $XDG_CONFIG_HOME/quickshell.")
|
||||||
->envname("QS_CONFIG_NAME");
|
->envname("QS_CONFIG_NAME");
|
||||||
|
|
||||||
|
if (withNewestOption) {
|
||||||
|
group->add_flag("-n,--newest", state.config.newest)
|
||||||
|
->description("Operate on the most recently launched instance instead of the oldest");
|
||||||
|
}
|
||||||
|
|
||||||
return group;
|
return group;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -146,7 +151,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
|
||||||
|
|
||||||
sub->add_flag("-j,--json", state.output.json, "Output the list as a json.");
|
sub->add_flag("-j,--json", state.output.json, "Output the list as a json.");
|
||||||
|
|
||||||
addConfigSelection(sub)->excludes(all);
|
addConfigSelection(sub, true)->excludes(all);
|
||||||
addLoggingOptions(sub, false, true);
|
addLoggingOptions(sub, false, true);
|
||||||
|
|
||||||
state.subcommand.list = sub;
|
state.subcommand.list = sub;
|
||||||
|
@ -156,7 +161,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
|
||||||
auto* sub = cli->add_subcommand("kill", "Kill quickshell instances.");
|
auto* sub = cli->add_subcommand("kill", "Kill quickshell instances.");
|
||||||
//sub->add_flag("-a,--all", "Kill all matching instances instead of just one.");
|
//sub->add_flag("-a,--all", "Kill all matching instances instead of just one.");
|
||||||
auto* instance = addInstanceSelection(sub);
|
auto* instance = addInstanceSelection(sub);
|
||||||
addConfigSelection(sub)->excludes(instance);
|
addConfigSelection(sub, true)->excludes(instance);
|
||||||
addLoggingOptions(sub, false, true);
|
addLoggingOptions(sub, false, true);
|
||||||
|
|
||||||
state.subcommand.kill = sub;
|
state.subcommand.kill = sub;
|
||||||
|
@ -182,7 +187,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
|
||||||
->excludes(arguments);
|
->excludes(arguments);
|
||||||
|
|
||||||
auto* instance = addInstanceSelection(sub);
|
auto* instance = addInstanceSelection(sub);
|
||||||
addConfigSelection(sub)->excludes(instance);
|
addConfigSelection(sub, true)->excludes(instance);
|
||||||
addLoggingOptions(sub, false, true);
|
addLoggingOptions(sub, false, true);
|
||||||
|
|
||||||
sub->require_option();
|
sub->require_option();
|
||||||
|
|
Loading…
Reference in a new issue