js: Print ConsoleMessages and color the output based on their kind :^)

This commit is contained in:
Emanuele Torre 2020-05-01 18:11:03 +02:00 committed by Andreas Kling
parent be1a5bf3f7
commit e861af2a5c
Notes: sideshowbarker 2024-07-19 07:06:54 +09:00

View file

@ -84,7 +84,6 @@ String read_next_piece()
StringBuilder piece;
do {
String line = s_editor->get_line(prompt_for_level(s_repl_line_level));
s_editor->add_to_history(line);
@ -389,6 +388,37 @@ void sigint_handler()
interrupt_interpreter();
}
void console_message_handler(JS::ConsoleMessage& message)
{
switch (message.kind) {
case JS::ConsoleMessageKind::Count:
case JS::ConsoleMessageKind::Log:
case JS::ConsoleMessageKind::Info:
case JS::ConsoleMessageKind::Trace:
puts(message.text.characters());
break;
case JS::ConsoleMessageKind::Debug:
printf("\033[36;1m");
puts(message.text.characters());
printf("\033[0m");
break;
case JS::ConsoleMessageKind::Warn:
printf("\033[33;1m");
puts(message.text.characters());
printf("\033[0m");
break;
case JS::ConsoleMessageKind::Error:
printf("\033[31;1m");
puts(message.text.characters());
printf("\033[0m");
break;
case JS::ConsoleMessageKind::Clear:
printf("\033[3J\033[H\033[2J");
fflush(stdout);
break;
}
};
int main(int argc, char** argv)
{
bool gc_on_every_allocation = false;
@ -416,6 +446,7 @@ int main(int argc, char** argv)
if (script_path == nullptr) {
interpreter = JS::Interpreter::create<ReplObject>();
interpreter->console().on_new_message = console_message_handler;
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
if (test_mode)
enable_test_mode(*interpreter);
@ -638,6 +669,7 @@ int main(int argc, char** argv)
repl(*interpreter);
} else {
interpreter = JS::Interpreter::create<JS::GlobalObject>();
interpreter->console().on_new_message = console_message_handler;
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
if (test_mode)
enable_test_mode(*interpreter);