diff --git a/src/launch/command.cpp b/src/launch/command.cpp index eb27df74..449fab79 100644 --- a/src/launch/command.cpp +++ b/src/launch/command.cpp @@ -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; }); }; @@ -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; @@ -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(); diff --git a/src/launch/launch_p.hpp b/src/launch/launch_p.hpp index d752edbc..1b59de86 100644 --- a/src/launch/launch_p.hpp +++ b/src/launch/launch_p.hpp @@ -49,6 +49,7 @@ struct CommandState { QStringOption path; QStringOption manifest; QStringOption name; + bool newest = false; } config; struct { diff --git a/src/launch/parsecommand.cpp b/src/launch/parsecommand.cpp index bee9dd00..9a88c4f5 100644 --- a/src/launch/parsecommand.cpp +++ b/src/launch/parsecommand.cpp @@ -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."); @@ -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; }; @@ -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; @@ -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; @@ -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();