Skip to content

Commit

Permalink
Support for more console.log with variable number of args.
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Treat <[email protected]>
  • Loading branch information
manyoso committed Dec 21, 2024
1 parent c7d7345 commit da23636
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions gpt4all-chat/src/codeinterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,79 @@ class JavaScriptConsoleCapture : public QObject
Q_OBJECT
public:
QString output;
Q_INVOKABLE void log(const QString &message)
Q_INVOKABLE void log(const QJSValue &a)
{
logInternal(a, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b)
{
logInternal(a);
logInternal(b, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b, const QJSValue &c)
{
logInternal(a);
logInternal(b);
logInternal(c, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b, const QJSValue &c, const QJSValue &d)
{
logInternal(a);
logInternal(b);
logInternal(c);
logInternal(d, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b, const QJSValue &c, const QJSValue &d,
const QJSValue &e)
{
logInternal(a);
logInternal(b);
logInternal(c);
logInternal(d);
logInternal(e, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b, const QJSValue &c, const QJSValue &d,
const QJSValue &e, const QJSValue &f)
{
logInternal(a);
logInternal(b);
logInternal(c);
logInternal(d);
logInternal(e);
logInternal(f, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b, const QJSValue &c, const QJSValue &d,
const QJSValue &e, const QJSValue &f, const QJSValue &g)
{
logInternal(a);
logInternal(b);
logInternal(c);
logInternal(d);
logInternal(e);
logInternal(f);
logInternal(g, true);
}
Q_INVOKABLE void log(const QJSValue &a, const QJSValue &b, const QJSValue &c, const QJSValue &d,
const QJSValue &e, const QJSValue &f, const QJSValue &g, const QJSValue &h)
{
logInternal(a);
logInternal(b);
logInternal(c);
logInternal(d);
logInternal(e);
logInternal(f);
logInternal(g);
logInternal(h, true);
}

private:
void logInternal(const QJSValue &value, bool newLine = false)
{
const int maxLength = 1024;
if (output.length() >= maxLength)
return;

const QString message = value.toString();
if (output.length() + message.length() + 1 > maxLength) {
static const QString trunc = "\noutput truncated at " + QString::number(maxLength) + " characters...";
int remainingLength = maxLength - output.length();
Expand All @@ -28,7 +95,9 @@ class JavaScriptConsoleCapture : public QObject
output.append(trunc);
Q_ASSERT(output.length() > maxLength);
} else {
output.append(message + "\n");
if (!output.isEmpty() && output.back() != '\n') output.append(' ');
output.append(message);
if (newLine) output.append('\n');
}
}
};
Expand Down

0 comments on commit da23636

Please sign in to comment.