mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:17:35 +00:00
LibJS+WebContent+Browser+js: Implement console.group() methods
This implements: - console.group() - console.groupCollapsed() - console.groupEnd() In the Browser, we use `<details>` for the groups, which is not actually implemented yet, so groups are always open. In the REPL, groups are non-interactive, but still indent any output. This looks weird since the console prompt and return values remain on the far left, but this matches what Node does so it's probably fine. :^) I expect `console.group()` is not used much outside of browsers.
This commit is contained in:
parent
ff5e07d718
commit
d702678d16
10 changed files with 281 additions and 25 deletions
|
@ -1125,51 +1125,71 @@ public:
|
|||
virtual void clear() override
|
||||
{
|
||||
js_out("\033[3J\033[H\033[2J");
|
||||
m_group_stack_depth = 0;
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Variant<Vector<JS::Value>, JS::Console::Trace> arguments) override
|
||||
virtual void end_group() override
|
||||
{
|
||||
if (m_group_stack_depth > 0)
|
||||
m_group_stack_depth--;
|
||||
}
|
||||
|
||||
// 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer
|
||||
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, PrinterArguments arguments) override
|
||||
{
|
||||
String indent = String::repeated(" ", m_group_stack_depth);
|
||||
|
||||
if (log_level == JS::Console::LogLevel::Trace) {
|
||||
auto trace = arguments.get<JS::Console::Trace>();
|
||||
StringBuilder builder;
|
||||
if (!trace.label.is_empty())
|
||||
builder.appendff("\033[36;1m{}\033[0m\n", trace.label);
|
||||
builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label);
|
||||
|
||||
for (auto& function_name : trace.stack)
|
||||
builder.appendff("-> {}\n", function_name);
|
||||
builder.appendff("{}-> {}\n", indent, function_name);
|
||||
|
||||
js_outln("{}", builder.string_view());
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
if (log_level == JS::Console::LogLevel::Group || log_level == JS::Console::LogLevel::GroupCollapsed) {
|
||||
auto group = arguments.get<JS::Console::Group>();
|
||||
js_outln("{}\033[36;1m{}\033[0m", indent, group.label);
|
||||
m_group_stack_depth++;
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
auto output = String::join(" ", arguments.get<Vector<JS::Value>>());
|
||||
m_console.output_debug_message(log_level, output);
|
||||
|
||||
switch (log_level) {
|
||||
case JS::Console::LogLevel::Debug:
|
||||
js_outln("\033[36;1m{}\033[0m", output);
|
||||
js_outln("{}\033[36;1m{}\033[0m", indent, output);
|
||||
break;
|
||||
case JS::Console::LogLevel::Error:
|
||||
case JS::Console::LogLevel::Assert:
|
||||
js_outln("\033[31;1m{}\033[0m", output);
|
||||
js_outln("{}\033[31;1m{}\033[0m", indent, output);
|
||||
break;
|
||||
case JS::Console::LogLevel::Info:
|
||||
js_outln("(i) {}", output);
|
||||
js_outln("{}(i) {}", indent, output);
|
||||
break;
|
||||
case JS::Console::LogLevel::Log:
|
||||
js_outln("{}", output);
|
||||
js_outln("{}{}", indent, output);
|
||||
break;
|
||||
case JS::Console::LogLevel::Warn:
|
||||
case JS::Console::LogLevel::CountReset:
|
||||
js_outln("\033[33;1m{}\033[0m", output);
|
||||
js_outln("{}\033[33;1m{}\033[0m", indent, output);
|
||||
break;
|
||||
default:
|
||||
js_outln("{}", output);
|
||||
js_outln("{}{}", indent, output);
|
||||
break;
|
||||
}
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
private:
|
||||
int m_group_stack_depth { 0 };
|
||||
};
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue