fix: properly handle CLI arguments (#2368)

Fix CLI arguments not being respected. This happened due to the addition of category-based logging (--help, --version) and changes to the window loading ( --channels), respectively.

When handling --channels, I took the liberty to refactor the previous version of window description (which relied on generating JSON) to directly building the WindowLayout.
This commit is contained in:
Leon Richardt
2021-01-23 16:26:42 +01:00
committed by GitHub
parent 55dd09a9f0
commit 1b75dc1e2c
6 changed files with 140 additions and 81 deletions
+79 -36
View File
@@ -3,11 +3,12 @@
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
#include "common/QLogging.hpp"
#include "singletons/Paths.hpp"
#include "singletons/WindowManager.hpp"
#include "util/CombinePath.hpp"
#include "widgets/Window.hpp"
namespace chatterino {
@@ -45,7 +46,7 @@ Args::Args(const QApplication &app)
if (parser.isSet("help"))
{
qCInfo(chatterinoArgs).noquote() << parser.helpText();
qInfo().noquote() << parser.helpText();
::exit(EXIT_SUCCESS);
}
@@ -56,44 +57,86 @@ Args::Args(const QApplication &app)
if (parser.isSet("c"))
{
QJsonArray channelArray;
QStringList channelArgList = parser.value("c").split(";");
for (QString channelArg : channelArgList)
{
// Twitch is default platform
QString platform = "t";
QString channelName = channelArg;
const QRegExp regExp("(.):(.*)");
if (regExp.indexIn(channelArg) != -1)
{
platform = regExp.cap(1);
channelName = regExp.cap(2);
}
// Twitch (default)
if (platform == "t")
{
// TODO: try not to parse JSON
QString channelObjectString =
"{\"splits2\": { \"data\": { \"name\": \"" + channelName +
"\", \"type\": \"twitch\" }, \"type\": \"split\" }}";
channelArray.push_back(
QJsonDocument::fromJson(channelObjectString.toUtf8())
.object());
}
}
if (channelArray.size() > 0)
{
this->dontSaveSettings = true;
this->channelsToJoin = channelArray;
}
this->applyCustomChannelLayout(parser.value("c"));
}
this->printVersion = parser.isSet("v");
this->crashRecovery = parser.isSet("crash-recovery");
}
void Args::applyCustomChannelLayout(const QString &argValue)
{
WindowLayout layout;
WindowDescriptor window;
/*
* There is only one window that is loaded from the --channels
* argument so that is what we use as the main window.
*/
window.type_ = WindowType::Main;
// Load main window layout from config file so we can use the same geometry
const QRect configMainLayout = [] {
const QString windowLayoutFile =
combinePath(getPaths()->settingsDirectory,
WindowManager::WINDOW_LAYOUT_FILENAME);
const WindowLayout configLayout =
WindowLayout::loadFromFile(windowLayoutFile);
for (const WindowDescriptor &window : configLayout.windows_)
{
if (window.type_ != WindowType::Main)
continue;
return window.geometry_;
}
return QRect(-1, -1, -1, -1);
}();
window.geometry_ = std::move(configMainLayout);
QStringList channelArgList = argValue.split(";");
for (const QString &channelArg : channelArgList)
{
if (channelArg.isEmpty())
continue;
// Twitch is default platform
QString platform = "t";
QString channelName = channelArg;
const QRegExp regExp("(.):(.*)");
if (regExp.indexIn(channelArg) != -1)
{
platform = regExp.cap(1);
channelName = regExp.cap(2);
}
// Twitch (default)
if (platform == "t")
{
TabDescriptor tab;
// Set first tab as selected
tab.selected_ = window.tabs_.empty();
tab.rootNode_ = SplitNodeDescriptor{"twitch", channelName};
window.tabs_.emplace_back(std::move(tab));
}
}
// Only respect --channels if we could actually parse any channels
if (!window.tabs_.empty())
{
this->dontSaveSettings = true;
layout.windows_.emplace_back(std::move(window));
this->customChannelLayout = std::move(layout);
}
}
static Args *instance = nullptr;
void initArgs(const QApplication &app)
+6 -2
View File
@@ -1,7 +1,8 @@
#pragma once
#include <QApplication>
#include <QJsonArray>
#include <boost/optional.hpp>
#include "common/WindowDescriptors.hpp"
namespace chatterino {
@@ -15,7 +16,10 @@ public:
bool crashRecovery{};
bool shouldRunBrowserExtensionHost{};
bool dontSaveSettings{};
QJsonArray channelsToJoin{};
boost::optional<WindowLayout> customChannelLayout;
private:
void applyCustomChannelLayout(const QString &argValue);
};
void initArgs(const QApplication &app);