12using namespace Qt::StringLiterals;
16 registerCommand({
"help"_L1,
"h"_L1 }, {}, tr(
"Show the list of available commands."),
17 [
this](
const QStringList &) {
29 const QString &description, CommandHandler handler)
31 m_commands.append(Command{ names, argsHint, description, std::move(handler) });
36 Q_ASSERT(!m_listenerThread.isRunning());
38 m_listener =
new QQmlDebugCommandListener;
39 m_listener->moveToThread(&m_listenerThread);
40 connect(m_listener, &QQmlDebugCommandListener::command,
this, &QQmlDebugConsole::handleLine);
42 connect(
this, &QQmlDebugConsole::requestCommand, m_listener,
43 &QQmlDebugCommandListener::readCommand);
45 m_listenerThread.start();
52 if (m_listenerThread.isRunning()) {
53 m_listenerThread.quit();
54 m_listenerThread.wait();
57 delete std::exchange(m_listener,
nullptr);
62 if (!message.isEmpty())
63 std::cerr << qPrintable(message) <<
std::endl;
66 emit requestCommand();
71 std::cerr << qPrintable(message) <<
std::endl;
75 std::function<
void()> onDeclined)
77 m_pendingQuestion = question;
78 m_onConfirmed = std::move(onConfirmed);
79 m_onDeclined = std::move(onDeclined);
85 const QStringList args = line.split(QChar::Space, Qt::SkipEmptyParts);
91 const QString keyword = args.first().toLower();
92 const QStringList rest = args.mid(1);
94 if (!m_pendingQuestion.isEmpty()) {
95 if (keyword ==
"y"_L1 || keyword ==
"yes"_L1) {
96 m_pendingQuestion.clear();
98 if (
auto onConfirmed = std::exchange(m_onConfirmed, {}))
100 }
else if (keyword ==
"n"_L1 || keyword ==
"no"_L1) {
101 m_pendingQuestion.clear();
103 if (
auto onDeclined = std::exchange(m_onDeclined, {}))
108 prompt(m_pendingQuestion);
113 for (
const Command &cmd : std::as_const(m_commands)) {
114 if (cmd.names.contains(keyword)) {
126 printLine(tr(
"The following commands are available:"));
127 for (
const Command &cmd : std::as_const(m_commands)) {
129 quoted.reserve(cmd.names.size());
130 for (
const QString &name : cmd.names)
131 quoted << u'\'' + name + u'\'';
132 QString header = quoted.join(
", "_L1);
133 if (!cmd.argsHint.isEmpty())
134 header += u' ' + cmd.argsHint;
136 printLine(
" "_L1 + cmd.description);
142#include "moc_qqmldebugconsole_p.cpp"
void askConfirmation(const QString &question, std::function< void()> onConfirmed, std::function< void()> onDeclined={})
QQmlDebugConsole(QObject *parent=nullptr)
void printLine(const QString &message)
void registerCommand(const QStringList &names, const QString &argsHint, const QString &description, CommandHandler handler)
void prompt(const QString &message=QString(), bool ready=true)