Skip to content

Commit

Permalink
core/command: add option to select newest matching instance
Browse files Browse the repository at this point in the history
  • Loading branch information
outfoxxed committed Jan 25, 2025
1 parent b289bfa commit 325be88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/launch/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ int locateConfigFile(CommandState& cmd, QString& path) {
return 0;
}

void sortInstances(QVector<InstanceLockInfo>& list) {
std::ranges::sort(list, [](const InstanceLockInfo& a, const InstanceLockInfo& b) {
return a.instance.launchTime < b.instance.launchTime;
void sortInstances(QVector<InstanceLockInfo>& list, bool newestFirst) {
std::ranges::sort(list, [=](const InstanceLockInfo& a, const InstanceLockInfo& b) {
auto r = a.instance.launchTime < b.instance.launchTime;
return newestFirst ? !r : r;
});
};

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

auto instances = QsPaths::collectInstances(path);
sortInstances(instances);
sortInstances(instances, cmd.config.newest);

if (instances.isEmpty()) {
qCInfo(logBare) << "No running instances for" << configFilePath;
Expand Down Expand Up @@ -227,7 +228,7 @@ int listInstances(CommandState& cmd) {
qCInfo(logBare) << "Use --all to list all instances.";
}
} else {
sortInstances(instances);
sortInstances(instances, cmd.config.newest);

if (cmd.output.json) {
auto array = QJsonArray();
Expand Down
1 change: 1 addition & 0 deletions src/launch/launch_p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct CommandState {
QStringOption path;
QStringOption manifest;
QStringOption name;
bool newest = false;
} config;

struct {
Expand Down
13 changes: 9 additions & 4 deletions src/launch/parsecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int parseCommand(int argc, char** argv, CommandState& state) {
.argv = argv,
};

auto addConfigSelection = [&](CLI::App* cmd) {
auto addConfigSelection = [&](CLI::App* cmd, bool withNewestOption = false) {
auto* group = cmd->add_option_group("Config Selection")
->description("If no options in this group are specified,\n"
"$XDG_CONFIG_HOME/quickshell/shell.qml will be used.");
Expand All @@ -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.")
->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;
};

Expand Down Expand Up @@ -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.");

addConfigSelection(sub)->excludes(all);
addConfigSelection(sub, true)->excludes(all);
addLoggingOptions(sub, false, true);

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

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

auto* instance = addInstanceSelection(sub);
addConfigSelection(sub)->excludes(instance);
addConfigSelection(sub, true)->excludes(instance);
addLoggingOptions(sub, false, true);

sub->require_option();
Expand Down

0 comments on commit 325be88

Please sign in to comment.