Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for more console.log with variable number of args. #3341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading